Understanding Codec Tag Alteration
When working with M2TS files, particularly those extracted from Blu-ray discs, you may notice that FFmpeg changes the internal metadata describing the audio and video streams. The most common culprit is the handling of uncompressed Pulse Code Modulation (PCM) audio. While many players rely on standard tags like pcm_s16le, some Blu-ray sources use specific descriptors that FFmpeg might normalize to a more generic format during processing.
This alteration is not always an error but rather a side effect of FFmpeg's strict adherence to standard container specifications. When you remux a file without re-encoding, FFmpeg attempts to map the source stream to the closest compatible codec in the output container. If the source uses a non-standard or proprietary tag for PCM audio, FFmpeg may replace it with a standard WAV-like tag. This can cause compatibility issues with older hardware players that expect the original Blu-ray specific tags.
It is important to distinguish between actual data corruption and mere metadata changes. In most cases, the underlying audio or video data remains intact. However, if the player fails to recognize the new tag, playback may fail or switch to a fallback decoder, which can result in stuttering or silence. For detailed diagnostics on stream composition, you can refer to our guide on How to detect with ffmpeg if the stream has video or audio or both to verify what streams are actually present in your file.
Identifying the Problematic Streams
Before attempting a fix, you must identify exactly which stream is being altered. Use the following command to inspect the input file:
ffprobe -v error -show_entries stream=codec_name,codec_tag_string,channels,sample_rate -of json input.m2ts
Look for the audio stream entry. If the codec_tag_string shows something unusual or differs from the expected standard, this confirms the issue. Common tags for Blu-ray PCM include LPCM or specific vendor codes. If FFmpeg outputs a different tag in the log, it indicates where the mismatch occurs.
Solutions to Preserve Codec Tags
To prevent FFmpeg from altering these tags, you need to force the output codec to match the input exactly. This is achieved using the -c:a and -c:v flags with specific codec names that preserve the original encoding.
For Audio Streams
If you are dealing with PCM audio, avoid using generic encoders. Instead, specify the exact codec name used in the source. For example, if the source is 16-bit little-endian PCM, use:
ffmpeg -i input.m2ts -c:a pcm_s16le -c:v copy output.m2ts
If the source uses a different PCM variant, such as big-endian, adjust the flag accordingly (pcm_s24be). It is crucial to ensure that the target container supports the specific bit depth and endianness of the source audio.
For Video Streams
Video codec tags are less frequently the source of this specific issue but can still be affected. To preserve the video stream without re-encoding, use the copy option:
ffmpeg -i input.m2ts -c:v copy -c:a copy output.m2ts
This method copies all streams verbatim, including their tags. However, if you must re-encode the video for size reduction, you will inevitably change the codec tag to the new encoder's identifier (e.g., avc1 for H.264). In such cases, test the output on your target device to ensure compatibility.
Handling Complex Multi-Stream Files
M2TS files often contain multiple audio tracks, subtitles, and chapters. When combining media, it is easy to inadvertently drop or alter streams. If you are adding delayed audio or merging streams, ensure that each stream is mapped individually to maintain control over its codec settings. For more information on managing complex stream arrangements, see Combining Video and Audio with FFmpeg.
Use the -map option to explicitly select streams. This prevents FFmpeg from making automatic decisions about which streams to include and how to encode them. For example:
ffmpeg -i input.m2ts -map 0:0 -map 0:1 -c:v copy -c:a pcm_s16le output.m2ts
This command maps the first video stream and the first audio stream, ensuring that only the specified audio stream is processed with the PCM encoder while the video is copied directly.
Verifying the Output
After running the conversion, verify that the codec tags have been preserved correctly. Run the same ffprobe command on the output file and compare the results. If the tags match your expectations, the issue is resolved. If they still differ, check the FFmpeg documentation for your specific version, as codec handling can vary between releases.
Frequently Asked Questions
Why does my Blu-ray player not recognize the new audio track?
Many consumer Blu-ray players are strict about codec tags and may reject streams that do not match the expected specification. Even if the audio data is identical, a different tag string can cause the player to skip the track or display an error. Always use the exact codec name from the source file when remuxing.
Can I convert PCM to AC3 without changing the tag incorrectly?
Yes, but you must explicitly specify the AC3 encoder. Use -c:a ac3 to encode the audio into Dolby Digital. This will change the codec tag to reflect AC3, which is generally widely supported. Ensure the bitrate is set appropriately using -b:a to maintain quality.
Does FFmpeg always alter tags when re-encoding video?
Yes, re-encoding changes the codec identifier to the one used by the new encoder. For example, converting from MPEG-2 to H.264 will change the video codec tag from mp2v to avc1. This is normal and usually does not affect playback unless the target device has very limited codec support.
How do I handle delayed audio streams in M2TS files?
If your audio stream has a delay, you can correct it using the adelay filter or by adjusting the timestamps. For complex scenarios involving multiple delayed tracks, refer to our guide on Adding Multiple Delayed Audio Streams to a Single Video Using FFmpeg for detailed filtering techniques.
Is it safe to use -c copy for all streams?
Using -c copy is safe and preserves all original metadata and codec tags. However, it increases file size significantly because no compression is applied. Use this method only when you need to modify container-level metadata or merge streams without altering the core media data.