[MEncoder-users] Successful ATSC encodes

Matyas Sustik mplayer.list at sustik.com
Sun Jan 18 18:37:11 CET 2009


A reader of the mplayer, mencoder lists asked me about encoding ATSC
recordings (since I reported that I could use ccextractor to play closed
captions.)

I include the technical part of my response.  I hope this may help others.
If experts find errors or possible improvements I welcome the comments.

I am no expert in mplayer or mencoder use or scripting.  Use this information
at your own risk. :-)

I used the output of h264enc as a starting point and of course many comments
on the mailing lists and docs to produce the process described.

So once you get your card capture the video stream it will save it as an mpg
file.  MythTV does that on my system.  Then I use the following steps to
produce the encoding:

1.  I use mencoder to cut the video.  Actually, I do this in two steps,
because in my experience the OTA broadcasts have messed up time markings so
the -ss option does not work right away.  (The mpg file reports incorrect
movie length too.)  So first I do:

mencoder /scratch/mythtv/1181_20081109190000.mpg -oac copy -ovc copy
-channels 6 -aid 52 -o MONKEYS.avi

Note that I picked the audio id and used -channels 6, because I wanted the
surround sound to be encoded.  Many broadcasts have more than one audio
streams, for example for the above -aid 53 picked a 2 channel (stereo) one.
Even though I  do not have a surround system at the moment I am getting ready
for the time when I get one.  You may use -af
pan=2:1:.2:.2:1:.4:-.4:-.4:.4:1:1:1:1 to play back the six channel audio in
stereo mode.  In order to find the right audio id and other info about the
video I use the mplayer options: -identify -frames 0.

Next I determine the cut points.  (I encode shows without commercials aired
on the public channel, so I only cut off the beginning and the end.)

mencoder -oac copy -ovc copy -channels 6 -ss 5 -endpos 3200 MONKEYS.avi -o
MONKEYS.cut.avi

(The MONKEYS.avi file was not needed after this and was removed.)

2. Find the crop values.  I use the mplayer option -vf cropdetect *and*
visual inspection.  The latter is important, because sometimes the black bars
are not completely black or some parts of the movie has embedded parts with
extra  black bars (typical for documentaries) or there is a logo on the bars.

3. I run a 2 pass mencoder encode.  This takes a long time.  I copy the audio
 with -oac pcm, because I want it to recompress with ogg vorbis.  If you
prefer the original AC-3 you will have to use -oac copy.

4. I extract the audio to a wav file and reencode it with oggenc.

5. I extract the closed captions with ccextractor avialable from sourceforge.

6. I mux all the files into a matroska container.  I include a message file
documenting the encoding parms, times, psnr, ssim, errors as an attachement
in the mkv file.

The script below does steps 3-6:

#!/bin/sh

#SRC=/scratch/mythtv/1181_20081109190000.mpg
# We want to cut the video first.  The time stamps are bogus on the OTA
# recording so we copy it first with mencoder:
# mencoder /scratch/mythtv/1181_20081109190000.mpg -oac copy -ovc copy
-channels 6 -aid 52 -o MONKEYS.avi
# Note that we used an audio id above.  The default -aid 53 also
# included an extra narration.
# We identified the cuts and then:
#mencoder -oac copy -ovc copy -channels 6 -ss 5 -endpos 3200 MONKEYS.avi -o
MONKEYS.cut.avi
SRC=MONKEYS.cut.avi
WORK=/scratch/encode/MONKEYS
NAME=MONKEYS
# You can encode a sample:
#SAMPLE="-endpos 30"
FILTER=crop=1920:1072:0:0,yadif,softskip,scale=960:536:0:0:7,harddup
CHANNELS=6
# Use -af pan=2:1:.2:.2:1:.4:-.4:-.4:.4:1:1:1:1 to play back in stereo.
BITS=3000
# For different runs you may set TAG to mark them:
TAG=
FILE=$NAME$TAG
MSG=$WORK/encode$TAG.log
NICE=10

echo "Running $0:" >> $MSG
echo "###########" >> $MSG
cat $0 >> $MSG
echo "###########" >> $MSG
echo $(date) >> $MSG

COMMON_OPTS=bitrate=$BITS:nr=250:subq=6:8x8dct:frameref=6:bframes=14:b_adapt=1:weight_b:b_pyramid:nointerlaced:nofast_pskip:trellis=1:mixed_refs:weight_b:me=umh:me_range=24:nodct_decimate:partitions=all:psy-rd=0.5,0.3:qcomp=0.7:direct_pred=auto:keyint=300:threads=auto:psnr:ssim:log=2
X264OPTS_PASS1=pass=1:$COMMON_OPTS:turbo=2
X264OPTS_PASS2=pass=2:$COMMON_OPTS

echo "First pass started on $(date)." >> $MSG
TIC=$SECONDS
nice -n $NICE \
time mencoder $SRC -cache 8192 $SAMPLE \
-o /dev/null -vf $FILTER -sws 9 -mc 0 \
-ovc x264 -x264encopts $X264OPTS_PASS1 \
-af volnorm=2 -oac pcm -channels $CHANNELS \
-passlogfile $WORK/passlog$TAG \
2>> $MSG
TOC=$SECONDS
let ET=$TOC-$TIC
echo "First pass done in $ET seconds." >> $MSG

echo "Second pass started on $(date)." >> $MSG
TIC=$SECONDS
nice -n $NICE \
time mencoder $SRC -cache 8192 $SAMPLE \
-o $WORK/$FILE.avi \
-vf $FILTER -sws 9 -mc 0 \
-ovc x264 -x264encopts $X264OPTS_PASS2 \
-af volnorm=2 -oac pcm -channels $CHANNELS \
-passlogfile $WORK/passlog$TAG \
2>> $MSG
TOC=$SECONDS
let ET=$TOC-$TIC
echo "Second pass done in $ET seconds." >> $MSG

TIC=$SECONDS
mkfifo audio.wav
nice -n $NICE oggenc -q 6 audio.wav -o audio.ogg &
nice -n $NICE mplayer $WORK/$FILE.avi -vc null -vo null -nocache -channels
$CHANNELS -ao pcm:fast:file=audio.wav >/dev/null 2>&1
TOC=$SECONDS
let ET=$TOC-$TIC
echo "Extracted $CHANNELS audio channels in $ET seconds." >> $MSG

TIC=$SECONDS
~/src/ccextractor.0.52/linux/ccextractor $SRC -o subtitles.srt
TOC=$SECONDS
let ET=$TOC-$TIC
echo "Extracted closed captions in $ET seconds." >> $MSG

echo "Encoding finished on $(date)." >> $MSG

mkvmerge --noaudio $WORK/$FILE.avi audio.ogg subtitles.srt
--attachment-description "Message file." --attachment-mime-type plain
--attach-file $MSG -o $WORK/$FILE.mkv

echo "Created $WORK/$FILE.mkv."
# Check the log file and the results before removing any intermediary files!

Matyas
-
Every hardware eventually breaks.  Every software eventually works.



More information about the MEncoder-users mailing list