To prevent losing embedded subtitles when repairing video with FFmpeg, use the -c:s copy flag in your command. This tells FFmpeg to copy the subtitle streams directly without re-encoding them, ensuring they remain intact during the remuxing process. Always verify stream selection with -map 0 to guarantee all tracks are included in the output file.
Understanding the Problem of Lost Subtitles
When video files become corrupted or suffer from container errors, users often attempt to "repair" them by re-encoding the entire file using FFmpeg. A common mistake in this process is allowing FFmpeg to default behavior for subtitle streams. By default, if you do not explicitly specify how to handle subtitle streams, FFmpeg may drop them entirely or attempt to transcode them into a different format, which can result in data loss, synchronization issues, or complete disappearance of the subtitles in the final output.
This issue is particularly prevalent when dealing with complex containers like MKV or MP4 that support multiple subtitle formats such as ASS, SRT, or MOV_TEXT. If the repair process involves re-encoding the video and audio streams but neglects the subtitle streams, the resulting file will be missing those text overlays. The user is left with a video that plays correctly but lacks the necessary linguistic information provided by the subtitles.
The Solution: Using Stream Copying
The most effective way to preserve embedded subtitles during a video repair operation is to use stream copying rather than re-encoding. This method creates a new container structure without altering the underlying data of the subtitle tracks. To achieve this, you must explicitly instruct FFmpeg to copy the subtitle streams using the -c:s copy option.
Here is the basic structure of the command:
ffmpeg -i input.mp4 -c:v copy -c:a copy -c:s copy output.mp4
In this command, -c:v copy ensures the video stream is copied without re-encoding, -c:a copy does the same for audio, and -c:s copy preserves the subtitle streams exactly as they are. This approach is significantly faster than full re-encoding because it skips the computationally intensive decoding and encoding steps for all streams except those that might need correction.
However, simply adding -c:s copy is not always enough if FFmpeg decides not to include the subtitle streams in the output by default. In many cases, you need to explicitly map all streams from the input file to the output file. This is done using the -map 0 flag, which selects all streams from the first input file (0) and includes them in the output.
Ensuring All Streams Are Mapped
To guarantee that no streams are dropped, including subtitles, you should combine stream mapping with stream copying. The following command is a robust template for repairing video files while preserving all metadata and subtitle tracks:
ffmpeg -i corrupted_video.mkv -c copy -map 0 output_repaired.mkv
The -c copy shorthand applies the copy codec to all streams (video, audio, and subtitles) unless overridden by specific flags. The -map 0 ensures that every track detected in the input file is written to the output file. This is crucial because some containers may have hidden or optional streams that FFmpeg might ignore if not explicitly mapped.
If you encounter issues where subtitles are still missing, it may be due to incompatibility between the input and output containers. For example, trying to copy ASS subtitles into an MP4 container might fail because MP4 does not support the ASS format natively. In such cases, you may need to convert the subtitles to a compatible format like srt or mov_text, but this requires explicit conversion commands, not simple copying.
Advanced Scenarios and Troubleshooting
Sometimes, the subtitle stream might be corrupted along with the video stream. In these rare cases, copying the stream will also copy the corruption. If you suspect the subtitle data itself is damaged, you may need to extract the subtitles, fix them externally, and then mux them back in. However, for most "repair" scenarios involving container-level corruption, stream copying is the correct and safest approach.
Another common issue arises when the input file has multiple subtitle tracks. You might want to select only one specific subtitle track for the output. You can do this by specifying the stream index. For example, to select only the first subtitle track, you would use -map 0:s:0 instead of -map 0. This gives you precise control over which subtitles are included in the repaired file.
It is also important to note that some video repair tools work by truncating or fixing the header of the video file. If you are using a dedicated video repair tool before FFmpeg, ensure that the tool does not strip out subtitle tracks during its initial fix. FFmpeg can then be used as a final step to repack the file into a more stable container, using the -c copy method to preserve everything.
For users who frequently deal with subtitle compatibility, understanding the differences between container formats is helpful. As discussed in our guide on generating FFmpeg commands for VLC-style subtitles, proper muxing ensures that players like VLC can read the subtitle tracks correctly. Ensuring the output container supports the subtitle codec you are using is a key part of preventing lost subtitles.
Verifying the Output
After running the FFmpeg command, always verify that the subtitles are present in the output file. You can do this by inspecting the file's metadata using the -i flag followed by the output filename:
ffmpeg -i output_repaired.mkv
Look for lines starting with Stream #0:x:s in the output log. If these lines are present, it confirms that the subtitle streams were successfully copied into the new file. If they are missing, review your command to ensure that -map 0 and -c:s copy were correctly applied.
Best Practices for Video Repair
Always create a backup of the original corrupted file before attempting any repair operations. While FFmpeg is generally safe, any modification to a file carries a risk of further data loss. Additionally, consider using the -y flag to automatically overwrite the output file if it already exists, which can be useful in automated scripts but should be used with caution in manual operations.
Finally, remember that "repair" via FFmpeg is essentially a remuxing operation. It fixes container-level issues but does not fix content-level errors within the video or audio streams themselves. If the video frames are visually corrupted, stream copying will not resolve that issue, and re-encoding the video stream with error resilience settings may be necessary. However, for subtitle preservation, sticking to -c:s copy remains the gold standard.
Frequently Asked Questions
Why are my subtitles missing after using FFmpeg to fix a video?
This usually happens because FFmpeg did not explicitly map the subtitle streams to the output file. By default, FFmpeg may only copy video and audio streams if not instructed otherwise. Using the -map 0 flag ensures all streams, including subtitles, are included in the output.
Can I re-encode subtitles to fix them if they are corrupted?
Yes, but you cannot simply copy them if they are corrupted. You must specify a subtitle codec to transcode them, such as -c:s srt. However, this converts the subtitle format and may lose styling information, so copying is preferred whenever possible.
Does the -c copy command work for all subtitle formats?
No, it depends on the output container. For example, MP4 containers do not support ASS or SSA subtitle formats. If you try to copy these into an MP4, FFmpeg may drop them or fail. Use MKV or MOV containers for advanced subtitle formats.
How do I check if subtitles are in my video file?
You can run ffmpeg -i yourfile.mp4 and look for lines beginning with Stream that contain s for subtitle. If no such line appears, the file has no subtitle tracks or they were not detected.
Is it faster to copy subtitles or re-encode them?
Copying subtitles is significantly faster because it involves no processing. Re-encoding subtitles requires decoding the text, applying any transformations, and re-encoding it into the target format, which consumes CPU time and can introduce errors.
What if I want only one subtitle track from a file with many?
You can use the -map option to select specific streams. For example, -map 0:v -map 0:a -map 0:s:1 would copy the video, all audio tracks, and only the second subtitle track from the input file.