50% off from Dreamhost hosting plan (that's 59.50$ less for full hosting) - use promo code HOSTING_PROMO during account cration on Dreamhost

Nowalijka - Ĺšledzenie nowych aukcji allegro

For a while I used a commercial windows software to convert a bunch of MTS files, but as a Linux user (Windows is for games mostly :) ), I finally managed to find a way to do it using ffmpeg, a patch and mencoder.

I bought Panasonic SD-9 2 years ago and since then I was filming all my vacations, parties, etc. Usually when I film I end up with at least a 50 MTS files that are mostly unrelated, so I wanted to have some nice fade out and fade ins between the joined slices. Those fades are the only thing that was some kind hard to do in linux, but half a year ago I found a nice patch to ffmpeg that does exactly this. So here we go.

1. ffmpeg source code (if you use the latest trunk then you need libx264 version >= 0.99):
svn co svn://svn.ffmpeg.org/ffmpeg/trunk
2. patch file (mail list post, or patch authors site or the file hosted on my blog)
3. mencoder (it is used to count the total frames of the movie)
4. avimerge (to put all the converted files back together)

1. Go to the directory for ffmpeg
2. Apply patch (patch -p 0 < patch_file)
Application of patch may fail, as ffmpeg is changing, but all you really need is vf_fade.c file placed in libavfilter directory, and add line:
to libavfilter/Makefile, as well a line:
to libavfilter/allfilters.c

3. Build ffmpeg: ./configure with appropriate options, mine are:
--prefix=$HOME/ffmpeg --enable-libx264 --enable-libxvid --enable-libvorbis --enable-libtheora --enable-libmp3lame --enable-libfaac --enable-avfilter --enable-nonfree --enable-postproc --enable-runtime-cpudetect --enable-hardcoded-tables --enable-hwaccel=h263_vaapi --enable-hwaccel=h264_vaapi --enable-hwaccel=mpeg2_vaapi --enable-hwaccel=mpeg4_vaapi --enable-hwaccel=vc1_vaapi --enable-hwaccel=wmv3_vaapi --enable-filter=fade --enable-encoder=libx264 --enable-encoder=libxvid --enable-encoder=libmp3lame --enable-encoder=libfaac --enable-encoder=wmv2 --enable-encoder=wmv1 --enable-encoder=vorbis --enable-encoder=mpeg4 --enable-encoder=ac3 --enable-decoder=h264_vdpau --enable-gpl
And make, followed by make install.

Now you should have working ffmpeg with fade in/outs.

Here’s a make_avi.sh script that I’ve written for this:

for FILE_IN in $*; do
echo -n "$FILE_IN...."
#last_frame=`$FRAME_COUNTER $FILE_IN`
last_frame=` mencoder -nosound -ovc frameno -vc null -o /dev/null $FILE_IN 2>/dev/null | egrep " [0-9]+ frames\$" | gawk '{print \$12}'`
echo $last_frame
filters="-vfilters fade=in:0:$fade_dur,fade=out:$[last_frame/2 - $fade_dur-5]:$fade_dur"
${FFMPEG} -i $FILE_IN -deinterlace -vcodec libxvid -aspect 4:3 -r 25 -s $SIZE -b $BIT_RATE $filters -acodec libmp3lame -ac 2 -ab 128k -y -pass 1 -passlogfile temp.log -f avi /dev/null && \
${FFMPEG} -i $FILE_IN -deinterlace -vcodec libxvid -aspect 4:3 -r 25 -s $SIZE -b $BIT_RATE $filters -acodec libmp3lame -ac 2 -ab 128k -y -pass 2 -passlogfile temp.log -f avi ${FILE_IN/MTS/avi}
avimerge -o merge.avi -i $out_file_list

Just save it, change FFMPEG variable to your location of ffmpeg and run it passing a list of MTS files, it will create a merge.avi file in the current directory.
You can also tweak the “fade_dur” to make the fade longer/shorter.

I know it’s not much of a how to, but maybe someone will find it useful.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DZone
  • del.icio.us
  • Digg
  • Technorati

One Response to “Converting a set of mts files to a single avi file with fade in/out in Linux”  

  1. 1 ataraxic

    Very nice, thxs!

Leave a Reply