Back to the code page

Compress AVCHD (MTS/M2TS) files into Quicktime movies (MOV) with ffmpeg


One thing to know about HD movie camera, is to never modify, erase or add files directly on the hard-drive of the camera instead of using the display menu. The movie camera usually maintains a file database, and if it does not match the content of the hard drive, video edition software (such as Apple iMovie or Final Cut) will not be able to retrieve the video from the camera. Neither they can take an HD video directly as an input.

The only remaining solution is to compress all of the HD video into Quicktime format (e.g. for iMovie of Final Cut) or AVI format. Some non-free software can do that, but they are either expensive, or the time or disk space it consumes is way too big.

Fortunately, ffmpeg can do that very easily as long as you use the correct set of codecs and parameters. Here is the procedure to convert such files on a Debian GNU/Linux computer:

  • Add the following entry in your /etc/apt/sources.list file:
    deb http://www.debian-multimedia.org lenny main
  • Install the needed packages
    # apt-get update
    # apt-get install subversion csh
    # apt-get install faac libfaac0 libfaac-dev libxvidcore4 libxvidcore4-dev
    $ svn checkout svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpeg
  • Compile and install it with the support of the codecs we will use:
    $ cd ffmpeg
    $ ./configure --enable-gpl --disable-debug --enable-libfaac --enable-libxvid
    $ make
    # make install
  • The below script will take care of compressing your AVCHD files into MOV ones. Copy/paste it into a new file (named e.g. m2tstomov), and modify it in order to adjust the ffmpeg parameters according to your needs in terms of bitrate, size, etc.:
    #!/bin/csh
    #
    # Convert AVCHD (MTS/M2TS) files into Quicktime movies (MOV)
    # 2009/03/13 - Romain KUNTZ 
    #
    # Mostly inspired by the tutorial and examples from:
    # http://wesleybailey.com/articles/m2tstoavi-avchd
    #

    if ( $#argv == "0" ) then
        echo usage: $0 filename.m2ts ...
        exit
    else
        set files=($*)
    endif

    set path = ( . $path )

    echo using:
    which ffmpeg || exit

    foreach file ($files)
        if ( ! -f $file ) then
            echo file $file not found
            exit
        endif

        set filebase=`basename $file | sed s/\.m2ts// | sed s/\.MTS//`
        set passfile="passfile.mov"
        set outputfile=$filebase".mov"

        # 2-pass encoding
        # You can modify the -s and -b parameter to 
        # increase/decrease the quality of the video. 
        # See the ffmpeg manpage
        if ( ! -f $outputfile ) then
            # Pass 1
            ffmpeg -i $file -pass 1 \
                      -acodec libfaac -ab 128k -ac 2 -ar 44100 \
                      -vcodec mpeg4 -aspect 16:9 \
                      -s hd720 -b 10000k \
                      -copyts $passfile
            # Pass 2
            ffmpeg -i $passfile -pass 2 \
                      -acodec libfaac -ab 128k -ac 2 -ar 44100 \
                      -vcodec mpeg4 -aspect 16:9 \
                      -s hd720 -b 10000k \
                      -copyts $outputfile 
            # Remove junk files
            rm $passfile ffmpeg2pass-0.log
        else
            echo $outputfile exists, not creating it.
        endif
    end
  • Make the script executable:
   $ chmod +x m2tstomov
  • You can now compress your videos with:
   $ ./m2tstomov my_video.m2ts

The my_video.m2ts video will be converted and compressed into a my_video.mov file that can be used directly as an input to your video edition software.