Map Specific PID Streams from MPEG TS Using FFmpeg
This article explains how to identify, select, and map specific
Packet Identifier (PID) streams from an MPEG Transport Stream (.ts)
using FFmpeg. You will learn how to use ffprobe to locate
the PIDs within your file and how to use the ffmpeg
command-line tool to map and extract those precise streams into a new
output file while preserving or modifying their PID assignments.
Step 1: Identify the PIDs and Stream Indexes
An MPEG Transport Stream contains multiple multiplexed streams (video, audio, subtitles, and metadata), each identified by a unique PID. Before you can map these streams, you must identify their corresponding stream indexes in FFmpeg.
Run the following ffprobe command to inspect your input
TS file:
ffprobe -i input.tsIn the output, look for the stream details. They will look similar to this:
Stream #0:0[0x101]: Video: h264 (High) ([27][0][0][0] / 0x001B)
Stream #0:1[0x102](eng): Audio: ac3 ([129][0][0][0] / 0x0081)
Stream #0:2[0x103](spa): Audio: ac3 ([129][0][0][0] / 0x0081)
In this example: * 0:0 is the stream index for the video
stream, which has the PID 0x101 (257 in decimal). *
0:1 is the stream index for the English audio stream, which
has the PID 0x102 (258 in decimal). * 0:2 is
the stream index for the Spanish audio stream, which has the PID
0x103 (259 in decimal).
Step 2: Map the Specific Streams
To extract only the video (0:0) and the English audio
(0:1) streams, use the -map option in
FFmpeg.
ffmpeg -i input.ts -map 0:0 -map 0:1 -c copy output.ts-i input.ts: Specifies the input TS file.-map 0:0: Selects the first stream (video with PID0x101).-map 0:1: Selects the second stream (audio with PID0x102).-c copy: Copies the video and audio packets directly without re-encoding, preserving quality and saving CPU resources.
Step 3: Set Specific PIDs in the Output File (Optional)
By default, FFmpeg will assign new, sequential PIDs to the streams in
the output TS file. If your playback equipment or software requires the
output streams to retain their original PIDs (or use specific custom
PIDs), you must use the -streamid option.
The -streamid option maps an output stream index to a
specific PID value (in hexadecimal or decimal):
ffmpeg -i input.ts -map 0:0 -map 0:1 -c copy -streamid 0:0x101 -streamid 1:0x102 output.ts-streamid 0:0x101: Assigns the PID0x101to the first mapped output stream (index 0).-streamid 1:0x102: Assigns the PID0x102to the second mapped output stream (index 1).