November 21, 2010
MTS/M2TS to MKV (Especially for Sony HDR-SR12)
After spending many frustrating hours, I finally found a solution to easily re-mux m2ts/mts files produced by Sony HDR-SR12 or other similar camcoders in order to stream them smoothly over DLNA to Samsung C-Series (55C8000) TV in full HD quality. But the solution is generic and can be used for any rendered.
Problem:First the problem: Above mentioned Sony camcoder produces 1920×1080, 60i video. However, the time-stamps on the frames in each interlaced field are wrong, both frames of a filed having same time-stamp. This is very problematic for almost all rendering devices.
My first approach was to re-encode these mts files using Handbrake to convert to m4v. This worked though at a heavy computing cost. The conversion is very intensive and for 50GB of videos could easily take more than 12 hours on a core-2 processor. One day, I was browsing Ubuntu forums, I came across this thread. This actually produced the re-muxed file in a fraction of a second as opposed to re-encoding with Handbrake that took very long time.
But there are two problems in the above solution:
- It is using mencoder + lavf, a known broken combination in presence of B-frames.
- It does not allow to copy the subtitle track (PGS) that Sony camcoder produces. This subtitle track is very important as it contains the recording date/time
information which will be valuable when using these videos in future.
Solution:The following solution overcomes all the above-mentioned problems, is rock-stable and preserves the sub-title stream as well. I use ffmpeg to simply copy all the streams and output them in an mkv container. No information is lost/re-encoded. Here is the simple answer (as often observed in scientific research, the useful answer to a complex problem can often be stated very simply ):
ffmpeg -i input.mt2s -scodec copy -acodec copy -vcodec copy -f matroska input.mkv
I have written a script that converts all the m2ts files in a subfolder to mkv. The script is as below:
# m2tstomkv_subs (Fix the faulty fps (dual frames in the interlaced fields with same time-stamps) and repackage in matroska mkv container)
# remux m2ts movies into mkv with ffmpeg
for i in *.m2ts; do
ffmpeg -i $i -scodec copy -acodec copy -vcodec copy -f matroska `basename $i .m2ts`.mkv;
echo "Conversion done";
The conversion is lightening fast. Finally I am able to watch the videos in their true quality. I will be glad if this information helps someone facing similar issues.