To remove all but two subtitles in FFmpeg without knowing their index numbers, use the -map option with the s:0 and s:1 selectors to explicitly select the first two subtitle streams. This approach bypasses the need for manual inspection or prior knowledge of stream IDs by targeting positional indices directly during the remuxing process.
Understanding Stream Mapping
FFmpeg handles multimedia files by organizing data into separate streams. A single video file can contain multiple tracks for video, audio, and subtitles. When you do not know the specific index number of a subtitle track, relying on automatic selection is risky because FFmpeg might pick any available subtitle stream based on its internal sorting order, which is not guaranteed to be consistent across different files or encodings. The most reliable method to keep exactly two subtitles is to use explicit stream mapping. The syntax-map 0:s:0 tells FFmpeg to take the first subtitle stream from the input file (index 0), and -map 0:s:1 takes the second. Any streams not explicitly mapped are discarded by default. This technique ensures that regardless of what other subtitle tracks exist in the file, only the first two are retained in the output.
This strategy is particularly useful when dealing with complex container formats like MKV or MP4, where multiple language tracks are common. For more context on handling multiple subtitles, see Unexpected Output When Using FFMPEG to Add Multiple English Subtitles to MP4 File. Understanding how FFmpeg selects streams prevents unexpected behavior where the wrong language track becomes the default.
Step-by-Step Execution
To execute this operation efficiently, follow these steps using the command line interface. Ensure you have FFmpeg installed and accessible from your terminal or command prompt.- Identify Input File: Locate the video file containing the subtitles. Let us assume the file is named
input.mkv. - Construct Command: Use the following base command structure:
ffmpeg -i input.mkv -map 0:v -map 0:a -map 0:s:0 -map 0:s:1 output.mkv. - Verify Selection: The
-map 0:vpart ensures the video stream is kept. The-map 0:akeeps the primary audio stream. The critical parts are-map 0:s:0and-map 0:s:1, which lock in the first two subtitle tracks. - Run and Monitor: Execute the command. FFmpeg will remux the file. If no errors appear, the output file will contain only the specified streams.
-map 0:a includes all audio streams. The key constraint here is strictly limiting the subtitle maps to indices 0 and 1. This avoids the pitfall of accidentally including a third subtitle track that might be present in the original file.
For users encountering issues with ASS subtitle formatting during such operations, refer to Using subtitles.ass with ffmpeg: Common Bugs and Solutions. While this guide focuses on stream selection, proper codec handling remains essential for visual integrity.
Handling Edge Cases
Sometimes, a file may contain fewer than two subtitle streams. In such cases, FFmpeg will raise an error if you attempt to map a non-existent index. To handle this gracefully, you can first inspect the file using theffprobe tool. Run ffprobe -v error -select_streams s -show_entries stream=index -of csv=p=0 input.mkv. This command lists all subtitle stream indices.
If the output shows only one index, such as 0, then you should only map -map 0:s:0. If it shows three indices (0, 1, 2), you map the first two. This diagnostic step confirms whether your assumption about having at least two subtitles holds true. It prevents runtime failures due to missing stream references.
Another edge case involves mixed stream types. Some containers embed subtitles within the video stream itself (like DVB subtitles). The -map 0:s selector targets distinct subtitle streams, not embedded ones. If your subtitles are embedded, you must use different filtering techniques, such as -vf subtitles=, which is outside the scope of simple stream remapping. Always verify the stream type using ffprobe before applying the mapping strategy.
Alternative Approaches
While explicit mapping is the most robust method for keeping specific indices, some users prefer removing unwanted streams by negative indexing. However, FFmpeg does not support negative indexing for streams in the same way it does for arrays in programming languages. You cannot simply say "keep everything except the last subtitle." Therefore, positive indexing with-map is the standard practice.
You might also consider using the -disposition flag to set default flags, but this does not remove streams; it only changes which one plays automatically. To physically reduce file size and complexity, removal via unmapped streams is necessary. For related troubleshooting on video processing errors, check Invalid PNG signature FFmpeg Error: How to Fix It, although this addresses image decoding rather than stream management.
Frequently Asked Questions
What happens if I map only one subtitle stream?
If you map only -map 0:s:0, the output file will contain exactly one subtitle track. All other subtitle streams in the source file will be discarded. This is useful when you want to strip down to a single language.
Can I use this method to add new subtitles?
No, the -map option only selects existing streams from the input file. To add new subtitles, you must use the -c:s codec setting along with -i to include an external subtitle file, then map it using a new stream index.
Does this re-encode the video?
No, if you do not specify any encoding parameters for video or audio, FFmpeg will copy the streams directly. This makes the process extremely fast and lossless, as it only manipulates the container structure.
How do I know which subtitle is which language?
Use ffprobe to inspect the metadata of each stream. Look for the language tag in the stream details. This allows you to identify which index corresponds to your desired language before constructing the map command.
Why did my command fail with "Stream not found"?
This error occurs when you try to map a stream index that does not exist in the input file. For example, if the file has only one subtitle track, mapping 0:s:1 will fail. Always verify the available streams first.