Map Specific Program IDs in MPEG-TS Using FFmpeg

MPEG Transport Streams (MPEG-TS) often carry multiple programs—such as different television channels—within a single stream, each containing its own video, audio, and data tracks. This guide explains how to use FFmpeg to target, select, and map specific program IDs or individual elementary stream PIDs (Packet Identifiers) from a multi-program transport stream (MPTS) into a clean output file.

Step 1: Identify the Program IDs and PIDs

Before mapping, you must identify the program numbers and stream PIDs inside your MPEG-TS file. Run ffprobe to inspect the file structure:

ffprobe input.ts

In the output, look for entries labeled Program (e.g., Program 101 or Program 102) and the streams associated with them. You will see lines like this:

Program 101
    Stream #0:0[0x101]: Video: h264
    Stream #0:1[0x102]: Audio: mp3
Program 102
    Stream #0:2[0x201]: Video: mpeg2video
    Stream #0:3[0x202]: Audio: ac3

Here, 101 and 102 are the Program IDs, while 0x101, 0x102, etc., are the packet identifiers (PIDs) for the individual video and audio streams.

Step 2: Map a Specific Program Using Program IDs

To extract an entire program (including all its video, audio, and subtitle streams) using its Program ID, use the -map option with the p:program_id specifier.

For example, to map only Program 101 and copy the streams without re-encoding:

ffmpeg -i input.ts -map 0:p:101 -c copy output.ts

Step 3: Map Specific Streams (Video or Audio) inside a Program

If you do not want the entire program, you can narrow your selection down to specific stream types (like audio only or video only) within that program.

To extract only the video from Program 101:

ffmpeg -i input.ts -map 0:p:101:v -c copy output.ts

To extract only the audio from Program 101:

ffmpeg -i input.ts -map 0:p:101:a -c copy output.ts

Step 4: Map by Stream Index

If your FFmpeg version does not fully support program specifiers for your specific file container, you can map the streams directly using their index numbers (e.g., 0:0 for the first stream, 0:1 for the second stream):

ffmpeg -i input.ts -map 0:0 -map 0:1 -c copy output.ts

This method is highly precise but requires you to verify the stream indexes beforehand using the ffprobe command from Step 1.