typo fix
[mplayer/greg.git] / TOOLS / encode2mpeglight
blob221433021b464c2beb02ccc630447a1da393d648
1 #!/bin/bash
3 # Version: 0.6.3
5 # Licence: GPL
7 # 2004-05-22 Giacomo Comes <encode2mpeg at users.sourceforge.net>
8 # 2006-11-06 <encode2mpeg at email.it>
10 # Purpose: Convert anything MPlayer can play to AVI/VCD/SVCD/DVD MPEG
12 # encode2mpeglight is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License.
16 # encode2mpeglight is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with encode2mpeglight; if not, write to the Free Software
23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 ###############################################################################
27 # encode2mpeglight is a program that can create VCD/SVCD/DVD MPEGs
28 # and eventually extract VobSub subtitles from a DVD using only
29 # MEncoder/MPlayer.
31 # encode2mpeglight is a stripped release of encode2mpeg and therefore the
32 # code is redundant in several places, with many variables defined and
33 # used for no apparent reason. This cannot be avoided easily.
34 # A command line like:
35 # encode2mpeglight <encode2mpeglight options>
36 # will produce almost the same results as:
37 # encode2mpeg -mpeg -mpegonly <encode2mpeglight options>
39 # If you need more features like:
40 # - two or more audio streams, chapters, subtitles, menu
41 # - creation, burn and verification of the disk image
42 # - creation of MPEG-4 avi and subtitles for a hardware player
43 # and more, consider to use the full release (http://encode2mpeg.sf.net)
45 # encode2mpeglight is mainly tested with the stable release of MPlayer,
46 # I try to make it work with SVN too, but due to the "unstable" nature of
47 # SVN, bugs may suddenly appear. If you find any, please report them to me.
48 ###############################################################################
50 ###############################################################################
51 #### start
52 ###############################################################################
53 export LC_ALL=POSIX
54 set -B +f
55 shopt -u xpg_echo nullglob
56 PROGNAME=${0##*/}
57 PROGFILE=$(type -p "$0")
58 VERSION=$(awk '$2=="Version:"{print $3;exit}' <"$PROGFILE")
59 BROWSER=
61 ###############################################################################
62 #### some functions
63 ###############################################################################
64 OptionsText () {
65 echo
66 echo "Arguments enclosed in [ ] are optional"
68 ###############################################################################
69 ModeText () {
70 echo
71 echo "$PROGNAME defaults to encode2mpeg's MPEG Mode, the other modes are not"
72 echo "available."
74 ###############################################################################
75 usage () {
76 echo -e "Usage: $PROGNAME options source\nOptions:"
77 sed -n '/^#### PARSING/,/^done/!d;/^done/q;/^[ ]*-[^)]*)/,/#-/!d;s/)$//;s/) *#/ /;s/#-/# /;s/ *#L.$//;s/#//;p' "$PROGFILE"
78 ModeText
79 OptionsText
81 ###############################################################################
82 shortusage () {
83 echo -e "\n$PROGNAME v. $VERSION Copyright (C) 2004-2006 Giacomo Comes\n"
84 echo "This is free software; see the source for copying conditions. There is NO"
85 echo "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,"
86 echo "to the extent permitted by law."
87 echo
88 echo "Usage: $PROGNAME options source"
89 echo "Options:"
90 sed -n '/^#### PARSING/,/^done/!d;/^done/q;/^[ ]*-[^)]*)/!d;s/)$//;s/) *#/ /;s/ *#L.$//;p' "$PROGFILE"
91 OptionsText
93 ###############################################################################
94 mp_identify () {
95 mplayer -msglevel identify=6 -vo md5sum:outfile=/dev/null -ao null -nocache -frames 0 "$@" 2>/dev/null
97 ###############################################################################
98 id_find () {
99 local ID=$1
100 shift
101 mp_identify "$@" | awk -v id=$ID -F= '$1==id{t=$2}END{print t}'
103 ###############################################################################
104 get_aspect () {
105 mplayer -vc -mpegpes, -vo null -ao null -nocache -frames 4 "$@" 2>/dev/null | sed '/^Movie-Aspect is/!d;s/.*Movie-Aspect is //;s/:.*//'
107 ###############################################################################
108 mlistopt () {
109 mplayer -list-options 2>&1 | awk '$1=="Name"{m=1}{if(m&&$1!="Name"&&$1!=""&&$1!="Total:")print "\""$1"\""}'
111 ###############################################################################
112 do_log () {
113 echo "$1"
114 ((LG)) && echo "$1" >>"$output".log || WARN="$WARN${WARN:+\n}$1"
116 ###############################################################################
117 isarg () {
118 if [[ ${2:0:1} = - || ! $2 || $2 = help ]]; then
119 [[ ${2:0:1} = - ]] && echo "**ERROR: [$PROGNAME] invalid argument '$2' for option '$1'"
120 [[ ! $2 ]] && echo "**ERROR: [$PROGNAME] invalid null argument for option '$1'"
121 "$PROGFILE" -norc -l | sed '/^ '$1'$/,/^ -/!d' | sed '$d'
122 "$PROGFILE" -norc -l | sed '/^ '$1'[ |]/,/^ -/!d' | sed '$d'
123 exit 1
125 if [[ $2 = doc ]]; then
126 show_html ${3:+$3.html}
127 exit
130 ###############################################################################
131 pr_date () {
132 echo "[$(date '+%Y-%m-%d %H:%M:%S')]"
134 ###############################################################################
135 pr_time () {
136 date '+%j %H %M %S' | awk '{print $1*86400+$2*3600+$3*60+$4}'
138 ###############################################################################
139 get_abr () {
140 local INFO ABR
141 INFO=$(mp_identify "${MPLAYEROPT[@]}" ${dvddev:+-dvd-device "$dvddev"} "$@" | grep '^ID_')
142 ABR=$(echo "$INFO" | grep '^ID_AUDIO_BITRATE' | tail -1 | cut -f2 -d=)
143 case $(echo "$INFO" | grep '^ID_AUDIO_CODEC' | cut -f2 -d=) in
144 dvdpcm) abr=$((abr+ABR/1024)) ;;
145 *) abr=$((abr+ABR/1000)) ;;
146 esac
148 ###############################################################################
149 get_pwd () {
150 pushd &>/dev/null "$1"
151 echo "$PWD/$2"
152 popd &>/dev/null
154 ###############################################################################
155 show_html () {
156 local i LIST HTML PREFIX OPTION INSTDOCDIR SRCDOCDIR
157 INSTDOCDIR=${PROGFILE%/bin/$PROGNAME}/share/doc/encode2mpeg
158 SRCDOCDIR=${PROGFILE%/$PROGNAME}/doc
159 if [[ -f $INSTDOCDIR/encode2mpeg.html ]]; then
160 HTML=$(get_pwd "$INSTDOCDIR" encode2mpeg.html)
161 elif [[ -f $SRCDOCDIR/encode2mpeg.html ]]; then
162 HTML=$(get_pwd "$SRCDOCDIR" encode2mpeg.html)
163 else
164 HTML="http://encode2mpeg.sourceforge.net"
166 if [[ -f $INSTDOCDIR/html/$1 ]]; then
167 HTML=$(get_pwd "$INSTDOCDIR/html" $1)
168 elif [[ -f $SRCDOCDIR/html/$1 ]]; then
169 HTML=$(get_pwd "$SRCDOCDIR/html" $1)
170 elif [[ $1 && ! -d $INSTDOCDIR/html && ! -d $SRCDOCDIR/html ]]; then
171 HTML="http://encode2mpeg.sourceforge.net/html/$1"
173 LIST=(mozilla seamonkey firefox)
174 [[ ${HTML:0:1} = / ]] && PREFIX=file:// || PREFIX=
175 for ((i=0;i<${#LIST[*]};i++)); do
176 type ${LIST[i]} &>/dev/null && ${LIST[i]} -remote 'openURL('"$PREFIX$HTML"',new-tab)' 2>/dev/null && return
177 done
178 LIST=(mozilla firefox seamonkey opera konqueror epiphany galeon)
179 if [[ $BROWSER ]]; then
180 type "$BROWSER" &>/dev/null && LIST=("$BROWSER") || echo "++ WARN: default browser '$BROWSER' not found, using builtin browser list"
182 for ((i=0;i<${#LIST[*]};i++)); do
183 if type "${LIST[i]}" &>/dev/null; then
184 case ${LIST[i]} in
185 opera) OPTION=--newpage ;;
186 epiphany|galeon) OPTION=--new-tab ;;
187 *) OPTION= ;;
188 esac
189 "${LIST[i]}" $OPTION "$HTML" &
190 return
192 done
195 ###############################################################################
196 #### variable initialization
197 ###############################################################################
198 abr=;asr=;vbr=;vfr=;videonorm=;frameformat=;output=;audioformat=;mp1=;mp2=;mp3=;ac3=;dts=;lpcm=;aac=;normalize=;volume=;multiaudio=;mpegchannels=;quiet=;resume=;blank=;encode=;ofps=;mono=;usesbr=;sbr=;clear=;keep=;frames=;avisplit=;channels=;vcustom=;acustom=;cpu=;interlaced=;mpegaspect=;intra_matrix=;inter_matrix=;fixavi=;mpeg=;crop=;audioid=;dvdaudiolang=;bframes=;firstchap=;lastchap=;dvdtrack=;addchapter=;cdi=;mpegfixaspect=;nowait=;vf=;frameres=;trick=;autocrop=;afm=;cache=;removecache=;turbo=;dvddev=;srate=;endpos=;fourcc=;menu=;menubg=;dvdtitle=;rotate=;menuvtsbg=;autosync=;telecine=;AFMT=;telesrc=;vcodec=;vrfyumnt=;burniso=;verify=;fixasync=;removedir=;MPGRES=;GOP=;TXTSUBOPT=;usespeed=;testmca=;chconf=;noodml=;pictsrc=;slideaudio=;audioonly=;norc=;rawsub=;vobsubsrc=;af=;lavf=;subrender=;harddup=;overburn=
199 unset encsid encsdx encsla addsub addsdx addsla savecache txtsub txtsubopts
200 zoom=0
201 scale=1
202 fast=1
203 MAXSTEP=6 # burn is the default
204 step=$MAXSTEP
205 split=0
206 vbitrate=16000
207 mpegmbr=0
208 overscan=0
209 iter=0
210 bakiter=0
211 hispeed=0
212 BGTITLE=1
213 TANIM=0
214 TFMT=png
215 TMODE=0
216 TKFRM=0
217 TSECS=5
218 TPART=4
219 TPARTSEC=15
220 TFONTSIZE=1
221 TFONTBG=1
222 TLINES=2
223 TCPR=2
224 MENUERR=0
225 DVDPLAY=0
226 MENUOS=0
227 TSETB=1
228 DVDFS=1
229 VTSMBG=0
230 menufix=0
231 cdburndevice=0,0,0
232 dvdburndevice=/dev/cdrecorder
233 fsize=;fint=;ffrac=;fpre=;audiosize=0
234 ASPECT=(1 1 4/3 16/9 2.21)
235 CH6LIST=(l r ls rs c lfe)
236 TOOL=
237 MPEG2ENCOPT=;YUVSCALEROPT=;YUVDENOISE=;MPLEXOPT=;VCDIMAGEROPT=;DVDAUTHOROPT=;CDRDAOOPT=;GROWISOFSOPT=;MUSICINOPT=
238 unset MPLAYEROPT MPLEXSTREAM MENCODEROPT
239 LPCMPAR=
240 SUBLANG=
241 unset SUBTEXT AUDIOTEXT CHAPTERTEXT TITLESET EXTWAV PROFILE MSRC
242 unset CLEAN
243 DEBUG=0
244 LAVC=
245 WARN=
246 LOG=
247 LG=0
248 SVN=0
249 MAXFIX=20
250 timelen=0
251 ocrsub=0
252 slidefps=1
253 default_intra=8,16,19,22,26,27,29,34,16,16,22,24,27,29,34,37,19,22,26,27,29,34,34,38,22,22,26,27,29,34,37,40,22,26,27
254 default_intra=$default_intra,29,32,35,40,48,26,27,29,32,35,40,48,58,26,27,29,34,38,46,56,69,27,29,35,38,46,56,69,83
255 default_inter=16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16
256 default_inter=$default_inter,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16
257 hires_intra=8,16,18,20,24,25,26,30,16,16,20,23,25,26,30,30,18,20,22,24,26,28,29,31,20,21,23,24,26,28,31,31,21,23,24
258 hires_intra=$hires_intra,25,28,30,30,33,23,24,25,28,30,30,33,36,24,25,26,29,29,31,34,38,25,26,28,29,31,34,38,42
259 hires_inter=$default_inter
260 kvcd_intra=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
261 kvcd_intra=$kvcd_intra,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
262 kvcd_inter=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
263 kvcd_inter=$kvcd_inter,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
264 tmpgenc_intra=8,16,19,22,26,27,29,34,16,16,22,24,27,29,34,37,19,22,26,27,29,34,34,38,22,22,26,27,29,34,37,40,22,26,27
265 tmpgenc_intra=$tmpgenc_intra,29,32,35,40,48,26,27,29,32,35,40,40,58,26,27,29,34,38,46,56,69,27,29,35,38,46,56,69,83
266 tmpgenc_inter=16,17,18,19,20,21,22,23,17,18,19,20,21,22,23,24,18,19,20,21,22,23,24,25,19,20,21,22,23,24,26,27,20,21,22
267 tmpgenc_inter=$tmpgenc_inter,23,25,26,27,28,21,22,23,24,26,27,28,30,22,23,24,26,27,28,30,31,23,24,25,27,28,30,31,33
268 TXTSUBDEF=( languageId nolang delay 0 font arial.ttf size 28 bottom-margin 30 characterset ISO8859-1 movie-height-reduction 0 fps default )
269 AVISUBDEF=( format SubViewer name-extension null fileformat unix version-number off delay 0 fps default suffix default )
271 #### encode2mpeglight defauls
272 mpeg=1
273 encode=7:2:2
275 (($#)) || ! shortusage || exit 1
276 CMD=( "$@" )
278 #### options array
279 MOPT=( $(mlistopt | grep -v -e : -e '*')
280 $(mlistopt | sed -n '/:/s/:.*/"/p' | uniq)
281 $(mplayer -vfhelp 2>&1 | awk '/vf-/{printf("\"%s\"\n",$1)}')
282 $(mplayer -zrhelp 2>/dev/null | awk '$1~/^-zr/{printf("\"%s\"\n",substr($1,2))}') )
284 ###############################################################################
285 #### check rc file
286 ###############################################################################
287 if [[ -s ~/.encode2mpegrc ]]; then
288 for ((i=0;i<${#CMD[*]};i++)); do
289 [[ ${CMD[i]} = -norc ]] && norc=1 && break
290 done
291 if [[ ! $norc ]] && ! awk '{if($1~"^#")exit 1}' ~/.encode2mpegrc ; then
292 do_log "++ WARN: [$PROGNAME] ~/.encode2mpegrc appears to contain comments, ignoring it" >/dev/null
293 norc=1
295 [[ ! $norc ]] && set -- $(<~/.encode2mpegrc) "$@"
298 ###############################################################################
299 #### arguments parsing
300 ###############################################################################
301 while (($#)) ; do
302 #### PARSING
303 case $1 in
304 -h|-help)
305 #-list the available options
306 [[ $2 = doc || $2 = help ]] && isarg $1 $2
307 shortusage
308 exit
310 -l|-longhelp)
311 #-print this help page
312 [[ $2 = doc || $2 = help ]] && isarg $1 $2
313 usage
314 exit
316 -doc|-documentation) #[[<browser>:][<html page>]]
317 #-show the html documentation
318 [[ $2 = doc || $2 = help ]] && isarg $1 $2
319 [[ ${2%:*} != $2 ]] && BROWSER=${2%:*}
320 show_html ${2#*:}
321 exit
323 -noshowlog)
324 #-do not show the log file
325 [[ $2 = doc || $2 = help ]] && isarg $1 $2
326 quiet=1
328 -o|-output) #<filename>
329 #-filename of the output stream
330 echo "$2" | grep -q '/' && [[ ! -d ${2%/*} ]] && echo "**ERROR: [$PROGNAME] directory ${2%/*} argument of '$1' not found" && exit 1
331 output=$2
332 shift
334 -a) #<n>
335 # aspect ratio VCD SVCD DVD
336 # 1 - 1:1 x * x x = mpeg2enc and mencoder
337 # 2 - 4:3 x x x * = mencoder only
338 # 3 - 16:9 x x x
339 #- 4 - 2.21:1 * x
340 MPEG2ENCOPT="$MPEG2ENCOPT -a $2"
341 [[ $2 -eq 0 ]] && MPEG2ENCOPT="${MPEG2ENCOPT% ${2}} 1"
342 mpegaspect=$2
343 isarg $1 "$2" direct
344 shift
346 -mpegfixaspect) #[pad|crop]
347 # force the aspect ratio of the source video to match the one
348 # specified with -a; this can be done padding the video with
349 #-black lines or cropping the video; the default is padding
350 [[ $2 = doc || $2 = help ]] && isarg $1 $2 aspect
351 mpegfixaspect=0
352 if [[ $2 = pad || $2 = crop ]]; then
353 [[ $2 = crop ]] && mpegfixaspect=1
354 shift
357 -rotate) #<0-3>
358 # rotate the source video by 90 degrees; set -mpegfixaspect;
359 # 0 rotate clockwise and flip, 1 rotate clockwise
360 #-2 rotate counterclockwise, 3 rotate counterclockwise and flip
361 rotate=1
362 echo "$2" | grep -q '^[0-3]$' && rotate=$2
363 isarg $1 "$2" aspect
364 shift
365 : ${mpegfixaspect:=0}
367 -overscan) #<n>
368 # shrink the video of n% and surround it with black borders,
369 # n can be 1-99, using 10 should make visible on a TV the full video
370 # image; if n is negative, the result is a magnification of the
371 # central part of the image; n>0 set -mpegfixaspect pad, n<0 set
372 #--mpegfixaspect crop; in Avi Mode n can only be positive
373 echo "$2" | grep -qE '^-?[0-9]?[0-9]$' && overscan=$2
374 [[ ${2:0:1} = - ]] && mpegfixaspect=1 || mpegfixaspect=0
375 isarg $1 "${2#-}" $( ((step==1)) && echo Avi || echo aspect)
376 shift
378 -abr) #<n>
379 #-audio bit rate of the VCD/SVCD/DVD [MP2:224,MP3:128,AC3:448]
380 if [[ $2 = list ]]; then
381 sed '/^check_abr/,/case/!d;/'"$audioformat"':/!d;/[^0-9]'"$asr"'/!d;s/[^)]*)//;s/ */ /g;s/^/'"$audioformat $asr"'/' "$PROGFILE"
382 exit
384 abr=$2
385 isarg $1 "$2" direct
386 shift
388 -asr) #<n>
389 #-audio sample rate of the VCD/SVCD/DVD [MP2/MP3:44100,AC3:48000]
390 asr=$2
391 isarg $1 "$2" direct
392 shift
394 -vbr) #<n>
395 #-video bit rate of the VCD/SVCD/DVD [VCD:1152,SVCD:2500,DVD:7500]
396 vbr=$2
397 isarg $1 "$2" direct
398 shift
400 -vfr) #<n>
401 # video frame rate of the output stream
402 # [NTSC/VCD:4,NTSC/SVCD-DVD:1,PAL:3,AVI:2]
403 # 1 - 24000.0/1001.0 (NTSC 3:2 pulldown converted FILM)
404 # 2 - 24.0 (NATIVE FILM)
405 # 3 - 25.0 (PAL/SECAM VIDEO / converted FILM)
406 # 4 - 30000.0/1001.0 (NTSC VIDEO)
407 # 5 - 30.0
408 # 6 - 50.0 (PAL FIELD RATE)
409 # 7 - 60000.0/1001.0 (NTSC FIELD RATE)
410 #- 8 - 60.0
411 vfr=$2
412 isarg $1 "$2" direct
413 shift
415 -n|-video-norm) #<n|p|s>
416 # set the video norm of the VCD/SVCD/DVD; NTSC is USA standard,
417 # PAL is European standard and SECAM is French standard; concerning
418 #-VCD/SVCD/DVD encoding, SECAM is equivalent to PAL
419 case $2 in
420 n|N|ntsc|NTSC) videonorm=n ;;
421 p|P|pal|PAL) videonorm=p ;;
422 s|S|secam|SECAM) videonorm=s ;;
423 doc|help) isarg $1 $2 direct ;;
424 *) echo "**ERROR: [$PROGNAME] invalid argument '$2' for option '$1'" ; "$PROGFILE" -norc $1 help ; exit 1 ;;
425 esac
426 shift
428 -p|-pulldown|-telecine) #
429 # set a flag in the output stream of the SVCD/DVD that tell the
430 # decoder to play the movie as NTSC video using "3:2 pull-down"
431 #-instead of "-vfr 4" use "-vfr 1 -p" (smaller output stream)
432 [[ $2 = doc || $2 = help ]] && isarg $1 $2 direct
433 telecine=1
435 -res) #<1-7>
436 # force one of the following video resolutios:
437 # PAL NTSC
438 # 1 352x288 352x240 (VCD)
439 # 2 352x576 352x480 (CVD)
440 # 3 480x576 480x480 (SVCD)
441 # 4 528x576 528x480 (KVCD)
442 # 5 544x576 544x480 (KVCD)
443 # 6 704x576 704x480 (DVB)
444 #- 7 720x576 720x480 (DVD)
445 isarg $1 "$2" direct
446 echo "$2" | grep -q '^[1-7]$' && MPGRES=$2
447 shift
449 -gop) #<n>
450 # set the number of pictures per GOP; the default value is the one
451 #-required by the standard
452 isarg $1 "$2" direct
453 GOP=$2
454 shift
456 -kvcd) #<1-4>
457 # generate KVCD (www.kvcd.net) compliant frames on output; the
458 # following resolutions are possible:
459 # PAL NTSC GOP
460 # 1 528x576 528x480 24
461 # 2 528x576 528x480 def
462 # 3 544x576 544x480 24
463 #- 4 544x576 544x480 def
464 isarg $1 "$2" direct
466 echo "$2" | grep -q '^[1-4]$' && a=$2
467 shift 2
468 set -- " " -qmatrix kvcd -res $((3+(a+1)/2)) $([[ $a == [13] ]] && echo "-gop 24") "$@"
470 -vcd) #
471 #-generate VCD compliant frames on output (default)
472 [[ $2 = doc || $2 = help ]] && isarg $1 $2 direct
473 frameformat=VCD
475 -svcd) #[1-2]
476 # generate SVCD compliant frames on output; the following resolutions
477 # are possible: PAL NTSC
478 # 1 480x576 480x480
479 # 2 352x288 352x240
480 #-default is 1
481 [[ $2 = doc || $2 = help ]] && isarg $1 $2 direct
482 frameformat=SVCD
483 frameres=1
484 echo "$2" | grep -q '^[1-2]$' && frameres=$2 && shift
486 -svcdht) #
487 # enable the VCD header trick; this trick could allow to play SVCD on
488 # DVD player that does not support SVCD. For more details see:
489 #-http://www.videohelp.com/svcd
490 [[ $2 = doc || $2 = help ]] && isarg $1 $2 direct
491 trick=1
493 -dvd) #[1-5]
494 # generate DVD compliant frames on output; the following resolutions
495 # are possible: PAL NTSC
496 # 1 720x576 720x480
497 # 2 704x576 704x480
498 # 3 480x576 480x480 (non standard DVD-SVCD)
499 # 4 352x576 352x480
500 # 5 352x288 352x240
501 #-default is 1
502 [[ $2 = doc || $2 = help ]] && isarg $1 $2 direct
503 frameformat=DVD
504 frameres=1
505 echo "$2" | grep -q '^[1-5]$' && frameres=$2 && shift
507 -vcodec) #<mpeg1|mpeg2|mpeg4>
508 #-force the selected video codec [VCD:mpeg1,SVCD-DVD:mpeg2,AVI:mpeg4]
509 isarg $1 "$2" Avi
510 [[ $2 == mpeg[124] ]] && vcodec=$2 && [[ ${vcodec:4:1} == [12] ]] && vcodec=${vcodec}video
511 shift
513 -qmatrix) #<kvcd|tmpgenc|default|hi-res>
514 # mpeg2enc custom quantization matrices: kvcd produce a smaller
515 #-output stream, hi-res is good for hi-quality source material
516 case $2 in
517 kvcd|tmpgenc|default|hi-res)
518 MPEG2ENCOPT="$MPEG2ENCOPT -K $2"
519 intra_matrix=$(eval echo \$${2/-}_intra)
520 inter_matrix=$(eval echo \$${2/-}_inter)
523 MPEG2ENCOPT="$MPEG2ENCOPT -K default"
524 intra_matrix=
525 inter_matrix=
527 esac
528 isarg $1 "$2" direct
529 shift
531 -mpeg1vbr) #
532 # produce a VCD/MPEG-1 variable bit rate stream, the output stream
533 # is smaller and a complete movie could fit in one VCD; check if
534 #-your hardware player support variable bit rate VCD
535 [[ $2 = doc || $2 = help ]] && isarg $1 $2 direct
536 MPEG2ENCOPT="$MPEG2ENCOPT -q 8"
537 LAVC=":vrc_buf_size=327"
539 -mpegmbr) #<n>
540 # set the maximum video bit rate; the default is the value of vbr;
541 #-a value of 0 remove the limit
542 mpegmbr=$2
543 [[ $2 -eq 0 ]] && mpegmbr=
544 isarg $1 "$2" mpeg
545 shift
547 -mpegchannels) #<1-6>
548 # number of channels of the MPEG audio stream, 1-6 for ac3 and lpcm;
549 # 1-2 for mp1, mp2 and mp3; 3-6 for mp2 Multichannel Audio; for the
550 #-avi audio stream use MPlayer's option -channels
551 mpegchannels=2
552 isarg $1 "$2" direct
553 echo "$2" | grep -q '^[1-6]$' && mpegchannels=$2
554 shift
556 -normalize)
557 #-normalize to the audio stream(s)
558 [[ $2 = doc || $2 = help ]] && isarg $1 $2 $([[ $encode ]] && echo Avi || echo direct)
559 normalize=1
561 -volume) #<n>
562 # change amplitude of the audio stream; less than 1.0 decreases,
563 #-greater than 1.0 increases (float)
564 volume=$2
565 isarg $1 "$2" direct
566 shift
568 -noscale) #
569 # encode2mpeg automatically scales the video according to the MPEG
570 # profile chosen, this option disables this feature; used mainly
571 # for debug purposes.
572 [[ $2 = doc || $2 = help ]] && isarg $1 $2 direct
573 scale=
575 -monochrome)
576 #-create B/W video or avoid color artifacts in B/W source streams
577 [[ $2 = doc || $2 = help ]] && isarg $1 $2 direct
578 YUVSCALEROPT="$YUVSCALEROPT -O MONOCHROME"
580 -split) #<n>
581 #-split the resulting MPEG stream in <n> MB chunks.
582 split=$2
583 isarg $1 "$2" direct
584 shift
586 -encode) #<n:m:i[,b]>
587 # the parameter n:m:i selects the audio codec, video codec options
588 # and number of pass for mencoder, possible values are:
589 # n m i
590 # 0 copy 0 copy 1
591 # 1 pcm 1 libavcodec/mpeg 2
592 # 2 mp3lame/fast 2 as 1 + mbd=2 3
593 # 3 mp3lame/standard 3 as 1 + compression opts
594 # 4 libavcodec/mp2 4 as 1 + quality opts
595 # 5 libavcodec/mp3
596 # 6 libavcodec/ac3 for m=[2-4] and i>1 turbo is on
597 # 7 toolame/mp2
598 # 8 libfaac/aac
599 #- with n=[3-7] b specify the audio bit rate
600 encode=5:3:2
601 echo "$2" | grep -qE '^[0-8]:[0-4]:[1-9](,[0-9]+)?$' && encode=$2
602 isarg $1 "$2" $([[ $mpeg ]] && echo mpeg || echo Avi)
603 shift
605 -telecinesrc) #
606 # if you use -encode n:0:i in MPEG Mode, encode2mpeg needs to know
607 # if the source NTSC MPEG is telecined, otherwise the stream copy may
608 # not work properly; normally encode2mpeg is able to detect telecined
609 # sources, but, if the source MPEG is mixed, part not telecined and
610 # part telecined, encode2mpeg may fail to detect it. In such case,
611 #-you can use this option to specify a telecined source.
612 [[ $2 = doc || $2 = help ]] && isarg $1 $2 mpeg
613 telesrc=1
615 -turbo) #<0-1>
616 # disable (0) or force (1) turbo mode for the first pass of N pass
617 #-encoding (N>1)
618 echo "$2" | grep -q '^[0-1]$' && turbo=$2
619 isarg $1 "$2" Avi
620 shift
622 -hispeed) #
623 # increase the encoding speed of 25-50% (with -encode n:1:1); the
624 # output video stream will be bigger and can have poor quality; this
625 # option is mainly intented for testing, but it can be used if you
626 #-want to create more quickly an MPEG-4/AVI or a DVD.
627 [[ $2 = doc || $2 = help ]] && isarg $1 $2 Avi
628 hispeed=1
630 -bframes) #<0-4>
631 # specify the number of B frames; if -encode is n:3:i the default 2,
632 #-otherwise the default is no B frames; for VCD the default is 2
633 echo "$2" | grep -q '^[0-4]$' && bframes=$2
634 isarg $1 "$2" Avi
635 shift
637 -vcustom) #<libavcodec options>
638 # specify a custom set of video options for libavcodec, use with
639 #--encode n:1:i
640 vcustom=$2
641 isarg $1 "$2" Avi
642 shift
644 -acustom) #<mp3lame options>
645 # specify a custom set of lame options (with -encode 2:m:i) or faac
646 #-options (with -encode 8:m:i)
647 acustom=$2
648 isarg $1 "$2" Avi
649 shift
651 -encsid) #<sid0,sid1,...>
652 #-dump the DVD vobsub, sid is the number you give the -sid option
653 # of MPlayer.
654 encsid=( ${2//,/ } )
655 isarg $1 "$2" subtitle
656 shift
658 -encsdx) #<sid0,sid1,...>
659 # if you dump subtitle 2 and 4 and you want them to have id 0 and 1
660 #-use -encsid 2,4 -encsdx 0,1
661 encsdx=( ${2//,/ } )
662 isarg $1 "$2" subtitle
663 shift
665 -encsla) #<sla0,sla1,...>
666 #-see doc/html/subtitle.html
667 encsla=( ${2//,/ } )
668 isarg $1 "$2" subtitle
669 shift
671 -usespeed) #
672 # do frame rate conversion changing the playback speed; this option
673 # can be used if you are converting from NTSC 24fps, with or without
674 # telecine, to PAL 25fps and viceversa; during normal frame rate
675 # conversion, frames are skipped or duplicated as needed; with this
676 #-option all the frames are encoded
677 [[ $2 = doc || $2 = help ]] && isarg $1 $2 mpeg
678 usespeed=1
680 -usesbr) #[1-6|>100]
681 # use the suggested video bitrate to set the output stream size
682 # 1 - for 650MB CD 4 - for 2 x 650MB CD
683 # 2 - for 700MB CD (default) 5 - for 2 x 700MB CD
684 # 3 - for 800MB CD 6 - for 2 x 800MB CD
685 # n where n > 100 will set the file size to n MB
686 #-with -multiaudio you must to set the streamsize in MB
687 usesbr=2
688 SBR=(650 700 800 "2 x 650" "2 x 700" "2 x 800")
689 echo "$2" | grep -q '^[1-6]$' && usesbr=$2 && shift
690 echo "$2" | grep -qE '^[1-9][0-9]{2,}$' && usesbr=$2 && shift
691 [[ $2 = doc || $2 = help ]] && isarg $1 $2 Avi
693 -cpu) #<n>
694 #-number of cpu to use, default all the cpu present in the system
695 cpu=1
696 echo "$2" | grep -q '^[1-8]$' && cpu=$2
697 isarg $1 "$2" Avi
698 shift
700 -interlaced)
701 #-turn on optimization for interlaced source video.
702 [[ $2 = doc || $2 = help ]] && isarg $1 $2 $([[ $mpeg ]] && echo mpeg || echo Avi)
703 interlaced=1
705 -slidefps) #<n>
706 # fps to use when creating video from pictures, default is 1; if
707 # there is a audio file associated with a picture, the duration of
708 #-the audio file is used
709 isarg $1 "$2" images
710 slidefps=$(awk -v a=$2 'BEGIN{printf("%.3f",1/a)}')
711 shift
713 -slideaudio) #<audio file>
714 # use the content of <audio file> as audio stream when creating video
715 #-from pictures; it works correctly only if used with mf://singlepic
716 slideaudio=$2
717 if [[ ! -f $2 && $2 != /dev/null ]]; then
718 [[ $2 = doc || $2 = help || ! $2 ]] && isarg $1 "$2" images || ! echo "**ERROR: [$PROGNAME] slide audio file '$2' not found" || exit 1
720 shift
722 -norc)
723 #-do not use the settings in the file $HOME/.encode2mpegrc
724 [[ $2 = doc || $2 = help ]] && isarg $1 $2 rc
726 -debug)
727 # make a more verbose log file and creates a debug file; if you are
728 # submitting a bug report, use this option and compress and send the
729 #-log file and the debug file
730 [[ $2 = doc || $2 = help ]] && isarg $1 $2
731 echo "$2" | grep -q '^[0-7]$' && DEBUG=$2 && shift || DEBUG=3
733 (-frames)
734 frames=$2
735 isarg $1 $2
736 shift
738 (-channels)
739 channels=$2
740 isarg $1 $2
741 shift
743 (-srate)
744 MPLAYEROPT=( "${MPLAYEROPT[@]}" -srate $2 -af-adv force=1 )
745 srate=$2
746 isarg $1 $2
747 shift
749 (-endpos)
750 endpos=$2
751 isarg $1 $2
752 shift
754 (-hr-edl-seek)
755 MENCODEROPT=( "${MENCODEROPT[@]}" $1 )
757 (-vf)
758 vf="-vf $2"
759 MPLAYEROPT=( "${MPLAYEROPT[@]}" $vf )
760 isarg $1 $2
761 shift
763 (-af)
764 af="-af $2"
765 isarg $1 $2
766 shift
768 (-dvd-device)
769 dvddev=$2
770 isarg $1 $2
771 shift
773 (mf://*)
774 MPLAYEROPT[${#MPLAYEROPT[*]}]=$1
775 pictsrc=1
776 a=$IFS
777 IFS=,
778 PICTSRC=( ${1#mf://} )
779 IFS=$a
780 srate=48000
782 (-sub)
783 [[ ! -f $2 ]] && echo "**ERROR: [$PROGNAME] subtitle file '$2' not found" && exit 1
784 MPLAYEROPT=( "${MPLAYEROPT[@]}" $1 "$2" )
785 subrender=1
786 shift
788 (-mpeg|-mpegonly|-nosplit) ;;
790 if [[ ! $TOOL ]]; then
791 [[ ${1:0:1} = - ]] && ! echo "${MOPT[@]}" | grep -q "\"${1#-}\"" && \
792 echo -e "Unknown option $1\nIf this is a valid MPlayer option add '-toolopts mplayer' in front of it" && exit 1
793 MPLAYEROPT[${#MPLAYEROPT[*]}]=$1
794 else
795 case $TOOL in
797 MPLAYEROPT[${#MPLAYEROPT[*]}]=$1
799 esac
802 esac
803 shift
804 done
806 [[ -s ~/.encode2mpegrc && ! $norc ]] && do_log " INFO: [$PROGNAME] using .encode2mpegrc settings: '$(<~/.encode2mpegrc)'"
807 if [[ $subrender || $harddup ]]; then
808 if [[ $vf ]]; then
809 vf=$vf${subrender:+,expand=::::1}
810 [[ $harddup ]] && ! echo $vf | grep -q harddup && vf=$vf,harddup
811 for ((a=0;a<${#MPLAYEROPT[*]};a++)); do
812 [[ ${MPLAYEROPT[a]} = -vf ]] && MPLAYEROPT[a+1]=${vf#-vf }
813 done
814 else
815 vf="-vf ${subrender:+expand=::::1}${harddup:+${subrender:+,}harddup}"
816 MPLAYEROPT=( "${MPLAYEROPT[@]}" $vf )
820 ###############################################################################
821 #### debug part
822 ###############################################################################
823 if ((DEBUG)); then
824 if ((DEBUG & 1)); then
825 #### redirect stdout and stderr to the debug file
826 exec 3>&1
827 rm -f "$output".debug.fifo
828 mkfifo "$output".debug.fifo
829 tee "$output".debug <"$output".debug.fifo >&3 &
830 PROCTEE=$!
831 exec &>"$output".debug.fifo
832 trap 'rm "$output".debug.fifo' 0
834 if ((DEBUG & 2)); then
835 #### catch mplayer/mencoder errors
836 mplayer () {
837 command mplayer "$@"
838 ret=$? ; (( ret )) && error_line "$FUNCNAME $*" || true
840 mencoder () {
841 command mencoder "$@"
842 ret=$? ; (( ret )) && error_line "$FUNCNAME $*" || true
845 ((DEBUG & 4)) && set -x
848 ###############################################################################
849 #### ERROR if some options conflict is detected part 1/2
850 ###############################################################################
851 #### mplayer/mencoder
852 for a in mplayer mencoder ; do
853 type -f $a &>/dev/null || ! echo "**ERROR: [$PROGNAME] $a missing, install $(echo ${a:0:2} | tr '[:lower:]' '[:upper:]')${a:2}" || exit 1
854 done
855 #### output stream name check
856 [[ ! $output ]] && echo "**ERROR: [$PROGNAME] name of the output stream missing (-o name)" && exit 1
857 #### unspecified video norm
858 [[ ! $videonorm && step -gt 1 && ! ( $mpeg && ${encode%,*} == ?:0:? && ! $menu ) && ${#TITLESET[*]} -eq 0 ]] && \
859 echo "**ERROR: [$PROGNAME] you must specify a video norm (-n n|p|s)" && exit 1
860 #### libfaac check
861 if [[ ${encode%,*} == 8:?:? ]]; then
862 ! mencoder -oac help 2>/dev/null | grep -q faac && echo "**ERROR: [$PROGNAME] missing libfaac support in mencoder [-encode 8:m:i]" && exit 1
864 #### mpeg4
865 if [[ $vcodec = mpeg4 ]]; then
866 [[ $pictsrc && $step -gt 1 && $mpeg ]] && echo "**ERROR: [$PROGNAME] -vcodec mpeg4, -mpeg and mf://file are not compatible" && exit 1
868 #### pictsrc
869 if [[ $pictsrc ]]; then
870 [[ $slideaudio != /dev/null && ${encode%,*} == 0:?:? && $mpeg ]] && \
871 echo "**ERROR: [$PROGNAME] -encode 0:m:i is not compatible with mf:// in MPEG Mode" && exit 1
872 [[ $audioonly ]] && echo "**ERROR: [$PROGNAME] -audioonly does not work with mf://" && exit 1
874 #### -encode 1:m:i is not allowed
875 [[ ${encode%,*} == 1:?:? ]] && echo "**ERROR: [$PROGNAME] do not use -encode 1:m:i" && exit 1
877 ###############################################################################
878 #### WARN if some options conflict is detected
879 ###############################################################################
880 #### missing toolame support
881 if [[ ${encode%,*} == 7:?:? ]]; then
882 if ! mencoder -oac help 2>/dev/null | grep -q t[wo]olame ; then
883 encode=4:${encode#?:}
884 do_log "++ WARN: [$PROGNAME] missing toolame support in mencoder, setting -encode $encode"
885 else
886 mencoder -oac help 2>/dev/null | grep -q toolame && TOOLAME=toolame || TOOLAME=twolame
890 ###############################################################################
891 #### set default values for unspecified parameters
892 ###############################################################################
893 if [[ ! $multiaudio ]]; then
894 audiostream=1
896 ###############################################################################
897 if [[ $encode ]]; then
898 case ${encode%%:*} in
899 0) [[ ! $pictsrc ]] &&
900 AFMT=acopy ;;
901 2|3|5) AFMT=mp3 ;;
902 4|7) AFMT=mp2 ;;
903 6) AFMT=ac3 ;;
904 8) AFMT=aac ;;
905 esac
906 [[ $AFMT ]] && eval : ${audioformat:-\${$AFMT:=copy\}}
908 : ${frameformat:=VCD}
909 : ${audioformat:=${AFMT:-mp2}}
910 : ${mp1:=mp2enc}
911 ((${mpegchannels:-2}>2)) && : ${mp2:=musicin} || : ${mp2:=mp2enc}
912 : ${mp3:=lame}
913 : ${ac3:=mencoder}
914 : ${dts:=copy}
915 : ${aac:=faac}
916 case $audioformat in
917 mp1)
918 case ${mpegchannels:-2} in
919 1) : ${abr:=192} ;;
920 2) : ${abr:=384} ;;
921 esac
923 mp2)
924 case ${mpegchannels:-2} in
925 1) : ${abr:=112} ;;
926 2) : ${abr:=224} ;;
927 [3-6]) : ${abr:=384} ;;
928 esac
930 mp3)
931 case ${mpegchannels:-2} in
932 1) : ${abr:=64} ;;
933 2) : ${abr:=128} ;;
934 esac
936 ac3)
937 case ${mpegchannels:-2} in
938 1) : ${abr:=96} ;;
939 2) : ${abr:=192} ;;
940 3) : ${abr:=320} ;; # to verify
941 4) : ${abr:=384} ;; # to verify
942 5) : ${abr:=448} ;; # to verify
943 6) : ${abr:=448} ;;
944 esac
946 aac) : ${abr:=$((${mpegchannels:-2}*48))} ;;
947 #### mplex fails with asr != 48000 for lpcm
948 lpcm) : ${asr:=48000} ${abr:=$((asr*16*${mpegchannels:-2}/1024))} ;;
949 esac
950 if [[ ${encode%,*} == 0:?:? && ${!audioformat} = copy ]]; then
951 abr=0
952 if [[ ! $multiaudio ]]; then
953 get_abr
956 [[ $mpeg && ${encode%,*} == ?:0:? ]] && \
957 vbr=$(($(id_find ID_VIDEO_BITRATE "${MPLAYEROPT[@]}" ${dvddev:+-dvd-device "$dvddev"} "$@")/1000))
958 case $frameformat in
959 DVD) : ${asr:=48000} ;;
960 *) : ${asr:=44100} ;;
961 esac
962 case $videonorm in
963 p|s)
964 : ${vfr:=3}
967 [[ $frameformat != VCD && ! $vfr && $hispeed -eq 0 && ! $testmca ]] && vfr=1 && telecine=1
968 : ${vfr:=4}
970 esac
971 [[ $encode && ${encode%,*} != ?:0:? ]] && : ${vfr:=2}
972 if [[ ! $ofps ]]; then
973 case $vfr in
974 1) ofps=24000/1001 ;;
975 2) ofps=24 ;;
976 3) ofps=25 ;;
977 4) ofps=30000/1001 ;;
978 5) ofps=30 ;;
979 6) ofps=50 ;;
980 7) ofps=60000/1001 ;;
981 8) ofps=60 ;;
982 esac
984 if [[ $split && $split -eq 0 ]]; then
985 [[ $mpeg ]] && split= || split=800
988 ###############################################################################
989 #### get MPlayer version
990 ###############################################################################
991 mver=$(mencoder 2>/dev/null | awk '$1=="MEncoder"{print $2;exit}')
993 ###############################################################################
994 #### threads check
995 ###############################################################################
996 if [[ ! $cpu && -f /proc/cpuinfo ]]; then
997 cpu=$(grep -c ^processor /proc/cpuinfo)
998 #### if there are 2 logical cpu but 1 physical (hyperthreading)
999 #### use only one thread. With kernel 2.4.x it is more efficent.
1000 ((cpu==2)) && [[ $(uname -r | cut -f 1-2 -d .) = 2.4 ]] && \
1001 [[ $(grep ^siblings /proc/cpuinfo | uniq | wc -l) -eq 1 ]] && \
1002 cpu=$((cpu/$(grep ^siblings /proc/cpuinfo | uniq | awk 'END{print $NF}')))
1004 ((cpu<2)) && cpu=
1006 ###############################################################################
1007 #### -ao pcm arguments
1008 ###############################################################################
1009 PCMWAV=(pcm:waveheader:fast:file=%$((${#output}+4))%"$output".wav)
1010 PCMTMP=(pcm:waveheader:fast:file=%$((${#output}+4))%"$output".tmp)
1011 PCMNOWYUV=(pcm:nowaveheader:fast:file=/dev/fd/4)
1013 [[ $dvddev ]] && MPLAYEROPT=( "${MPLAYEROPT[@]}" -dvd-device "$dvddev" )
1015 ###############################################################################
1016 #### mplayer/mencoder/mjpegtools options
1017 ###############################################################################
1019 #### mencoder output suffix
1020 if [[ $encode ]]; then
1021 [[ $mpeg ]] && SUF=mpeg || SUF=avi
1024 #### MPLAYERINFO is used for [info]
1025 MPLAYERINFO=( "${MPLAYEROPT[@]}" )
1027 #### mf:// case
1028 if [[ $pictsrc ]]; then
1029 if [[ $slideaudio && $slideaudio != /dev/null ]]; then
1030 [[ $(id_find ID_AUDIO_RATE "$slideaudio") != $asr ]] && SRATE="-srate $asr -af-adv force=1" || SRATE=
1031 CLEAN[${#CLEAN[*]}]="$output".wav
1032 mplayer "$slideaudio" $SRATE -vo null -vc dummy -ao "${PCMWAV[@]}" $afm ${mpegchannels:+-channels $mpegchannels -af channels=$mpegchannels}
1033 MPLAYEROPT=( "${MPLAYEROPT[@]}" -fps 1/$(id_find ID_LENGTH "$output".wav) -audiofile "$output".wav )
1034 else
1035 if [[ $slideaudio == /dev/null ]]; then
1036 MPLAYEROPT=( "${MPLAYEROPT[@]}" -fps $slidefps )
1037 encode=0:${encode#?:}
1038 else
1039 MPLAYEROPT=( "${MPLAYEROPT[@]}" -fps $slidefps -audiofile /dev/zero -audio-demuxer 20 -rawaudio format=0x1:rate=48000 )
1044 #### MENCODERARG is used for mencoding, vobsub dumping
1045 MENCODERARG=( "${MPLAYEROPT[@]}" ${frames:+-frames $frames} )
1047 MENCODERARG=( "${MENCODERARG[@]}" "${MENCODEROPT[@]}" ${endpos:+-endpos $endpos} ${ofps:+-ofps $ofps} )
1048 if [[ $mpeg && $mpegchannels ]]; then
1049 MENCODERARG=( "${MENCODERARG[@]}" -channels $mpegchannels )
1050 af=$(echo "${af:--af }${af:+,}" | sed 's/channels=[^,]*,//')channels=$mpegchannels
1051 elif [[ $channels ]]; then
1052 MENCODERARG=( "${MENCODERARG[@]}" -channels $channels )
1053 af=$(echo "${af:--af }${af:+,}" | sed 's/channels=[^,]*,//')channels=$channels
1056 YUVSCALEROPT="-v 1 -n $videonorm ${scale:+-O $frameformat} $YUVSCALEROPT"
1058 MPEG2ENCOPT="${cpu:+-M $cpu }-v 1 -S ${split:-50000} -n $videonorm -F $vfr -s -r 16 ${telecine:+-p} $MPEG2ENCOPT"
1059 case $vfr in
1060 1|2) MPEG2ENCOPT="-g 6 -G ${GOP:-12} $MPEG2ENCOPT" ;;
1061 3|6) MPEG2ENCOPT="-g 6 -G ${GOP:-15} $MPEG2ENCOPT" ;;
1062 4|5|7|8) MPEG2ENCOPT="-g 9 -G ${GOP:-18} $MPEG2ENCOPT" ;;
1063 esac
1064 [[ $frameformat = VCD ]] && MPEG2ENCOPT="-R 2 $MPEG2ENCOPT"
1065 echo "$MPEG2ENCOPT" | grep -q -e '-K hi-res' && YUVSCALEROPT="-M BICUBIC $YUVSCALEROPT" && \
1066 MPEG2ENCOPT="-4 1 -2 1 $MPEG2ENCOPT" || MPEG2ENCOPT="-4 2 -2 1 $MPEG2ENCOPT"
1068 case $frameformat in
1069 VCD) MPGRES=${MPGRES:-1} ;;
1070 SVCD)
1071 case $frameres in
1072 1) MPGRES=${MPGRES:-3} ;;
1073 2) MPGRES=${MPGRES:-1} ;;
1074 esac
1076 DVD)
1077 case $frameres in
1078 1) MPGRES=${MPGRES:-7} ;;
1079 2) MPGRES=${MPGRES:-6} ;;
1080 3) MPGRES=${MPGRES:-3} ;;
1081 4) MPGRES=${MPGRES:-2} ;;
1082 5) MPGRES=${MPGRES:-1} ;;
1083 esac
1085 esac
1086 case $MPGRES in
1087 1) H_RES=352 ; [[ $videonorm = n ]] && V_RES=240 || V_RES=288 ;;
1088 2) H_RES=352 ; [[ $videonorm = n ]] && V_RES=480 || V_RES=576 ;;
1089 3) H_RES=480 ; [[ $videonorm = n ]] && V_RES=480 || V_RES=576 ;;
1090 4) H_RES=528 ; [[ $videonorm = n ]] && V_RES=480 || V_RES=576 ;;
1091 5) H_RES=544 ; [[ $videonorm = n ]] && V_RES=480 || V_RES=576 ;;
1092 6) H_RES=704 ; [[ $videonorm = n ]] && V_RES=480 || V_RES=576 ;;
1093 7) H_RES=720 ; [[ $videonorm = n ]] && V_RES=480 || V_RES=576 ;;
1094 esac
1095 case $frameformat in
1096 VCD)
1097 : ${vbr:=1152}
1098 MPEG2ENCOPT="-f 2 -b $vbr -V 46 -B $(((abr*audiostream*101875-vbr*2819+3980000)/100000)) $MPEG2ENCOPT"
1099 MPLEXOPT="-f 2 -V -b 46 -r $(((abr*audiostream+vbr)*4)) $MPLEXOPT"
1100 VCDIMAGEROPT="-t vcd2 $VCDIMAGEROPT"
1101 H_STILL=704
1102 [[ $videonorm = n ]] && V_STILL=480 || V_STILL=576
1103 ((MPGRES!=1)) && YUVSCALEROPT="-O SIZE_${H_RES}x${V_RES} ${YUVSCALEROPT/ -O $frameformat}"
1104 MAXBR=1394
1106 SVCD)
1107 : ${vbr:=2500}
1108 MPEG2ENCOPT="-f 5 -b $vbr -V 113 -B $(((abr*audiostream*101250+vbr*742+1665400)/100000)) $MPEG2ENCOPT"
1109 MPLEXOPT="-V -b 113 -r $(((abr*audiostream+vbr)*4)) $MPLEXOPT"
1110 if [[ $trick ]]; then
1111 MPLEXOPT="-f 2 $MPLEXOPT"
1112 VCDIMAGEROPT="-t vcd2 $VCDIMAGEROPT"
1113 else
1114 MPLEXOPT="-f 5 $MPLEXOPT"
1115 VCDIMAGEROPT="-t svcd $VCDIMAGEROPT"
1117 H_STILL=704
1118 [[ $videonorm = n ]] && V_STILL=480 || V_STILL=576
1119 ((frameres>1||MPGRES!=3)) && YUVSCALEROPT="-O SIZE_${H_RES}x${V_RES} ${YUVSCALEROPT/ -O $frameformat}"
1120 MAXBR=2778
1122 DVD)
1123 : ${vbr:=7500}
1124 MPEG2ENCOPT="-f 8 -b $vbr -V 230 -B $(((abr*audiostream*101250+vbr*742+1665400)/100000)) $MPEG2ENCOPT"
1125 MPLEXOPT="-f 8 -V -b 230 -r $(((abr*audiostream+vbr)*4)) $MPLEXOPT"
1126 H_STILL=720
1127 [[ $videonorm = n ]] && V_STILL=480 || V_STILL=576
1128 ((frameres>1||MPGRES!=7)) && YUVSCALEROPT="-O SIZE_${H_RES}x${V_RES} ${YUVSCALEROPT/ -O $frameformat}"
1129 MAXBR=10080
1131 esac
1133 ###############################################################################
1134 #### mencoder audio/video/pass options
1135 ###############################################################################
1136 if [[ $encode ]]; then
1137 OPTIONS="-noskiplimit -sws $((hispeed?1:7))"
1138 if [[ $mpeg ]]; then
1139 # mencoder can put in the MPEG container:
1140 # video: mpeg1, mpeg2, mpeg4
1141 # audio: mp1, mp2, mp3, ac3, aac (not yet: lpcm, dts)
1142 [[ ${encode%,*} != ?:0:? ]] && : ${mpegaspect:=2}
1143 #MUX="-mpegopts ${mpegaspect:+vaspect=${ASPECT[mpegaspect]}:}:vbitrate=${vbr}"
1144 MUX="-mpegopts "
1145 if [[ $telecine ]]; then
1146 if [[ $vcodec = mpeg2video || ! $vcodec && $frameformat != VCD ]]; then
1147 if [[ $vfr == [12] ]]; then
1148 [[ $videonorm = n ]] && MUX2=":telecine" || MUX2=":film2pal"
1149 else
1150 do_log "++ WARN: [$PROGNAME] telecine only works with 24000/1001 or 24 fps, disabling it"
1152 else
1153 do_log "++ WARN: [$PROGNAME] telecine only works for MPEG-2, disabling it"
1156 case $frameformat in
1157 VCD) MUX="${MUX}format=xvcd" LAVC="vcodec=${vcodec:-mpeg1video}${LAVC:-:vrc_buf_size=327:vrc_minrate=${vbr}:vrc_maxrate=${vbr}}" ;;
1158 SVCD) if [[ $trick ]]; then MUX="${MUX}format=xvcd" ; else
1159 MUX="${MUX}format=xsvcd" ; fi ; LAVC="vcodec=${vcodec:-mpeg2video}:vrc_buf_size=917" ;;
1160 DVD) MUX="${MUX}format=dvd" LAVC="vcodec=${vcodec:-mpeg2video}:vrc_buf_size=1835" ;;
1161 esac
1162 case $vfr in
1163 1|2) LAVC="$LAVC:keyint=$((hispeed?1:${GOP:-12}))" ;;
1164 3|6) LAVC="$LAVC:keyint=$((hispeed?1:${GOP:-15}))" ;;
1165 4|5|7|8) LAVC="$LAVC:keyint=$((hispeed?1:${GOP:-18}))" ;;
1166 esac
1167 [[ $frameformat = VCD && ! $bframes ]] && LAVC="$LAVC:vmax_b_frames=2"
1168 [[ $mpegmbr ]] && [[ $mpegmbr -lt $vbr ]] && mpegmbr=$vbr
1169 #### -a 1 is SAR=1 (do not scale), not DAR=1
1170 [[ $mpegaspect != 1 ]] && LAVC="${LAVC}:aspect=${ASPECT[mpegaspect]}"
1171 LAVC="${LAVC}${mpegmbr:+:vrc_maxrate=$mpegmbr}${inter_matrix:+:inter_matrix=${inter_matrix}}${intra_matrix:+:intra_matrix=${intra_matrix}}"
1172 OF="-of mpeg"
1173 NOSKIP=-noskip
1174 vbitrate=$vbr
1175 if [[ ${encode%,*} != ?:0:? ]]; then
1176 for ((a=0;a<${#MENCODERARG[*]};a++)); do
1177 while [[ ${MENCODERARG[a]} = -vf ]]; do
1178 unset MENCODERARG[a] MENCODERARG[a+1]
1179 MENCODERARG=( "${MENCODERARG[@]}" )
1180 done
1181 done
1182 MENCODERARG=( "${MENCODERARG[@]}" ${vf:--vf }${vf:+,}${scale:+scale=${H_RES}:${V_RES}${interlaced:+:1},}harddup )
1186 BASIC="$LAVC:vbitrate=${vbitrate}${cpu:+:threads=$cpu}${bframes:+:vmax_b_frames=$bframes}"
1187 ((hispeed)) && BASIC="$BASIC:vme=0" || BASIC="$BASIC:psnr"
1188 [[ $mpeg && $frameformat = VCD ]] || BASIC="$BASIC${interlaced:+:ildct:ilme}"
1189 echo "$YUVSCALEROPT" | grep -q -e '-O MONOCHROME' && BASIC="$BASIC:gray"
1191 HQ="${BASIC}:mbd=2"
1193 if [[ $mpeg ]]; then
1194 #### dia=6 could be added
1195 BESTSIZE="${BASIC}:mbd=1:loop:mv0:vlelim=-4:vcelim=7:trell:precmp=1:cmp=1:subcmp=1"
1196 [[ ! $bframes ]] && BESTSIZE="$BESTSIZE:vmax_b_frames=2"
1198 #### last_pred=1-2 could be added
1199 BESTQUALITY="${BASIC}:mbd=2:mv0:precmp=6:cmp=6:subcmp=6"
1200 [[ $vbitrate -ge 3500 && $frameformat != VCD && $vcodec != mpeg1video ]] && BESTQUALITY="$BESTQUALITY:dc=9"
1203 case $encode in
1204 0:?:?|0:?:?,*) AUDIOPASS="-oac copy" ;;
1205 1:?:?|1:?:?,*) AUDIOPASS="-oac pcm" ;;
1206 2:?:?|2:?:?,*) AUDIOPASS="-oac mp3lame -lameopts ${acustom:-fast}" ;;
1207 3:?:?) AUDIOPASS="-oac mp3lame -lameopts preset=standard" ;;
1208 4:?:?) AUDIOPASS="-oac lavc -lavcopts acodec=mp2:abitrate=$abr" ;;
1209 5:?:?) AUDIOPASS="-oac lavc -lavcopts acodec=mp3:abitrate=$abr" ;;
1210 6:?:?) AUDIOPASS="-oac lavc -lavcopts acodec=ac3:abitrate=$abr" ;;
1211 7:?:?) AUDIOPASS="-oac $TOOLAME -${TOOLAME}opts br=$abr" ;;
1212 8:?:?) AUDIOPASS="-oac faac -faacopts ${acustom:-br=$abr}" ;;
1213 3:?:?,*) AUDIOPASS="-oac mp3lame -lameopts preset=${encode#*,}" ;;
1214 4:?:?,*) AUDIOPASS="-oac lavc -lavcopts acodec=mp2:abitrate=${encode#*,}" ;;
1215 5:?:?,*) AUDIOPASS="-oac lavc -lavcopts acodec=mp3:abitrate=${encode#*,}" ;;
1216 6:?:?,*) AUDIOPASS="-oac lavc -lavcopts acodec=ac3:abitrate=${encode#*,}" ;;
1217 7:?:?,*) AUDIOPASS="-oac $TOOLAME -${TOOLAME}opts br=${encode#*,}" ;;
1218 8:?:?,*) AUDIOPASS="-oac faac -faacopts ${acustom:-br=${encode#*,}}" ;;
1219 esac
1220 encode=${encode%,*}
1221 case $encode in
1222 ?:0:?) VIDEOPASS="$OF $MUX -ovc copy $NOSKIP" ; encode=${encode%%:*}:0:1 ; turbo=0 ;; # copy uses only one pass
1223 ?:1:?) VIDEOPASS="$OF $MUX$MUX2 -ovc lavc -lavcopts ${BASIC}${vcustom:+:$vcustom}" ; : ${turbo:=0} ;;
1224 ?:2:?) VIDEOPASS="$OF $MUX$MUX2 -ovc lavc -lavcopts ${HQ}" ; : ${turbo:=1} ;;
1225 ?:3:?) VIDEOPASS="$OF $MUX$MUX2 -ovc lavc -lavcopts ${BESTSIZE}" ; : ${turbo:=1} ;;
1226 ?:4:?) VIDEOPASS="$OF $MUX$MUX2 -ovc lavc -lavcopts ${BESTQUALITY}" ; : ${turbo:=1} ;;
1227 esac
1228 ((turbo)) && turbo=:turbo || turbo=
1229 PASS=${encode##*:}
1230 [[ $normalize && $encode != 0:?:? ]] && af=${af:--af }${af:+,}volnorm
1234 ###############################################################################
1235 #### more functions
1236 ###############################################################################
1237 status_bit () {
1238 local a
1239 case $1 in
1240 avi) bit=0 ;;
1241 mpv) bit=1 ;;
1242 mpa) bit=2 ;;
1243 mpg) bit=3 ;;
1244 img) bit=4 ;;
1245 sub) bit=5 ;;
1246 ach) bit=6 ;;
1247 fno) bit=7 ;;
1248 ch0) bit=8 ;;
1249 sbm) bit=9 ;;
1250 spl) bit=10 ;;
1251 esac
1252 if [[ $2 = set ]]; then
1253 skip=$((skip|1<<bit))
1254 else
1255 return $(((skip&1<<bit) && 1))
1258 ###############################################################################
1259 file_size () { #fsize,fint,ffrac,fpre
1260 local -a PRE=([1]=K M G T)
1261 local i=0
1262 fsize=$(ls -l "$1" | awk '{print $5}')
1263 fint=$fsize
1264 ffrac=0
1265 while ((fint>=1024)) ; do
1266 ((fint/1024 < 1024)) && ffrac=$((( fint+=51 )%1024))
1267 i=$((++i))
1268 fint=$((fint/1024))
1269 done
1270 ffrac=$((ffrac*10/1024))
1271 fpre=${PRE[i]}
1273 ###############################################################################
1274 show_file_info () {
1275 file_size "$2"
1276 echo " $1: $2 is $fsize bytes, $fint.$ffrac ${fpre}B" >>"$output".log
1278 ###############################################################################
1279 show_finalvideo_info () {
1280 local codec TYPE i VIDEO OUT ASPECT CH FAAC SUBST
1281 SUBST=
1282 VIDEO="$(mplayer -nocache -frames 0 -vo null -nosound "$2" 2>/dev/null | grep "^VIDEO:")"
1283 if [[ ! $VIDEO ]]; then
1284 # MPEGs with MPEG-4 video do not show all the video informations in one line,
1285 # assebmling the informations (kbps is missing):
1286 OUT="$(mplayer -nocache -frames 1 -vo null -nosound -v "$2" 2>/dev/null)"
1287 if echo "$OUT" | grep -q '^\[V].*fourcc:0x10000004' ; then
1288 VIDEO="VIDEO: MPEG-4 $(echo "$OUT" | awk '$1=="VO:"&&$2=="[null]"{print $3}')"
1289 ASPECT=$(echo "$OUT" | awk '$1=="Movie-Aspect"{print $3}')
1290 case $ASPECT in
1291 undefined) VIDEO="$VIDEO (aspect 1)" ;;
1292 1.33:1) VIDEO="$VIDEO (aspect 2)" ;;
1293 1.78:1) VIDEO="$VIDEO (aspect 3)" ;;
1294 2.21:1) VIDEO="$VIDEO (aspect 4)" ;;
1295 *) VIDEO="$VIDEO (aspect $ASPECT)" ;;
1296 esac
1297 VIDEO="$VIDEO $(echo "$OUT" | awk '$1=="[V]"{print $5}')"
1300 echo " $1: $VIDEO" >>"$output".log
1301 #### removed -vc dummy
1302 for i in $(mplayer -vo null -ao null -nocache -frames 1 -v "$2" 2>/dev/null | awk '/==> Found audio str/{print $NF}' | sort -n) ; do
1303 echo -n " $1: AUDIO[$i]: " >>"$output".log
1304 codec=$(mplayer -ac mp3, -nocache -frames 0 -v -ao null -vo null "$2" -aid $i 2>/dev/null | \
1305 sed '/Selected audio codec:/!d;s/[^(]*(//;s/).*//;s/AAC.*/& AAC/;s/.* //')
1306 #### AAC info are insufficient/incorrect
1307 if [[ $codec = AAC ]]; then
1308 CH=$(mplayer -nocache -frames 0 -v -ao null -vo null "$2" -aid $i 2>/dev/null | awk '/FAAD: Negotiated samplerate:/{print $NF}')
1309 if type &>/dev/null faad ; then
1310 mplayer -dumpaudio -dumpfile "$output".audio -aid $i "$2" 2>/dev/null
1311 FAAC=$(faad -i "$output".audio 2>&1 | tail -2 | head -n 1)
1312 #ADTS, 4.087 sec, 103 kbps, 44100 Hz
1313 rm -f "$output".audio
1314 SUBST="s/AAC.*/AAC:$(echo "$FAAC" | sed 's/.*,\([^,]*Hz\).*/\1/'), "
1315 SUBST="${SUBST}$CH ch, $(echo "$FAAC" | sed 's/,.*//'),"
1316 SUBST="${SUBST}$(echo "$FAAC" | sed 's/.*,\([^,]*kbps\).*/\1/')"
1317 SUBST="${SUBST}/;"
1318 else
1319 SUBST="s/2 ch,/$CH ch,/;"
1322 mplayer -ac mp3, -nocache -frames 0 -v -ao null -vo null "$2" -aid $i 2>/dev/null | sed '/^Opening audio decode/,/^AUDIO:/!d;s/\r//g' | \
1323 grep -e '^AC3:' -e '^MPEG' -e '^AUDIO:' | sed 's/AUDIO/'"$codec"'/;'"$SUBST"'q' >>"$output".log
1324 done
1325 TYPE=$1
1326 shift
1327 for i ; do
1328 show_file_info "$TYPE" "$i"
1329 done
1331 ###############################################################################
1332 video_duration () {
1333 mencoder "$@" -ovc copy -nosound -o /dev/null -quiet 2>&1 | awk '/^Video stream:/{print $10+$10/$12}'
1335 ###############################################################################
1336 job_exit () {
1337 EXIT=$?
1338 status_bit mpg || [[ ! -f ${output}.mpg && ! -f ${output}01.mpg ]] || show_finalvideo_info "MPEG" "${output}"*.mpg
1339 [[ $usesbr ]] && ((DEBUG && usesbr>6)) && awk -v u=$usesbr -v f=$fsize \
1340 'BEGIN{b=u*1024*1024;printf("--DEBUG: [usesbr] target: %dMB (%d bytes), error: %f%%\n",u,b,(b-f)*100/b)}' >>"$output".log
1341 sec=$(($(pr_time)-STARTTIME))
1342 echo " JOBEND: $output $(pr_date) ($((sec/3600))h$((sec/60%60))m$((sec%60))s)" >>"$output".log
1343 rm -f "${CLEAN[@]}" psnr_??????.log
1344 ((DEBUG)) && exec >&1- >&2- && exec >&3
1345 if [[ -f $output.yuvscaler.log || -f $output.mpeg2enc.log ]]; then
1346 echo
1347 else
1348 ((!EXIT)) && [[ ! $quiet ]] && cat "$output".log
1350 ((DEBUG)) && rm -f "$output".debug.fifo && kill $PROCTEE
1352 ###############################################################################
1353 me_log () {
1354 rm -f "$output".fifo
1355 mkfifo "$output".fifo
1356 tee -a /dev/stderr <"$output".fifo | sed 's/.*\r//' | \
1357 awk '/^PSNR:|^M[EP][ln]|^There are |^\[open]|^audio stream:|^number of|^subtitle|^==> |^Recommended video bitrate/{sub(/^/," INFO: [mencoder] ");
1358 print}' >>"$output".log &
1360 ###############################################################################
1361 me_bit_log () {
1362 rm -f "$output".fifo
1363 mkfifo "$output".fifo
1364 tee -a /dev/stderr <"$output".fifo | sed 's/.*\r//;/^Recommended video bitrate/!d;s/^/ INFO: [mencoder] /' >>"$output".log &
1366 ###############################################################################
1367 check_abr () {
1368 # abr permitted:
1369 # ac3: ( 8000/11025/12000) 8 16 24 32 40 48 56 64 80 96 112 128 144 160
1370 # ac3: (16000/22050/24000) 16 24 32 40 48 56 64 80 96 112 128 160 192 224 256 288 320
1371 # ac3: (32000/44100/48000) 32 40 48 56 64 80 96 112 128 160 192 224 256 320 384 448 512 576 640
1372 # mp3: ( 8000/11025/12000) 8 16 24 32 40 48 56 64 80 96 112 128 144 160
1373 # mp3: (16000/22050/24000) 8 16 24 32 40 48 56 64 80 96 112 128 144 160
1374 # mp3: (32000/44100/48000) 32 40 48 56 64 80 96 112 128 160 192 224 256 320
1375 # mp2: ( 8000/11025/12000)
1376 # mp2: (16000/22050/24000) 8 16 24 32 40 48 56 64 80 96 112 128 144 160
1377 # mp2: (32000/44100/48000) 32 48 56 64 80 96 112 128 160 192 224 256 320 384
1378 # mp1: ( 8000/11025/12000)
1379 # mp1: (16000/22050/24000) 32 48 56 64 80 96 112 128 144 160 176 192 224 256
1380 # mp1: (32000/44100/48000) 32 64 96 128 160 192 224 256 288 320 352 384 416 448
1381 case $1 in
1382 ac3)
1383 case $2 in
1384 8000|11025|12000)
1385 case $3 in
1386 8|16|24|32|40|48|56|64|80|96|112|128|144|160) : ;;
1387 *) return 1 ;;
1388 esac
1390 16000|22050|24000)
1391 case $3 in
1392 16|24|32|40|48|56|64|80|96|112|128|160|192|224|256|288|320) : ;;
1393 *) return 1 ;;
1394 esac
1396 32000|44100|48000)
1397 case $3 in
1398 32|40|48|56|64|80|96|112|128|160|192|224|256|320|384|448|512|576|640) : ;;
1399 *) return 1 ;;
1400 esac
1402 *) return 1 ;;
1403 esac
1405 mp3)
1406 case $2 in
1407 8000|11025|12000|16000|22050|24000)
1408 case $3 in
1409 8|16|24|32|40|48|56|64|80|96|112|128|144|160) : ;;
1410 *) return 1 ;;
1411 esac
1413 32000|44100|48000)
1414 case $3 in
1415 32|40|48|56|64|80|96|112|128|160|192|224|256|320) : ;;
1416 *) return 1 ;;
1417 esac
1419 *) return 1 ;;
1420 esac
1422 mp2)
1423 case $2 in
1424 16000|22050|24000)
1425 case $3 in
1426 8|16|24|32|40|48|56|64|80|96|112|128|144|160) : ;;
1427 *) return 1 ;;
1428 esac
1430 32000|44100|48000)
1431 case $3 in
1432 32|48|56|64|80|96|112|128|160|192|224|256|320|384) : ;;
1433 *) return 1 ;;
1434 esac
1436 *) return 1 ;;
1437 esac
1439 mp1)
1440 case $2 in
1441 16000|22050|24000)
1442 case $3 in
1443 32|48|56|64|80|96|112|128|144|160|176|192|224|256) : ;;
1444 *) return 1 ;;
1445 esac
1447 32000|44100|48000)
1448 case $3 in
1449 32|64|96|128|160|192|224|256|288|320|352|384|416|448) : ;;
1450 *) return 1 ;;
1451 esac
1453 *) return 1 ;;
1454 esac
1456 *) return 1 ;;
1457 esac
1459 ###############################################################################
1460 debug_line () {
1461 echo "--DEBUG: [$PROGNAME] $2($1) $(eval echo $(sed -n $1p "$PROGFILE" | sed 's/ |.*//;s/.>.*//;s/</\\\</'))" >>"$output".log
1463 ###############################################################################
1464 error_line () {
1465 echo "--DEBUG: [$PROGNAME] $1"
1466 echo "**ERROR: [$PROGNAME] there has been an error during the execution of the previous line, this should not happen"
1467 echo "possible causes:"
1468 echo " 0) missing or misspelled input stream"
1469 echo " 1) the input stream is corrupted"
1470 echo " -> try a different input stream"
1471 echo " 2) one of the options used has triggered a bug present only on your combination of architecture/compiler/distribution"
1472 echo " -> try to recompile MPlayer with a different compiler version or try another distribution"
1473 if ((SVN)); then
1474 echo " 3) one of the options used is not valid or is buggy for your SVN/unsupported version of MPlayer/MEncoder"
1475 echo " -> check MPlayer's man page and/or try a supported release of MPlayer"
1477 echo
1478 echo "submit a bugreport if you think this is a bug in $PROGNAME"
1479 exit $ret
1481 ###############################################################################
1482 check_mencoder_abr () {
1483 local codec lib ASR
1484 codec=([4]=mp2 mp3 ac3 mp2 aac)
1485 lib=([4]=libavcodec libavcodec libavcodec lib${TOOLAME} libfaac)
1486 ASR=${encode%%:*}
1487 check_abr ${codec[ASR]} $1 $2 || ! echo "**ERROR: [$PROGNAME] ${lib[ASR]} does not support $2 kbps / $1 Hz for ${codec[ASR]}" || exit 1
1489 ###############################################################################
1490 is_film2pal () {
1491 local a
1492 a=$(mplayer -nocache "$@" -vo null -nosound -benchmark -frames 60 -noquiet 2>/dev/null | tr '\015' '\012' | tail | \
1493 awk -F/ '$1~/^V:/{s=$1;t=$2}END{match(s,/ [0-9]+$/);n=substr(s,RSTART+1);match(t,/[0-9]+ /);m=substr(t,1,RLENGTH-1);r=n/m;if(r>1.02)print "24"}')
1494 [[ $a ]] && a=$(mplayer -nocache "$@" -vo null -nosound -benchmark -frames 1500 -noquiet 2>/dev/null | tr '\015' '\012' | tail | \
1495 awk -F/ '$1~/^V:/{s=$1;t=$2}END{match(s,/ [0-9]+$/);n=substr(s,RSTART+1);match(t,/[0-9]+ /);m=substr(t,1,RLENGTH-1);r=n/m;
1496 if(r>1.042)print "24000/1001fps";if(r<1.042&&r>1.041)print "24fps"}')
1497 echo "$a"
1500 ###############################################################################
1501 #### ERROR if some options conflict is detected part 2/2
1502 ###############################################################################
1503 #### libavcodec codec/asr/abr
1504 #### libtoolame asr/abr
1505 #### libmp3lame asr
1506 #### no check is done on the other channel in case of multiaudio
1507 if [[ $encode == [2-8]:?:? ]]; then
1508 if [[ $srate ]]; then
1509 r=$srate
1510 else
1511 r=$(id_find ID_AUDIO_RATE "${MPLAYERINFO[@]}")
1512 if [[ ! $r ]]; then
1513 do_log "++ WARN: [$PROGNAME] failure to detect the audio sample rate of the input stream"
1514 [[ ! $multiaudio && ! $audioid ]] && do_log "++ WARN: [$PROGNAME] if your source video does not have audio use -encode 0:${encode#*:}" || \
1515 do_log "++ WARN: [$PROGNAME] probably is incorrect the audio stream selected with -${audioid:+aid}${multiaudio:+multiaudio}"
1517 if [[ $mpeg && ! $usespeed ]]; then
1518 case $frameformat in
1519 *VCD) ((r != 44100)) && do_log "++ WARN: [$PROGNAME] $frameformat standard requires 44100kHz audio, add -srate 44100" ;;
1520 DVD) ((r != 48000)) && do_log "++ WARN: [$PROGNAME] $frameformat standard requires 48000kHz audio, add -srate 48000" ;;
1521 esac
1524 if [[ $encode == [4-7]:?:? ]]; then
1525 check_mencoder_abr "$r" ${AUDIOPASS##*=}
1526 elif [[ $encode == 8:?:? ]]; then
1527 case $r in
1528 8000|11025|12000|16000|22050|24000|32000|44100|48000|64000|88200|96000) : ;;
1529 *) echo "**ERROR: [$PROGNAME] libfaac does not support $r Hz sample rate" ; exit 1 ;;
1530 esac
1531 else
1532 case $r in
1533 8000|11025|12000|16000|22050|24000|32000|44100|48000) : ;;
1534 *) echo "**ERROR: [$PROGNAME] libmp3lame does not support $r Hz sample rate" ; exit 1 ;;
1535 esac
1538 #### copy of non-MPEG audio in a VCD
1539 if [[ $step -gt 1 && $frameformat = VCD && $encode == 0:?:? && ( $mpeg || ${!audioformat} = copy ) && ! $testmca && ! $pictsrc ]]; then
1540 a=$(id_find ID_AUDIO_CODEC "${MPLAYERINFO[@]}")
1541 [[ $a != mp3 ]] && echo "**ERROR: [$PROGNAME] you cannot copy $a audio in a $frameformat" && exit 1
1543 #### mpegchannels > 2 only with ac3 and aac
1544 [[ $mpeg && ${mpegchannels:-2} -gt 2 && $encode == [2-57]:?:? ]] && CODEC=([2]=mp3 mp3 mp2 mp3 [7]=mp2) && \
1545 echo "**ERROR: [$PROGNAME] audio codec ${CODEC[${encode%%:*}]} selected with -encode $encode do not support more than 2 audio channels" && exit 1
1546 ###############################################################################
1547 #### set cleanup
1548 ###############################################################################
1549 trap 'job_exit' 0
1550 CLEAN[${#CLEAN[*]}]="$output".fifo
1552 skip=0
1553 LG=1
1555 ###############################################################################
1556 #### start the log file
1557 ###############################################################################
1558 if [[ ! $resume ]] ; then
1559 { if [[ $LOG ]]; then
1560 echo "$LOG"
1561 else
1562 STARTTIME=$(pr_time)
1563 echo "### LOG: $output $(pr_date)"
1565 echo -n " INFO: [$PROGNAME] version $VERSION running in "
1566 if ((${#TITLESET[*]})); then
1567 echo -n "Titleset Mode"
1568 else
1569 if [[ $mpeg ]]; then
1570 [[ $testmca ]] && echo -n "Testmca Mode" || echo -n "MPEG Mode"
1573 echo "${cpu:+ (cpu=$cpu)}"
1574 echo " INFO: [$PROGNAME] command line: '${CMD[@]}'"
1575 } >"$output".log
1578 ###############################################################################
1579 #### WARNING if some requested tools are missing and can be replaced
1580 ###############################################################################
1581 #### WARN
1582 if [[ $WARN ]]; then
1583 echo -e "$WARN" >>"$output".log
1585 #### volume and audio copy
1586 if [[ $volume ]]; then
1587 [[ $encode == 0:?:? && ( ${!audioformat} = copy || $step -eq 1 || $mpeg ) || ${!audioformat} = copy && ! $encode ]] && \
1588 do_log "++ WARN: [$PROGNAME] you cannot modify the volume of the output audio stream if you are making a copy the input audio stream"
1590 #### cpu and bframes
1591 if [[ $cpu && $bframes ]]; then
1592 ((bframes)) && do_log "++ WARN: [$PROGNAME] with bframes>0 the encoding will be faster with cpu=1"
1594 #### -usespeed
1595 if [[ $usespeed && ( $encode == 0:?:? || $encode == ?:0:? ) ]]; then
1596 do_log "++ WARN: [$PROGNAME] -usespeed may not work if you do not encode both audio and video." && echo -n "Press return to proceed" && read
1598 #### total br
1599 [[ $encode != ?:0:? ]] && ((step>1&&abr*audiostream*1024/1000+vbr>MAXBR)) && \
1600 do_log "++ WARN: [$PROGNAME] total video+audio bitrate ($vbr+$((abr*audiostream*1024/1000))kbps) exceed $frameformat specifications (${MAXBR}kbps)"
1601 #### -slideaudio/single picture
1602 if [[ $slideaudio && $slideaudio != /dev/null && $pictsrc ]]; then
1603 ((${#PICTSRC[*]}!=1||$(ls ${PICTSRC[0]} | wc -l)!=1)) && \
1604 do_log "++ WARN: [$PROGNAME] you should use only one source image if you use the option -slideaudio"
1607 ###############################################################################
1608 #### dump some info in the log file
1609 ###############################################################################
1610 { VARS=(frameformat ${split:+split} vfr vbr abr asr ${mpegchannels:+mpegchannels} ${GOP:+GOP} audioformat) # audioformat must be the last
1611 VARS[${#VARS[*]}]=${!VARS[${#VARS[*]}-1]}
1612 [[ $volume && ! $encode ]] && VARS[${#VARS[*]}]=volume
1613 echo -n " MPEG: "
1614 for ((i=0;i<${#VARS[*]};i++)) ; do
1615 echo -n "${VARS[i]}:${!VARS[i]} "
1616 done
1617 echo -n "${multiaudio:+multiaudio:${multiaudio// /,} }"
1618 echo -n "mpegencoder:$([ $mpeg ] && echo mencoder || echo mpeg2enc)"
1619 [[ $mpegaspect ]] && echo -n " aspect:${ASPECT[mpegaspect]}"
1620 [[ $cdi && $frameformat = VCD ]] && echo -n " CD-i"
1621 [[ $telecine ]] && echo -n " telecine"
1622 echo
1623 VARS=(encode ${vcodec:+vcodec} ${volume:+volume} ${usesbr:+usesbr} ${avisplit:+avisplit} ${channels:+channels} AUDIOPASS VIDEOPASS PASS)
1624 if [[ $encode ]]; then
1625 echo -n " $([ $mpeg ] && echo MPEG || echo \ AVI): "
1626 for ((i=0;i<${#VARS[*]};i++)) ; do
1627 echo -n "${VARS[i]}:${!VARS[i]} "
1628 done
1629 if ((PASS>1)); then
1630 [[ $turbo ]] && echo -n "TURBO:on " || echo -n "TURBO:off "
1632 txt=
1633 [[ $(echo $encode | cut -f 2 -d:) = 1 && $vcustom ]] && txt="libavcodec"
1634 [[ ${encode%%:*} = 2 && $acustom ]] && txt=${txt:+${txt} and} && txt="$txt lame"
1635 txt=${txt:+(custom ${txt} options)}
1636 echo $txt
1638 [[ ! $resume ]] && { [[ $audioonly ]] && mp_identify "$audioonly" || mp_identify "${MPLAYERINFO[@]}" -frames 1 ; } | \
1639 sed -n '/^ID_/s/^/ INFO: [identify] /p' | uniq
1640 VARS=(${encode:+MENCODERARG} MPLAYERYUVOPT)
1641 VARS=(${encode:+MENCODERARG})
1642 for ((i=0;i<${#VARS[*]};i++)) ; do
1643 s="INFO: \[${VARS[i]}] \${${VARS[i]}[*]}"
1644 eval echo "\ \ \ $s"
1645 done
1646 } >>"$output".log
1647 h_res=$(grep ID_VIDEO_WIDTH "$output".log | tail -1 | cut -f2 -d=)
1648 v_res=$(grep ID_VIDEO_HEIGHT "$output".log | tail -1 | cut -f2 -d=)
1649 if [[ $pictsrc ]]; then
1650 v_res=$(mplayer -vo null "${MPLAYERINFO[@]}" -frames 1 2>/dev/null | awk '/^VO:/{print $3}' | head -n 1)
1651 h_res=${v_res%x*}
1652 v_res=${v_res#*x}
1654 [[ $mpeg && ${encode%,*} == ?:0:? ]] && H_RES=$h_res && V_RES=$v_res
1656 ###############################################################################
1657 #### put the volume in DB
1658 ###############################################################################
1659 if [[ $volume ]]; then
1660 volume=$(awk -v a=$volume 'BEGIN{if(a>0) print 20*log(a)/log(10) ; else print 0}')
1661 af=${af:--af }${af:+,}volume=$volume
1664 [[ ${mver:0:5} = 1.0rc ]] || SVN=1
1666 ###############################################################################
1667 #### telecined (NTSC/PAL) MPEG copy/speed encoding change
1668 ###############################################################################
1669 if [[ $mpeg && ( $encode == ?:0:? || $usespeed ) || $usespeed && ! $encode && $step -gt 1 ]]; then
1670 FPS=($(grep ID_VIDEO_FPS "$output".log | cut -f2 -d=) [1]=23.976 24.000 25.000 29.970 30.000 50.000 59.940 60.000)
1671 for ((i=1;i<9;i++)); do
1672 a=$(awk -v a=${FPS[0]} -v b=${FPS[i]} 'BEGIN{if (sqrt((a-b)*(a-b))<.02) print b}')
1673 if [[ $a ]]; then
1674 if [[ ${FPS[0]} != ${FPS[i]} ]]; then
1675 do_log "++ WARN: [$PROGNAME] input video frame rate is not exactly ${FPS[i]}fps"
1676 FPS[0]=${FPS[i]}
1678 FPS[10]=$i
1679 break
1681 done
1682 [[ $usespeed && $i -eq 9 ]] && do_log "++ WARN: [$PROGNAME] input video frame rate is not a valid NTSC/PAL value; disabling -usespeed" && usespeed=
1684 if [[ ${FPS[0]} = ${FPS[4]} || ${FPS[0]} = ${FPS[5]} ]]; then
1685 FPS[9]=$(mplayer -nocache -quiet "${MPLAYERINFO[@]}" -vo null -nosound -benchmark -frames 60 2>/dev/null | awk '/^demux_mpg:/{print $2}')
1686 [[ ${FPS[9]} = 24fps || ${FPS[9]} = 24000/1001fps ]] && FPS[10]=$((FPS[10]-3))
1687 elif [[ ${FPS[0]} = ${FPS[3]} ]]; then
1688 FPS[9]=$(is_film2pal "${MPLAYERINFO[@]}")
1689 if [[ ${FPS[9]} ]]; then
1690 [[ ${FPS[9]} = 24fps ]] && FPS[10]=2 || FPS[10]=1
1693 if [[ $usespeed ]]; then
1694 if ((vfr!=${FPS[10]})); then
1695 NSPEEDCOEF=([1]=1001 1001 5 1001 1001 5 1001 25 1250 5 25 2500 5 1200 6 2 2400 12 1001 1001 2 1001 5 2000 2 1200 6 1001 )
1696 MSPEEDCOEF=([1]=1000 960 4 800 480 2 400 24 1001 4 12 1001 2 1001 5 1 1001 5 1000 200 1 500 3 1001 1 1001 5 1000 )
1697 DCOEF=([1]=-2 3 7 10 12 13 13)
1698 if ((vfr>${FPS[10]})); then
1699 #### black magic here ;-)
1700 n=$((vfr+${FPS[10]}+${DCOEF[${FPS[10]}]}))
1701 a=${NSPEEDCOEF[n]}/${MSPEEDCOEF[n]}
1702 else
1703 n=$((vfr+${FPS[10]}+${DCOEF[vfr]}))
1704 a=${MSPEEDCOEF[n]}/${NSPEEDCOEF[n]}
1706 MENCODERARG=( -speed $a -srate $asr -af-adv force=1 "${MENCODERARG[@]}" )
1707 MPLAYERYUVOPT=("${MPLAYERYUVOPT[@]}" -speed $a )
1708 do_log " INFO: [usespeed] using speed factor $a"
1710 elif [[ ${FPS[0]} = ${FPS[4]} || ${FPS[0]} = ${FPS[5]} || ${FPS[0]} = ${FPS[3]} ]]; then
1711 if [[ ${FPS[9]} = 24fps || ${FPS[9]} = 24000/1001fps || $telesrc ]]; then
1712 { echo -n " INFO: [$PROGNAME] "
1713 [[ ${FPS[9]} = 24fps || ${FPS[9]} = 24000/1001fps ]] && echo -n "detected" || echo -n "user selected"
1714 [[ ${FPS[0]} = ${FPS[3]} ]] && echo -n " PAL" || echo -n " NTSC"
1715 echo " telecined source"
1716 } | tee -a "$output".log
1717 [[ ${FPS[10]} = 1 ]] && MENCODERARG=( "${MENCODERARG[@]}" -ofps 24000/1001 -mc 0 ) || MENCODERARG=( "${MENCODERARG[@]}" -ofps 24 -mc 0 )
1722 ###############################################################################
1723 #### scale and expand/crop to adapt the aspect ratio
1724 #### added rotation and overscan
1725 ###############################################################################
1726 if [[ $mpegfixaspect && $step -gt 1 ]]; then
1727 a=$(get_aspect "${MPLAYERINFO[@]}")
1728 [[ ${a:0:9} = undefined ]] && a=$(awk -v a=$h_res -v b=$v_res 'BEGIN{printf("%f",a/b)}')
1729 [[ $mpegaspect == 1 ]] && b=$(awk -v a=$H_RES -v b=$V_RES 'BEGIN{printf("%f",a/b)}') || b=${ASPECT[${mpegaspect:-2}]}
1730 vfilter=$(awk -v a=$a -v A=$b -v W=$H_RES -v H=$V_RES -v crop=$mpegfixaspect -v i=${interlaced:-0} -v r=$rotate -v o=$overscan -v logfile="$(echo "$output" | sed 's/\\/\\\\/g')".log 'BEGIN{
1731 ko=(1-o/100)
1732 if(a==1.78||a==1.74)a=16/9
1733 if(a==1.33||a==1.30)a=4/3
1734 if(A=="4/3")A=4/3
1735 if(A=="16/9")A=16/9
1736 if(r!=""){
1737 A=1/A
1738 tmp=W
1740 H=tmp
1742 if(a>A&&crop==0||a<A&&crop==1){
1743 Eh=A*H/a
1744 Ew=W
1745 }else{
1746 Ew=W*a/A
1747 Eh=H
1749 Ew=2*int(Ew*ko/2+0.50)
1750 Eh=2*int(Eh*ko/2+0.50)
1751 printf("-vf scale=%d:%d",Ew,Eh)
1752 printf(" INFO: [mpegfixaspect] -vf scale=%d:%d",Ew,Eh) >>logfile
1753 if(i==1)printf(":1")
1754 if(i==1)printf(":1") >>logfile
1755 if(crop==0){
1756 printf(",expand=%d:%d",W,H)
1757 printf(",expand=%d:%d",W,H) >>logfile
1758 }else{
1759 printf(",crop=%d:%d",W,H)
1760 printf(",crop=%d:%d",W,H) >>logfile
1762 if(r!=""){
1763 printf(",rotate=%d",r)
1764 printf(",rotate=%d",r) >>logfile
1766 if(o!=0) printf(" [overscan=%d]",o) >>logfile
1767 printf("\n") >>logfile
1769 if [[ $mpeg ]]; then
1770 for ((a=0;a<${#MENCODERARG[*]};a++)); do
1771 if [[ ${MENCODERARG[a]} = -vf ]]; then
1772 MENCODERARG[a+1]=$(echo ${MENCODERARG[a+1]} | sed 's/scale=[^,]*,//;s/^/'"${vfilter#-vf }"',/')
1774 done
1778 ###############################################################################
1779 #### warn for aspect ratio
1780 ###############################################################################
1781 if [[ ( $mpeg && $encode != ?:0:? || ! $mpeg && $step -gt 1 ) && ! $mpegfixaspect && ${#TITLESET[*]} -eq 0 && ! $testmca ]]; then
1782 a=$(get_aspect "${MPLAYERINFO[@]}")
1783 [[ ${mpegaspect:-2} = 2 && $a != 1.33 && $a != 1.30 || ${mpegaspect:-2} = 3 && $a != 1.78 && $a != 1.74 || ${mpegaspect:-2} = 4 && $a != 2.21 ]] && \
1784 do_log "++ WARN: [$PROGNAME] selected aspect ratio [${ASPECT[${mpegaspect:-2}]}] and source aspect ratio [$a] are different"
1787 ###############################################################################
1788 #### dvd vobsub
1789 ###############################################################################
1790 #### function to select the vobsub to extract
1791 next_vobsub_idx () {
1792 if ((${#SID[*]})); then
1793 if ((idx < ${#encsid[*]})); then
1794 SID=(-sid ${encsid[idx]} -vobsuboutindex ${encsdx[idx]} ${encsla:+-vobsuboutid ${encsla[idx]}} -vobsubout "$output")
1795 do_log " INFO: [$PROGNAME] dumping subtitle ${encsid[idx]} to vobsub ${encsdx[idx]}${encsla:+ (${encsla[idx]})}"
1796 idx=$((idx+1))
1797 else
1798 unset SID
1802 #### turn on vobsub extraction if encsid is given
1803 if (( ${#encsid[*]} )) ; then
1804 (( ${#encsdx[*]} )) || encsdx=( ${encsid[*]} )
1805 idx=0
1806 SID=(0000)
1807 next_vobsub_idx
1808 else
1809 unset SID
1811 status_bit sub || unset SID
1813 ###############################################################################
1814 #### test condition "extra"
1815 ###############################################################################
1816 IDACOD=$(grep "ID_AUDIO_CODEC" "$output".log | tail -1 | cut -f2 -d=)
1817 [[ $IDACOD = hwdts ]] && echo "**ERROR: [$PROGNAME] dts audio support missing in MPlayer" && \
1818 echo "**ERROR: add dts support (libdts-0.0.2.tar.gz) or select a non dts stream" && \
1819 echo "**ERROR: example: -aid 128 (ac3), -aid 160 (lpcm), -aid 0 (mpeg)" && exit 1
1820 [[ $mpeg && ! $pictsrc && ( $encode == 1:?:? || $multiaudio || $encode == 0:?:? && $IDACOD != mp3 && $IDACOD != a52 && $IDACOD != faad ) ]] && extra=1 || \
1821 extra=
1822 [[ $extra ]] && do_log "**ERROR: [$PROGNAME] output stream: unsupported audio codec $IDACOD" && exit 1
1824 CLEAN[${#CLEAN[*]}]="$output".tmp
1826 ###############################################################################
1827 #### AVI/MPEG section
1828 ###############################################################################
1829 if [[ $encode ]]; then
1830 if status_bit avi ; then
1831 find_sbr () {
1832 local kv k ka
1833 sleep 2
1834 [[ $mpeg ]] && kv=9888 k=33 ka=1015 || kv=10000 k=0 ka=1011
1835 if ((usesbr<=6)); then
1836 sbr=$(awk '/for '"${SBR[usesbr-1]}"'MB CD/{print $NF}' <"$output".log)
1837 [[ $sbr ]] && ((sbr<vbitrate)) && VIDEOPASS=${VIDEOPASS/vbitrate=$vbitrate:/vbitrate=$sbr:} && \
1838 do_log " INFO: [mencoder] using vbitrate=$sbr"
1839 else
1840 #### usesbr is in MB
1841 #### remind: 650-800,650-1400,800-1400
1842 sbr[0]=650
1843 sbr[1]=1400
1844 sbr[2]=$(awk '/for '"${SBR[0]}"'MB CD/{print $NF}' <"$output".log)
1845 sbr[3]=$(awk '/for '"${SBR[4]}"'MB CD/{print $NF}' <"$output".log)
1846 [[ ${sbr[2]} && ${sbr[3]} ]] && \
1847 sbr[4]=$(((((usesbr*kv/10000-k)-(audiosize*ka/1000))*(sbr[3]-sbr[2])+sbr[1]*sbr[2]-sbr[0]*sbr[3])/(sbr[1]-sbr[0])))
1848 [[ ${sbr[4]} ]] && ((sbr[4]<vbitrate && sbr[4]>0)) && VIDEOPASS=${VIDEOPASS/vbitrate=$vbitrate:/vbitrate=${sbr[4]}:} && \
1849 do_log " INFO: [mencoder] using vbitrate=${sbr[4]}"
1852 AID=
1853 if [[ ! $extra ]]; then
1854 RAWVIDEO=( -o "$output".$SUF )
1856 #### start mencoder
1857 PLOG=( -passlogfile "$output".avi2pass.log )
1858 MSG=( -msglevel open=6:demuxer=6:demux=6 )
1859 rm -f frameno.avi
1860 [[ $encode == 0:?:? && ! $extra ]] && F= || F=$af
1861 if [[ $usesbr && ! $extra ]]; then
1862 ((DEBUG)) && debug_line $((LINENO+2)) "usesbr "
1863 me_bit_log
1864 mencoder $OPTIONS -ovc frameno -o /dev/null "${SID[@]}" "${MENCODERARG[@]}" $AID $AUDIOPASS $F &>"$output".fifo
1865 find_sbr
1866 next_vobsub_idx
1868 if ((PASS==1)); then
1869 ((DEBUG)) && debug_line $((LINENO+2)) "PASS 1/$PASS"
1870 me_log
1871 mencoder $OPTIONS $VIDEOPASS "${RAWVIDEO[@]}" "${SID[@]}" "${MENCODERARG[@]}" $AID $AUDIOPASS $F "${MSG[@]}" >"$output".fifo
1872 next_vobsub_idx
1873 else
1874 #### N pass
1875 CLEAN[${#CLEAN[*]}]="$output".avi2pass.log
1876 ((DEBUG)) && debug_line $((LINENO+1)) "PASS 1/$PASS"
1877 mencoder $OPTIONS ${VIDEOPASS}:vpass=1$turbo -o /dev/null "${SID[@]}" "${MENCODERARG[@]}" $AID $AUDIOPASS $F "${PLOG[@]}"
1878 next_vobsub_idx
1879 if ((PASS==2)); then
1880 ((DEBUG)) && debug_line $((LINENO+2)) "PASS 2/$PASS"
1881 me_log
1882 mencoder $OPTIONS ${VIDEOPASS}:vpass=2 "${RAWVIDEO[@]}" "${SID[@]}" "${MENCODERARG[@]}" $AID $AUDIOPASS $F "${PLOG[@]}" "${MSG[@]}" \
1883 >"$output".fifo
1884 next_vobsub_idx
1885 else
1886 for ((a=2;a<PASS;a++)); do
1887 ((DEBUG)) && debug_line $((LINENO+1)) "PASS $a/$PASS"
1888 mencoder $OPTIONS ${VIDEOPASS}:vpass=3 -o /dev/null "${SID[@]}" "${MENCODERARG[@]}" $AID $AUDIOPASS $F "${PLOG[@]}"
1889 next_vobsub_idx
1890 done
1891 ((DEBUG)) && debug_line $((LINENO+2)) "PASS $PASS/$PASS"
1892 me_log
1893 mencoder $OPTIONS ${VIDEOPASS}:vpass=3 "${RAWVIDEO[@]}" "${SID[@]}" "${MENCODERARG[@]}" $AID $AUDIOPASS $F "${PLOG[@]}" "${MSG[@]}" \
1894 >"$output".fifo
1895 next_vobsub_idx
1898 status_bit avi set
1902 ###############################################################################
1903 #### if there are still vobsub to dump, do it now
1904 ###############################################################################
1905 while ((${#SID[*]})) ; do
1906 ((DEBUG)) && debug_line $((LINENO+1)) vobsub
1907 mencoder -ovc copy -o /dev/null $AID "${SID[@]}" "${MENCODERARG[@]}" -nosound
1908 next_vobsub_idx
1909 done
1910 if status_bit sub ; then
1911 if [[ -f $output.sub && -f $output.idx && ${#encsid[*]} -gt 0 ]]; then
1912 #### reset the subtitles with the wrong timestamp
1913 awk -v logfile="$(echo "$output" | sed 's/\\/\\\\/g')".log '{
1914 if($1=="id:")id=" ("$0")"
1915 if($1=="timestamp:"){
1916 n=$2;
1917 sub(/,/,"",n);
1918 # convert the timestamp in seconds
1919 m=3600*substr(n,1,index(n,":")-1);
1920 sub(/[0-9]*:/,"",n);
1921 m=m+60*substr(n,1,index(n,":")-1);
1922 sub(/[0-9]*:/,"",n);
1923 m=m+substr(n,1,index(n,":")-1);
1924 sub(/[0-9]*:/,"",n);
1925 m=m+n/1000;
1926 # .002 is already ok
1927 if(m+.004<t){
1928 printf("++ WARN: [encsid] reset bad timestamp sequence: %s %s%s\n",gensub(/[^ ]* /,"",1,gensub(/,.*/,"",1,p)),substr($2,1,length($2)-1),id) >>logfile ;
1929 id="";
1930 p=gensub(/ [^ ]* /," 00:00:00:000, ",1,p)" #"p}
1931 t=m}
1932 else t=0;
1933 if(NR>1)print p;
1934 p=$0
1936 END{print p}' <"$output".idx >"$output".idx.idx && mv "$output".idx.idx "$output".idx
1937 status_bit sub set
1941 ###############################################################################
1942 #### if fast mode skip multiplexing
1943 ###############################################################################
1944 if [[ $mpeg && $fast ]]; then
1945 if status_bit mpg ; then
1946 if [[ ! $extra ]]; then
1947 rm -f "$output".mpg "${output}"[0-9][0-9].mpg
1948 status_bit mpv set
1949 status_bit mpa set
1950 status_bit mpg set
1951 mv "$output".$SUF "$output".mpg
1956 ###############################################################################
1957 #### split the MPEG stream (for MPEG Mode)
1958 ###############################################################################
1959 if status_bit spl ; then
1960 #### split for MPEG Mode (MPEG-4 codec not supported)
1961 #### MEncoder does not support split as does mpeg2enc/mplex
1962 #### this solution is slow, but it seems quite accurate
1963 if [[ $mpeg && $split && $vcodec != mpeg4 ]]; then
1964 [[ ! $fast ]] && mv "$output"01.mpg "$output".mpg
1965 file_size "$output".mpg
1966 if ((split*1024*1024<fsize)); then
1967 chunks=$((fsize/(split*1024*1024)+1))
1968 CLEAN[${#CLEAN[*]}]="$output".framelist
1969 mplayer "$output".mpg -vf framestep=I -vo null -nosound -benchmark 2>/dev/null | tr '\015' '\012' | \
1970 awk '{if($1=="I!")print t,substr(f,1,index(f,"/")-1);t=$2;f=$3}' >"$output".framelist
1972 #### removed -vc dummy
1973 [[ $multiaudio ]] && a=$(mplayer -vo null -ao null -frames 1 -v "$output".mpg 2>/dev/null | awk '/==> Found audio str/{print $NF}' | \
1974 sort -n | tr '\012' ',' | sed 's/,$/\n/')
1975 for ((i=0;i<chunks;i++)); do
1976 if ((i<chunks-1)); then
1977 n=$(mplayer "$output".mpg -vf framestep=I -vo null -nosound -benchmark 2>/dev/null -sb $(((i+1)*split*1024*1024)) -frames 30 | \
1978 tr '\015' '\012' | awk '{if($1=="I!"){print t;exit};t=$2}')
1979 n=$(awk '/^'"$n"'/{print $2-1;exit}' <"$output".framelist) #should be -2
1980 else
1983 ((DEBUG)) && debug_line $((LINENO+1)) "mpeg_split $((i+1))/$chunks"
1984 "$PROGFILE" -norc "$output".mpg -o "$output"$(printf "%02d" $((i+1))) -mpegonly -mpeg -encode 0:0:1 -$(echo $frameformat| tr '[:upper:]' '[:lower:]') -nosplit -noshowlog -sb $((i*split*1024*1024)) ${n:+-frames $((n-m))} -a ${mpegaspect:-2} ${multiaudio:+-multiaudio $a} -mc 0
1985 rm "$output"$(printf "%02d" $((i+1))).log
1986 m=$n
1987 done
1988 rm "$output".mpg
1989 else
1990 mv "$output".mpg "$output"01.mpg
1992 status_bit spl set
1996 ################################################################################
1997 #### done
1998 ################################################################################
1999 exit