Oct 11

converting mts (avchd) to flv with ffmpeg

Sun, 10/11/2009 - 20:03 — peter

The Situation

With the baby duedate approaching, it was time to join the 21st century and upgrade my camcorder. My old miniDV-based Canon ZR200 just made the video creation/editing process too cumbersome and it didn't record in HD. After a bit of shopping I found a Canon HF200 at Nebraska Furniture Mart with 18 months, no interest! While closing the order, the sales guy asked me if I was gonna use the camera with PC (by which I am sure he meant Windows) or Mac. I answered "Linux!" :)

The Problem

Like most new HD cameras, this one records videos in the somewhat recent and common AVCHD format. It also comes with a bunch of Windows and Mac software that are not useful to me. As with any new toy, we want fast results, right? Although you can actually play that straight with VLC, what I wanted to do was get a quick FLV video on my photo album site that is based
on Gallery2. Since AVCHD is actually H.264 video with AC3 audio, I figured ffmpeg should be able to do the trick.

The Solution

Looking online really didn't help much here. FLV is a bit tricky on the kind of audio it accepts and WinFF was creating videos without sound. I found a solution online that required several passes: splitting the video and audio channels into 2 files with 1 utility, then use another to convert the audio, then use ffmpeg to combine it all into an AVI wrapper. That simply didn't do for me. I wanted somethign simple! So, armed with the documentation from ffmpeg site I went digging into the command line...

After a bit of tinkering, I found the options I needed to do the 1-step convertion from MTS (AVCHD) into FLV. The camera hooks up through USB to the computer and its memory card shows up as a mountable device in Linux. The video files are located in the "[camera root]/PRIVATE/AVCHD/BDMV/STREAM/99999.MTS" folder. The particular movie file I was looking for was 00002.MTS.

In my case, I copied the file to my ~/Videos folder and used following command for the convertion to FLV:

/usr/bin/ffmpeg -i "~/Videos/00002.MTS" -vcodec flv -f flv -r 25 -s 800x450 -aspect 16:9 \
                       -b 2000k -g 160 -cmp 2 -subcmp 2 -mbd 2 -flags +aic+cbp+mv0+mv4  \
                       -trellis 2 -acodec libmp3lame -ac 2 -ar 44100 -ab 256k \
                       "~/Videos/00002.flv"

That was it! After the conversion I uploaded the video to my gallery site and it showed up perfecly online! Next is coming up with a way to automate the video import process...

Easy Peasy!

-PCP