Extract SPTS from MPTS Using FFmpeg
Extracting a Single Program Transport Stream (SPTS) from a Multi-Program Transport Stream (MPTS) is a common task in broadcast and digital video processing. This guide provides a direct, step-by-step method to demultiplex a specific program from an MPTS file or network stream using FFmpeg. You will learn how to identify the programs within your stream and use FFmpeg’s mapping options to extract the desired program without re-encoding the video.
Step 1: Identify the Program ID (PID)
Before extracting a program, you must identify its Program ID or
Service ID within the MPTS. Use ffprobe to inspect the
stream:
ffprobe input.tsIn the output, look for the “Program” entries. They will look similar to this:
Program 101
Stream #0:0[0x101]: Video: h264
Stream #0:1[0x102]: Audio: mp3
Program 102
Stream #0:2[0x201]: Video: h264
Stream #0:3[0x202]: Audio: mp3
Note the program number you want to extract (for example,
101).
Step 2: Extract the SPTS using FFmpeg
To extract the program, use the -map option with the
p: specifier. This flag tells FFmpeg to select all video,
audio, and data streams associated with that specific program ID.
Run the following command:
ffmpeg -i input.ts -map 0:p:101 -c copy output.tsCommand Breakdown:
-i input.ts: Specifies the input MPTS file or stream URL.-map 0:p:101: Selects input index0(the first input) and maps program101(including all its associated video, audio, and subtitle streams).-c copy: Copies the streams directly without re-encoding. This process is extremely fast and preserves the original quality.output.ts: The resulting SPTS file containing only the selected program.
Extracting from a Live Network Stream
If you are receiving the MPTS over UDP or RTP, the syntax remains the same. Just replace the input file name with your network URL:
ffmpeg -i udp://239.0.0.1:1234 -map 0:p:101 -c copy output.ts