Friday, 7 August 2009

Transcoding AVCHD into DVD and Web Formats using the Command Line under Linux

I needed a way of transcoding output from my Panasonic HDC-HS300 into a DVD and web viewable format under linux. There are webpages out there that tell you how to do it, but I had to edit some of the scripts to perform this task automatically from the command line.

I've created two scripts, and which can be downloaded in the archive convert-mts.tgz. The scripts unpack into a bin directory.

In order to use the scripts you need to ensure you have the latest version of mplayer installed. My version reports:

MPlayer SVN-r29328-4.3 (C) 2000-2009 MPlayer Team

so I would suggest this version or later. The packaged version in the OpenSUSE 11.0 repository is not recent enough - I downloaded the latest source code using subversion and built it locally. The following commands download the latest version, configure and build it to /usr/local. The mplayer binary can then be found under /usr/local/bin so the last command ensures that you pick that version up in preference to a pre-packaged version that might be installed under /usr/bin for example.

svn checkout svn:// mplayer
cd mplayer
svn update
./configure --prefix=/usr/local/
make install
export PATH=/usr/local/bin:$PATH
The script is used to convert your HD video into DVD quality, suitable for directly burning to a DVD (for example using my command line tools described in a previous blog post). It contains the following commands:
rm -rf dvd/*
mkdir dvd
for f in *.mts
echo $f
mencoder -oac copy -ovc lavc -of mpeg -mpegopts format=dvd -vf scale=720:576,hh
arddup -lavcopts vcodec=mpeg2video:vrc_buf_size=1835:vrc_maxrate=9800:vbitrate=55
000:keyint=15:aspect=16/9:threads=4 $f -ofps 25 -fps 50 -o dvd/`basename $f .mts
`.mpg -demuxer lavf >>`basename $f .mts`.log 2>&1 &
Once you have this command on your path, cd to a directory containing the files you wish to convert (which have the extension .mts) and run the command. Note that with the Panasonic Camcorder you can copy the files directly off the camera - you don't need to use the Windows utility provided. It will create a sub-directory called dvd and proceed to convert all the .mts files into DVD format. It creates a log file for each of the converted files giving any output or error messages.

Note the ampersand on the end and the wait command - this script is multi-threaded and will utilise all cores on a mult-core processor. I did some experimentation with versions of this script and found that on a machine with a decent amount of memory the most efficient way of transcoding the videos was to spawn off all the transcode commands at the same time, even when transcoding several tens of mpeg. On my quad-core machine this command completes the process almost exactly four times quicker than processing the command sequentially.

The only disadvantage of spawning all the transcodes at once is that you might impact other processes on the box (I noticed that video streaming would be interrupted for example). You could always use the nice command infront of the call to mencoder to run it at a lower priority, giving preference to existing tasks.

The provides the appropriate command line arguments for generating a video size 360x288 - it creates a directory called web and places the transcoded video files there.

rm -rf web/*
mkdir web
for f in *.mts
echo $f
mencoder -oac copy -ovc lavc -of mpeg -mpegopts format=dvd -vf scale=360:288,harddup -lavcopts vcodec=mpeg2video:vrc_buf_size=1835:vrc_maxrate=1250:vbitrate=625:keyint=15:aspect=16/9:threads=4 $f -ofps 25 -fps 50 -o web/`basename $f .mts`.mpg -demuxer lavf >>`basename $f .mts`.log 2>&1 &