digitalFAQ.com Forums [Archives]

digitalFAQ.com Forums [Archives] (http://www.digitalfaq.com/archives/)
-   Video Encoding and Conversion (http://www.digitalfaq.com/archives/encode/)
-   -   FFMPEG: Mencoder/ffmpeg aspect-ratio (http://www.digitalfaq.com/archives/encode/7018-ffmpeg-mencoder-ffmpeg.html)

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

russiansexpat 12-07-2003 06:39 AM

Thanks.

Just a few minor suggestions:

scaleto=`echo "$HEI-(480-${HEIGHT})" | bc -l`
scaleto=`echo "$HEI-($WID-${HEIGHT})" | bc -l`

Use http://mpgtx.sourceforge.net/
to get information from mpg source (tcprobe does not work with them).

VFILTER="hqdn3d,"
VFILTER="denoise3d," I could not mention any difference between quality but denoise3d is faster.

Bring back $SWS variable to choose type of software scaler
-sws 2 should be if you do upscaling either dimension.
Video filter time consumption is minor compared time spent by vcodec.

I did not try keyint=$OTHER gop sizes, but I saw ppl use 50.

Finally, if you use tcprobe or mpgtx to detect source resoluition and fps, and do calculations and scaling,
you can have static code with no need of editable section.

If you have toolame from http://mikecheng.d2.net.au/#tooLAME you can try to encode sound with variable mp2. Not sure if standalone players are capable of playing variable mp2.

tetra 12-07-2003 08:45 AM

Quote:

Originally Posted by russiansexpat
Thanks.

Just a few minor suggestions:

scaleto=`echo "$HEI-(480-${HEIGHT})" | bc -l`
scaleto=`echo "$HEI-($WID-${HEIGHT})" | bc -l`

Ooops, whipped that up too fast to think like that :)

Quote:

Use http://mpgtx.sourceforge.net/
to get information from mpg source (tcprobe does not work with them).
I leave that for you :)

Quote:

VFILTER="hqdn3d,"
VFILTER="denoise3d," I could not mention any difference between quality but denoise3d is faster.
Great, gonna try that.

Quote:

Bring back $SWS variable to choose type of software scaler
-sws 2 should be if you do upscaling either dimension.
Video filter time consumption is minor compared time spent by vcodec.
I don't have the slightest idea what that does or means.

Quote:

Finally, if you use tcprobe or mpgtx to detect source resoluition and fps, and do calculations and scaling,
you can have static code with no need of editable section.
That is very true.

Quote:

If you have toolame from http://mikecheng.d2.net.au/#tooLAME you can try to encode sound with variable mp2. Not sure if standalone players are capable of playing variable mp2.
I'll leave this one for you too :)

/tetra

kwag 12-07-2003 09:58 AM

I love this thread and data :mrgreen:

Thanks,
-kwag

ak47 12-07-2003 11:01 AM

Did you try CQ mode instead of just VBR. I added this vqscale=5 to my script and the compression gained a lot(also I removed the VBITRATE).

russiansexpat 12-07-2003 07:33 PM

2) I missed here something. My understanding is that quantisation matrix tells encoder how maintain quality in proximity of enconding frame and if bitrate is variable, encoder adjusts it accordingly.
Constant quality encoding is maintaining target quality and to adjust bitrate accordingly.
How you can mix these two different approaches - constant quality (CQ) and Matrix?

1) I have some doubts how mencoder actually processing the matrix. mencoder mpeg1video and mpeg2video codecs are based on libavcodec from ffmpeg. I can't see options in command line ffmpeg for custom intra and non-intra matrices;
then how mencoder can have them?

http://www.digitalfaq.com/archives/error.gif

kwag 12-07-2003 09:29 PM

Quote:

Originally Posted by russiansexpat
2) I missed here something. My understanding is that quantisation matrix tells encoder how maintain quality in proximity of enconding frame and if bitrate is variable, encoder adjusts it accordingly.
Constant quality encoding is maintaining target quality and to adjust bitrate accordingly.
How you can mix these two different approaches - constant quality (CQ) and Matrix?

Constant quality and a matrix are two different things, but related :!:
In full blown technical terms: http://www.studio-systems.com/broadf...0Cinema/40.htm

-kwag

japie 12-08-2003 01:23 PM

Quote:

Originally Posted by russiansexpat
I can't see options in command line ffmpeg for custom intra and non-intra matrices;

ffmpeg -h

-intra_matrix matrix specify intra matrix coeffs
-inter_matrix matrix specify inter matrix coeffs

russiansexpat 12-08-2003 03:29 PM

I use ffmpeg-0.4.8
I saw some scetches that next versions of ffmpeg have it, but
on this machine I can install .rpm only without compilation.

russiansexpat 12-08-2003 03:40 PM

Below is a version of the script with constant quality.
It appears to be faster and easy to use, just watch output line of mencoder - last field of this line is a current encoding bitrate, and also in this line you can see estimated time of completion and file size, so you can terminate the script if you don't like them.
(some minor problem with maxrate - when you select high quality, the maxrate is exceeded; nevertheless tcmplex clipped bitrates to 2300 max and vcdimager didn't complain.)
Code:

#!/bin/bash
#
# Copyright (C) 2003-2038 russiansexpat
#
# 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
#
# does not require: microsoft. This script has not been tested on animals.

for exe in mencoder mplayer ffmpeg tcmplex vcdimager cdrdao; do
  if [ -z "`which $exe`" ]; then
      echo "ERROR: $exe must be in your path $PATH"
      exit 1
  fi
done

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\
"

Q="6" # constant video quality
      # best=2 excellent<=4 good<=7 even=11 for XVCD
      #        excellent<=6 good<=9 even=13 for SVCD

#---------- below you can edit parameters ----------

AUDIORATE=128
#AUDIORATE=224

#ASPECT="aspect=4/3" # to store movie aspect internally
ASPECT="autoaspect"

# see 'man mencoder'
LAVCOPTS=":vlelim=-4:vcelim=7:lumi_mask=0.05:dark_mask=0.01:naq:dia=2:last_pred=2"
#

PASS="single"
#PASS="1st 2nd"

NOTverbose="2> /dev/null"
#NOTverbose="&> /dev/null"

# for xvcd
#VCODEC=mpeg1video
#MUXMODE="-m 1 -F tcmplex.tmp"
#IMAMODE="-t vcd2"

# for svcd
VCODEC=mpeg2video
MUXMODE="-m s -F tcmplex.tmp"
IMAMODE="-t svcd"

#CDSIZE=735 # 74min CD
CDSIZE=795 # 80min CD
#CDSIZE=895 # 90min CD

CDOPT="--force --reload --eject"
CDDEV="--device 0,4,0"

#---------- above you can edit parameters ----------

function usage() {
  echo
  echo "Usage:  $HOWCALLED <shrunk name> <file or mplayer source> [quality [$Q]]"
  echo
  echo "qualilty: best=2 excellent<=4 good<=7 even=11 - XVCD"
  echo "                excellent<=6 good<=9 even=13 - SVCD"
  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

if [ "$1"x != "x" ]; then
  Q=$1
  shift 1
fi

COMMAND_LINE_MENCODER=$*

rm -rf divx2pass.log
rm -rf frameno.avi

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"

  command="mencoder \
  -forceidx -of mpeg -nosound \
  -ovc lavc -lavcopts $ASPECT:vcodec=$VCODEC:$MATRIX:mbd=2:vqscale=$Q:vrc_minrate=300:vrc_maxrate=2300:vrc_buf_size=320:keyint=25${LAVCOPTS}$pass \
  $COMMAND_LINE_MENCODER $FILE -o $VIDEO"

  echo "$command $NOTverbose"
  eval "$command $NOTverbose"
done

rm -rf $NAME.mpv
eval "mplayer -noframedrop -vc dummy -vo null -dumpvideo -dumpfile $NAME.mpv $VIDEO &> /dev/null &"

AUDIO="audio.wav"
rm -f $AUDIO
mkfifo -m 660 $AUDIO

eval "mplayer -noframedrop -vc dummy -vo null -ao pcm -waveheader -aofile $AUDIO $FILE &> /dev/null &"
eval "ffmpeg -y -i $AUDIO -ab $AUDIORATE -ar 44100 -ac 2 -f mp2 $NAME.mpa &"
#eval "lame -h --abr $AUDIORATE --resample 44.1 - $NAME.mpa < $AUDIO &"

wait

rm -f $VIDEO
rm -f $AUDIO

rm -f $NAME??.mpg
echo "maxFileSize = $CDSIZE" > tcmplex.tmp
eval "tcmplex $MUXMODE -i $NAME.mpv -p $NAME.mpa -o $NAME.mpg"
rm -f tcmplex.tmp

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

echo -e "\n$HOWCALLED encoding is completed in $SECONDS seconds.\n"

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


russiansexpat 12-11-2003 11:19 AM

Just let you know that a new version of the script availabe:
http://www.kvcd.net/forum/viewtopic.php?t=7771&start=21
It can do variable audio bitrate for mp2.


All times are GMT -5. The time now is 06:16 AM  —  vBulletin © Jelsoft Enterprises Ltd

Site design, images and content © 2002-2024 The Digital FAQ, www.digitalFAQ.com
Forum Software by vBulletin · Copyright © 2024 Jelsoft Enterprises Ltd.