Comfortable Direct HD Video Encoding from AVCHD to Mpeg4

This nice little script encodes avchd m2ts files from e.g. an HD camcorder directly to mpeg4 avis. It uses a temporary file in the /tmp dir for the audio stream, but the rest is done using fifo pipes. You need to install the csh shell interpreter to run it. And you need to setup the environment from this package: (by invoking ./install, then ./installasroot, you need to modify the download script and change the JM version).

The script works badly on multi-core systems, but you can invoke it several times at once. It's originally a modified version of the script from this package, but I changed it so you can run it several times concurrently (with the same file names in different folders). I also added a check to prevent it from encoding files where the output files already exist. I recommend to run it on an entire directory for i in *.m2ts; do echo encoding $i; hdencode $i; done


#!/bin/csh

set BR=5000
set lavcopts="-lavcopts vcodec=mpeg4:vbitrate="$BR":turbo:threads=1:autoaspect:ilme:ildct"
set lavf="-of lavf -lavfopts format=mp4"
set menco="-vf scale=1280:540:1 $lavf"
set fps="25" # "29.97"
set ending="mp4"
set delay="-delay +0.7"

nice
#The scripts and instructions in this package are free to use and
#redistribute AT YOUR OWN RISK!! Standard disclaimers apply.
#NO WARRANTY!

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

set path = ( . $path )

echo using:
which xporthdmv || exit
which ldecod || exit
which ffmpeg || exit

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

rm -f ./bits0001.mpv ./bits0001.mpa

set filebase=`basename $file | sed s/\.m2ts// | sed s/\.MTS//`
set audiofile=/tmp/$filebase-$$".ac3"
set videofifo=/tmp/$filebase-$$".yuv"
set outputfile=$filebase"."$ending

if ( -f $outputfile ) then
echo file $outputfile already exists
exit
endif

if ( ! -f $audiofile ) then
echo xporthdmv -h $file 1 1 1
xporthdmv -h $file 1 1 1 && mv bits0001.mpa $audiofile
else
echo $audiofile already exists, not creating it.
endif

mkfifo $videofifo

if ( ! -f $videofifo ) then
echo ldecod -i bits0001.mpv -o $videofifo
ldecod -i bits0001.mpv -o $videofifo &
#ldecod -i bits0001.mpv -o $videofifo
else
echo $videofifo already exists, not creating it.
endif

if ( ! -f $outputfile ) then
echo mencoder $videofifo -demuxer rawvideo -rawvideo w=1440:h=1080 -aspect 16:9 -ofps $fps \
-ovc lavc -o $outputfile"-tmp" $lavcopts $menco

mencoder $videofifo -demuxer rawvideo -rawvideo w=1440:h=1080 -aspect 16:9 -ofps $fps \
-ovc lavc -o $outputfile"-tmp" $lavcopts $menco

echo mencoder $outputfile"-tmp" -aspect 16:9 -ofps $fps \
-audiofile $audiofile \
-oac copy -ovc copy \
-o $outputfile

mencoder $outputfile"-tmp" -aspect 16:9 -ofps $fps \
-audiofile $audiofile $delay \
-oac copy -ovc copy \
-o $outputfile
else
echo $outputfile exists, not creating it.
endif
end

#echo To remove temporary files: rm -f /tmp/*.ac3 /tmp/*.yuv
rm -f ./dataDec.txt ./log.dec bits0001.mpv $videofifo $audiofile $outputfile"-tmp"
echo $0 complete.

3 comments:

  1. the script looks good and im wondering why it is not commented yet. the movie output is great, but the sound wont work :/

    ReplyDelete
  2. Nice script, but what are you referring to by, "

    And you need to setup the environment from this package: (by invoking ./install, then ./installasroot, you need to modify the download script and change the JM version).

    " ?

    There are no links to any package in your article that I can see. Where does one get xporthdmv or ldecod?

    ReplyDelete
  3. Hey,
    here you go:
    xporthdmv http://www.w6rz.net/xport.zip
    JM http://iphome.hhi.de/suehring/tml/download

    but actually the script is a bit outdated, as you can probably use ffmpeg-0.5 for encoding. I shall post a new script... check the blog within the next days.

    ReplyDelete

I appreciate comments. Feel free to write anything you wish. Selected comments and questions will be published.