tetra |
12-06-2003 10:26 AM |
Mencoder/ffmpeg aspect-ratio
I modified russiansexpat's script (borrowing something from paranouei's script too) to fix the aspect-ratio problem automatically. I fixed it like this:
Code:
scaleto=TARGET_HEIGHT-(480-MOVIE_HEIGHT)
for example a PAL SVCD where target resolution is 480x576. I encode a movie with original resolution of 640x360
Code:
scaleto=576-(480-360)
This seems to work, checked with Universal Studio's movie 2Slow2Calm, where the earthglobe comes, which is round with this script, with original russiansexpat's script the earthglobe looks like an egg.
Also, with this script you can encode subtitles. Might be good that you check the subpos variable after encoding a short sampleclip.
[EDIT]
Forgot to mention, that this script cannot handle subtitles, if encoded directly from a DVD.
[/EDIT]
Here's the script.
Code:
#!/bin/bash
#
# Copyright (C) 2003-2038 russiansexpat
# Portions taken from paranouei's scripts
#
# Modified by tetra
# script: mpeg(1,2,3,4) conversion to (S,X)VCD with quantisation matrix
# and auto-splitting into multiple CDs and writing them.
#
# requires: mencoder mplayer ffmpeg tcmplex vcdimager cdrdao
#!!!!!!!!!!!!!! below you can edit parameters !!!!!!!!!!!!!!!!!!!!!!
# Storage medium size
#---------------------------------------------------------------
#CDSIZE=735 # 74min CD
CDSIZE=795 # 80min CD
#CDSIZE=895 # 90min CD
AUDIORATE=128
#---------------------------------------------------------------
# Subtitle settings
#---------------------------------------------------------------
# These settings look good with arialbd.ttf font
blur=4
font="/usr/X11R6/lib/X11/fonts/TTF/arialbd.ttf"
subpos=67
textscale=4
outline=4
autoscale=2
#---------------------------------------------------------------
# FPS settings
#---------------------------------------------------------------
#FPS=",telecine,lavcdeint -fps 29.97 -ofps 29.97"
#FPS=" -ofps 29.97" # NTSC
FPS=" -ofps 25" # PAL
#FPS=" -ofps 23.976" # NTSC pulldowned
#FPS=",ivtc=1 -ofps 23.976"
#---------------------------------------------------------------
# Optional filters
#---------------------------------------------------------------
VFILTER="hqdn3d,"
#LAVCOPTS=":vlelim=-4:vcelim=7:lumi_mask=0.05:dark_mask=0.01"
LAVCOPTS=""
#---------------------------------------------------------------
# Pass
#---------------------------------------------------------------
PASS="single"
#PASS="1st 2nd"
#---------------------------------------------------------------
# Verbosity
#---------------------------------------------------------------
NOTverbose="2> /dev/null" # I want to see what is going on
#NOTverbose="&> /dev/null" # I don't care what's going on
#---------------------------------------------------------------
# Target type settings
#---------------------------------------------------------------
# for VideoCD (VCD)
#WID=352
#HEI=240
#HEI=288
#VCODEC=mpeg1video
#MUXMODE="-m 1 -F tcmplex.tmp"
#IMAMODE="-t vcd2"
# for SuperVideoCD (SVCD)
WID=480
#HEI=480 # NTSC??
HEI=576 # This is for PAL
VCODEC=mpeg2video
MUXMODE="-m 2 -F tcmplex.tmp"
IMAMODE="-t svcd"
# CD-burner device ID (for a list of your devices, type 'cdrdao scanbus')
CDDEV="--device 0,1,0"
# Movie length in seconds
DEFAULT_MOVIE_SECONDS=6800
# !!!!!!!!!!!!!!!!!!!!! above you can edit parameters !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
echo "maxFileSize = $CDSIZE" > tcmplex.tmp
CDOPT="--force --reload --eject --speed 8"
# Find out if we have a subtitle-file to be hard encoded
FILE=$2
tmp=`echo $FILE | sed s/\.avi//`
SUB=""
[ -f "${tmp}".sub ] && SUB="-sub ${tmp}.sub"
[ -f "${tmp}".srt ] && SUB="-sub ${tmp}.srt"
# Get some necessary information
# Taken from paranouei's scripts
tcprobe -i "${FILE}" > info
if [ $? -eq 0 ]; then
ORIGFPS=`grep 'frame rate' info | \
perl -e ' $line=<STDIN> ; $line =~ /frame rate: -f (.+?) \[/ ; print $1' `
ORIGFPS_1=`echo "scale=1 ; $ORIGFPS/1"| bc -l`
WIDTH=`grep 'import frame size' info | \
perl -e ' $line=<STDIN> ; $line =~ /import frame size: -g (\d+?)x\d+ / ; print $1' `
HEIGHT=`grep 'import frame size' info | \
perl -e ' $line=<STDIN> ; $line =~ /import frame size: -g \d+?x(\d+) / ; print $1' `
FRAMES=`grep '\[avilib\] V:' info | \
perl -e ' $line=<STDIN> ; $line =~ /frames=(\d+)/ ; print $1' `
else
mplayer -ao null -vo null -frames 10 "$FILE" > info
WIDTH=`grep 'VIDEO' info | \
perl -e ' $line=<STDIN> ; $line =~ / (\d+)x\d+ / ; print $1' `
HEIGHT=`grep 'VIDEO' info | \
perl -e ' $line=<STDIN> ; $line =~ / \d+x(\d+) / ; print $1' `
ORIGFPS=`grep 'VIDEO' info | \
perl -e ' $line=<STDIN> ; $line =~ /\s(\S+)\sfps/ ; print $1' `
ORIGFPS=`echo "$ORIGFPS"| tr , .`
ORIGFPS_1=`echo "scale=1 ; $ORIGFPS/1"| bc -l`
fi
SEGUNDOS_TOTAL=`echo "scale=0 ; ($FRAMES / $ORIGFPS)"| bc -l`
#If couldn't get the length, use the default
[ "$SEGUNDOS_TOTAL" == "" ] && SEGUNDOS_TOTAL=$DEFAULT_MOVIE_SECONDS
HORAS=`echo "scale=0 ; ($SEGUNDOS_TOTAL / 3600)"| bc -l`
MINUTOS=`echo "scale=0 ; (($SEGUNDOS_TOTAL - \
3600 * $HORAS)/60)"| bc -l`
SEGUNDOS=`echo "scale=0 ; ($SEGUNDOS_TOTAL % 60)"| bc -l`
# Calculate variable videobitrate
VIDEO_RATE=`echo "scale=0 ;(($CDSIZE * 1024 - \
($AUDIORATE/8*$SEGUNDOS_TOTAL))*8 / $SEGUNDOS_TOTAL)"| bc -l`
MAXRATE=$VIDEO_RATE
[ "$MAXRATE" == "" ] && MAXRATE=1150
[ $MAXRATE -gt 2500 ] && MAXRATE=2500
MATRIX="\
intra_matrix=\
8,9,12,22,26,27,29,34,\
9,10,14,26,27,29,34,37,\
12,14,18,27,29,34,37,38,\
22,26,27,31,36,37,38,40,\
26,27,29,36,39,38,40,48,\
27,29,34,37,38,40,48,58,\
29,34,37,38,40,48,58,69,\
34,37,38,40,48,58,69,79:\
inter_matrix=\
16,18,20,22,24,26,28,30,\
18,20,22,24,26,28,30,32,\
20,22,24,26,28,30,32,34,\
22,24,26,30,32,32,34,36,\
24,26,28,32,34,34,36,38,\
26,28,30,32,34,36,38,40,\
28,30,32,34,36,38,42,42,\
30,32,34,36,38,40,42,44:\
"
VBITRATE=$MAXRATE
function usage() {
echo
echo "Usage: $HOWCALLED <shrunk name> <file or mplayer source> [variable video bitrate kbs [$VBITRATE]]"
echo
echo "result: (S,X)VCD and mpeg(1,2) encoded with $HOWCALLED."
echo
exit 1
}
HOWCALLED=`basename $0`
[ $# -lt 2 ] && usage
case $1 in
-*)
usage
;;
*)
NAME=$1
shift 1
;;
esac
DIR="`pwd`"
FILE=$1
if [ "$1" == "`basename \"$1\"`" ]; then
FILE="$DIR/$1"
fi
shift 1
VBITRATE=$MAXRATE
if [ "$1"x != "x" ]; then
VBITRATE=$1
shift 1
fi
COMMAND_LINE_MENCODER=$*
rm -rf divx2pass.log
rm -rf frameno.avi
rm -rf $NAME.mpv
AUDIO="audio.pcm"
# Calculate new scale-to sizes, to fix the aspect-ratio problem
scaleto=`echo "$HEI-(480-${HEIGHT})" | bc -l`
for word in $PASS ; do
VIDEO="video.mpg"
[ "$word" == "single" ] && pass=""
[ "$word" == "1st" ] && pass=":vpass=1"
[ "$word" == "1st" ] && VIDEO="/dev/null"
[ "$word" == "2nd" ] && pass=":vpass=2"
echo "Starting $word pass"
command="mencoder -sws 0 -vf ${VFILTER}dsize=4/3,scale=${WID}:${scaleto},expand=${WID}:${HEI}${FPS} \
-forceidx -of mpeg -nosound \
-ovc lavc -lavcopts aspect=4/3:${MATRIX}vcodec=${VCODEC}:vbitrate=${VBITRATE}:mbd=2:vrc_minrate=300:vrc_maxrate=2500:vrc_buf_size=320:keyint=25${LAVCOPTS}$pass \
${SUB} \
-subfont-autoscale ${autoscale} \
-subfont-outline ${outline} \
-subfont-text-scale ${textscale} \
-subpos ${subpos} \
$COMMAND_LINE_MENCODER $FILE -o $VIDEO"
echo "$command $NOTverbose"
eval "$command $NOTverbose"
done
# Dump video
eval "mplayer -noframedrop -dumpvideo -dumpfile $NAME.mpv $VIDEO"
# Create pipe
rm -f $AUDIO
mkfifo -m 660 $AUDIO
# Encode audio
eval "mplayer -ni -noframedrop -vo null -ao pcm -aofile $AUDIO $FILE $NOTverbose &"
eval "ffmpeg -y -i $AUDIO -ab $AUDIORATE -ar 44100 -ac 2 -f mp2 $NAME.mpa $NOTverbose &"
# Wait for process to die
wait
rm -f $VIDEO
rm -f $AUDIO
rm -f info
rm -f ${NAME}??.mpg
# Multiplex
eval "tcmplex $MUXMODE -i $NAME.mpv -p $NAME.mpa -o $NAME.mpg"
rm -f tcmplex.tmp
echo "$HOWCALLED ENCODING IS COMPLETED in $SECONDS seconds."
for i in ${NAME}??.mpg; do
cue="`basename $i .mpg`.cue"
bin="`basename $i .mpg`.bin"
rm -f $cue $bin
eval "vcdimager $IMAMODE -c $cue -b $bin $i"
done
for cue in ${NAME}??.cue; do
bin="`basename $cue .cue`.bin"
echo "PLEASE INSERT BLANK CD IN CD-WRITER, after a keypress we start:"
read -n 1 null
eval "cdrdao write $CDOPT $CDDEV $cue"
echo "CD WRITING IS COMPLETED."
done
exit 0
# last line of this script
[EDIT]
This
Code:
MUXMODE="-m s -F tcmplex.tmp"
should be
Code:
MUXMODE="-m 2 -F tcmplex.tmp"
[/EDIT]
/tetra
|