Tuesday, January 5, 2010
7:37 AM

Transcoding 50 fps Interlaced AVCHD to 50 fps Progressive x264 with MEncoder - Getting Every Last Drop of Quality out of your Interlaced AVCHD Recordings

Have you ever wondered how much quality your HD camera can deliver? This script helps you to find it out if you have a camera that encodes interlaced video. It uses special filters to calculate 50 whole frames out of the half frames, which makes motions seem extremely fluent. You could play the file at half speed slow motion and still have a full 25 frames per second.

You need a more or less recent version of mplayer. It should be a version of mplayer after the release of ffmpeg 0.5. Current repositories of current distributions should be able to deliver this. But be aware that not all of them provide an mencoder compiled to work properly with x264. If that's the case you may have to compile it yourself.

After that it's as "easy" as this command, once you've put it all together. I'll explain further below and post a sample script for the entire conversion. The result is extremely smoothly playing video files that look great.

This is the main part:

mencoder -demuxer lavf -sws 9 -fps 100 -vf yadif=1,mcdeint,softskip -ofps 50

The demuxer parameter works around mplayer's transport stream implementation, which does not work well with AVCHD files. It tells mplayer to trust ffmpeg's libavformat instead. This makes a huge difference for me. With this command I can play AVCHD files and transcode them in sync without problems and serious glitches.

The fps and yadif part was tricky. It creates 50 full progressive frames out of the 25 interlaced frames it gets from the camera. I had sync problems at first, but I found the solution somewhere in the mplayer mailing list I think. I'm not sure why you have to take twice the framerate of the AVCHD file, but only this way it works. Of course, if you camera is not PAL, you may have to change this to double your framerate. Otherwise I had the sound playing half as fast as the video with the consequence of constant frame skips and no sync at all.

Now let's put it all together into a little please-do-it-all-for-me script:


#!/bin/sh
# High Quality Mencoder AVCHD Transcoding Script, (c) 2010 linux-tipps.blogspot.com, published under the GNU GPL v. 3.0

# Settings
CRF=26 # 18-26, the higher the smaller the resulting file, HD files can stand a higher than SD
FPS=50 # input fps of your file, "mplayer -demuxer lavf -identify" helps you with finding this
X264OPTS=""  # add subq=6 frameref=6 for higher quality per bit but lower encoding rate.

# Preparation
INPUT="$1"; shift
OUT=$(basename "$INPUT"|sed 's/.m2ts//')"-x264.avi"
echo Mencoder Interlaced AVCHD to Progressive x264 Script, published under the GPL v. 3.0 \(http://www.gnu.org/licenses/gpl-3.0.txt\), check for the newest version of this script at http://linux-tipps.blogspot.com/2010/01/transcoding-50-fps-interlaced-avchd-to.html.
echo $0 Encoding $INPUT to $OUT using the following command line:
[ -f "$OUT" ] && echo File exists - aborting && exit 1;

# Encoding
mencoder="time nice mencoder -cache 8096 -demuxer lavf -sws 9 -fps $((FPS*2)) -vf yadif=1,mcdeint,softskip -ofps $FPS"
enc="$mencoder -ovc x264 -oac copy -x264encopts crf=$CRF:trellis=1:threads=0:ratetol=inf:frameref=2:bframes=2:8x8dct:ssim:psnr:$X264OPTS"

echo $enc -o "$OUT" "$INPUT" $*
$enc -o "$OUT" "$INPUT" $*

Just run the script and give a .m2ts file as parameter. E.g.
sh avchd-mencode 000001.m2ts # or for all m2ts file in the current directory:
for i in ./*.m2ts; do sh avchd-mencode "$i"; done;

Let me know how it worked for you! Or if you have an idea how to improve the script. Try skipping frame for frame with the "." button in mplayer. Be aware that the file size of the resulting video may easily be bigger than the input. That's because you're creating full frames out of half ones, and storing them takes more space.

Check out a Squirrel in HD slow motion produced with this script and then "mencoder -speed 0.5 -af scaletempo -oac lavc -lavcopts acodec=ac3 -ovc copy" and uploaded in FullHD (but available only in 720p currently):

0 comments:

Post a Comment