Transcoding AVCHD (.mts or .m2ts) files using mencoder on Linux

The current top-of-trunk version of mplayer will decode and playback .mts and .m2ts AVCHD files. The one caveat is that your computer has to be fast enough to decode them, which mine isn’t.

Therefore, transcoding the .mts files to a more easily playable format is preferred. Since mplayer can decode the orignials, then mencoder can transcode them directly. I’m still experimenting, but the following command produces a pretty nice output, and is significantly smaller than the original .mts file.

# mencoder $file -o ./$file.avi -oac copy -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=5000 -fps 60 -vf scale=1280:720

This will run at about 35FPS on my Athlon 4400+. I’m not specifying any fancy filters, like deinterlacing or denoising, and both of those could probably increase the quality of the output. Doing a 2-pass encode would also increase the quality of the output. Those commands would look like this:

# mencoder $file -o $file.avi -oac copy -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=5000:vpass=1:turbo -fps 60 -vf scale=1280:720
# mencoder $file -o $file.avi -oac copy -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=5000:vpass=2 -fps 60 -vf scale=1280:720

You’d probably only really need to do a 2-pass encode if you set the bitrate much lower than the 5Mbps that I’ve specified.

Now, I might actually think about buying that Canon HG10 instead of just borrowing it…

10 thoughts on “Transcoding AVCHD (.mts or .m2ts) files using mencoder on Linux”

  1. I’m trying to get my mts files to play in Ubuntu 7.10. I installed MPlayer using ‘Add/Remove Apps”, but it does not play these files. Now I’ll admit I have no idea what top-of-trunk means. Is there anything special I need to do to play mts files?

  2. It works well for me but at some point mplayer produces only garbled video and keeps showing me.

    [h264 @ 0x11e64b0]Missing reference picture 0min 21mb A-V:-0.014 [2870:384]
    [h264 @ 0x11e64b0]illegal short term buffer state detected
    [h264 @ 0x11e64b0]Missing reference picture 0min 21mb A-V:-0.014 [2870:384]
    [h264 @ 0x11e64b0]Missing reference picture

    Any idea?

  3. I also get this:
    [h264 @ 0x11e64b0]Missing reference picture 0min 21mb A-V:-0.014 [2870:384]
    [h264 @ 0x11e64b0]illegal short term buffer state detected
    [h264 @ 0x11e64b0]Missing reference picture 0min 21mb A-V:-0.014 [2870:384]
    [h264 @ 0x11e64b0]Missing reference picture

    my work around is add “-demuxer lavf”

    I am now using this command to convert m2ts file:
    mencoder $i -o ${i}.avi -vc ffh264 -demuxer lavf -oac mp3lame -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=3000 -fps 50 -vf scale=640:360

  4. This january I used the following software to convert AVCHD m2ts files from a Sony HD video camera with internal hard drive (the camera is not mine and I don’t remember the exact model):

    1. xport Transport Stream Demuxer, version 1.01 (2008-02-26):
    http://www.w6rz.net/xport.zip

    2. ldecod from jm15.0
    current version is newer – 15.1 (2009-01-29):
    http://iphome.hhi.de/suehring/tml/download/jm15.1.zip

    3. transcode-1.0.7

    4. SVN versions of ffmpeg or mplayer(mencoder):
    ffmpeg-20081230.r16400
    mplayer-20081230.r28215

    And this is the script using ffmpeg and pipe. Commented commands are experiments, so you can try those, too. I hope the HTML will not break the script text, because there is no preview of the comment:

    #!/bin/bash

    for xsrc in “$@”; do

    xdestavi=”${xsrc}.avi”
    xdestmpg=”${xsrc}.mpg”

    xac3=”${xsrc}.ac3″
    xwav=”${xsrc}.wav”
    xmp3=”${xsrc}.mp3″
    xyuv=”${xsrc}.yuv”

    uuidstring=`uuidgen`
    xyuvfifo=”/tmp/${xsrc}.${uuidstring}.fifo.yuv”

    xporthdmv -h “$xsrc” 1 1 1 || exit 1
    mv bits0001.mpa $xac3

    transcode -i “$xac3″ -y null,wav -o “$xwav”

    # lame “$xwav” “$xmp3″

    mkfifo “$xyuvfifo”

    #ldecod -i bits0001.mpv -o $xyuv
    ldecod -i bits0001.mpv -o “$xyuvfifo” &

    # mencoder “$xyuv” \
    # -demuxer rawvideo -rawvideo w=1440:h=1080 -aspect 16:9 -ofps 29.97 \
    # -audiofile “$xmp3″ -oac copy -ovc lavc \
    # -o “$xdestavi”

    # mencoder “$xyuv” \
    # -demuxer rawvideo -rawvideo w=1440:h=1080 -aspect 16/9 -ofps 29.97 \
    # -audiofile “$xmp3″ -oac copy -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:aspect
    # -o “$xdestavi”

    #SL: tozi e OK
    # mencoder “$xyuv” \
    # -demuxer rawvideo -rawvideo w=1440:h=1080 -aspect 16/9 -ofps 25 \
    # -audiofile “$xmp3″ -oac copy -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:aspect
    # -o “$xdestavi”

    # ffmpeg -r 29.97 -s 1440×1080 -vcodec mpeg4 -sameq -i “$xyuv” \
    # -acodec copy -i “$xmp3″ \
    # -b 15000k “$xdestavi”

    # ffmpeg -r 25 -s 1440×1080 -i “$xyuv” \
    # -i “$xmp3″ -acodec copy -vcodec mpeg4 -b 6000k -aspect 16:9 \
    # “$xdestavi”

    #SL: tva za DVD e OK
    #SL: 1. no fifo
    # ffmpeg -r 25 -s 1440×1080 -i “$xyuv” \
    # -i “$xwav” -target pal-dvd -b 6000k -aspect 16:9 \
    # “$xdestmpg”

    #SL: 2. fifo
    ffmpeg -r 25 -s 1440×1080 -i “$xyuvfifo” \
    -i “$xwav” -target pal-dvd -b 6000k -aspect 16:9 \
    “$xdestmpg”

    rm “$xyuvfifo”
    done

    ### script end

    p.s. There are two reasons not to use Sony’s original software for the purpose:
    * it runs only in Windows
    * the results were not goot if large area of water is in the movie, e.g. swimming pool – the source m2ts file is OK, but the converted one had many “still” frames

  5. P.P.S. Forgot to mention that with the above tools and script the Audio-Video sync is OK ;)

  6. Just wanted to thank you for your sample command. I had to tweak it a bit (I’m transcoding for a netbook) but other than nixing the FPS and setting the output resolution to something more netbook appropriate, it worked like a charm. Great sound, great picture, no artifacts. Love it! I’ll probably compile this into a quick bash script for handy’s sake.

Leave a Reply