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
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 ###############################################################################
52 ###############################################################################
55 shopt -u xpg_echo nullglob
57 PROGFILE
=$
(type -p "$0")
58 VERSION
=$
(awk '$2=="Version:"{print $3;exit}' <"$PROGFILE")
61 ###############################################################################
63 ###############################################################################
66 echo "Arguments enclosed in [ ] are optional"
68 ###############################################################################
71 echo "$PROGNAME defaults to encode2mpeg's MPEG Mode, the other modes are not"
74 ###############################################################################
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"
81 ###############################################################################
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."
88 echo "Usage: $PROGNAME options source"
90 sed -n '/^#### PARSING/,/^done/!d;/^done/q;/^[ ]*-[^)]*)/!d;s/)$//;s/) *#/ /;s/ *#L.$//;p' "$PROGFILE"
93 ###############################################################################
95 mplayer
-msglevel identify
=6 -vo md5sum:outfile
=/dev
/null
-ao null
-nocache -frames 0 "$@" 2>/dev
/null
97 ###############################################################################
101 mp_identify
"$@" |
awk -v id
=$ID -F= '$1==id{t=$2}END{print t}'
103 ###############################################################################
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 ###############################################################################
109 mplayer
-list-options 2>&1 |
awk '$1=="Name"{m=1}{if(m&&$1!="Name"&&$1!=""&&$1!="Total:")print "\""$1"\""}'
111 ###############################################################################
114 ((LG
)) && echo "$1" >>"$output".log || WARN
="$WARN${WARN:+\n}$1"
116 ###############################################################################
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'
125 if [[ $2 = doc
]]; then
126 show_html
${3:+$3.html}
130 ###############################################################################
132 echo "[$(date '+%Y-%m-%d %H:%M:%S')]"
134 ###############################################################################
136 date '+%j %H %M %S' |
awk '{print $1*86400+$2*3600+$3*60+$4}'
138 ###############################################################################
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)) ;;
148 ###############################################################################
150 pushd &>/dev
/null
"$1"
154 ###############################################################################
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
)
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
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
185 opera
) OPTION
=--newpage ;;
186 epiphany|galeon
) OPTION
=--new-tab ;;
189 "${LIST[i]}" $OPTION "$HTML" &
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
203 MAXSTEP
=6 # burn is the default
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
)
237 MPEG2ENCOPT
=;YUVSCALEROPT
=;YUVDENOISE
=;MPLEXOPT
=;VCDIMAGEROPT
=;DVDAUTHOROPT
=;CDRDAOOPT
=;GROWISOFSOPT
=;MUSICINOPT
=
238 unset MPLAYEROPT MPLEXSTREAM MENCODEROPT
241 unset SUBTEXT AUDIOTEXT CHAPTERTEXT TITLESET EXTWAV PROFILE MSRC
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
275 (($#)) ||
! shortusage ||
exit 1
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 ###############################################################################
286 ###############################################################################
287 if [[ -s ~
/.encode2mpegrc
]]; then
288 for ((i
=0;i
<${#CMD[*]};i
++)); do
289 [[ ${CMD[i]} = -norc ]] && norc
=1 && break
291 if [[ ! $norc ]] && ! awk '{if($1~"^#")exit 1}' ~
/.encode2mpegrc
; then
292 do_log
"++ WARN: [$PROGNAME] ~/.encode2mpegrc appears to contain comments, ignoring it" >/dev
/null
295 [[ ! $norc ]] && set -- $
(<~
/.encode2mpegrc
) "$@"
298 ###############################################################################
299 #### arguments parsing
300 ###############################################################################
305 #-list the available options
306 [[ $2 = doc ||
$2 = help ]] && isarg
$1 $2
311 #-print this help page
312 [[ $2 = doc ||
$2 = help ]] && isarg
$1 $2
316 -doc|
-documentation) #[[<browser>:][<html page>]]
317 #-show the html documentation
318 [[ $2 = doc ||
$2 = help ]] && isarg
$1 $2
319 [[ ${2%:*} != $2 ]] && BROWSER
=${2%:*}
324 #-do not show the log file
325 [[ $2 = doc ||
$2 = help ]] && isarg
$1 $2
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
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
340 MPEG2ENCOPT
="$MPEG2ENCOPT -a $2"
341 [[ $2 -eq 0 ]] && MPEG2ENCOPT
="${MPEG2ENCOPT% ${2}} 1"
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
352 if [[ $2 = pad ||
$2 = crop
]]; then
353 [[ $2 = crop
]] && mpegfixaspect
=1
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
362 echo "$2" |
grep -q '^[0-3]$' && rotate
=$2
365 : ${mpegfixaspect:=0}
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
)
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"
389 #-audio sample rate of the VCD/SVCD/DVD [MP2/MP3:44100,AC3:48000]
395 #-video bit rate of the VCD/SVCD/DVD [VCD:1152,SVCD:2500,DVD:7500]
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)
408 # 6 - 50.0 (PAL FIELD RATE)
409 # 7 - 60000.0/1001.0 (NTSC FIELD RATE)
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
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 ;;
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
436 # force one of the following video resolutios:
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)
446 echo "$2" |
grep -q '^[1-7]$' && MPGRES
=$2
450 # set the number of pictures per GOP; the default value is the one
451 #-required by the standard
457 # generate KVCD (www.kvcd.net) compliant frames on output; the
458 # following resolutions are possible:
460 # 1 528x576 528x480 24
461 # 2 528x576 528x480 def
462 # 3 544x576 544x480 24
463 #- 4 544x576 544x480 def
466 echo "$2" |
grep -q '^[1-4]$' && a
=$2
468 set -- " " -qmatrix kvcd
-res $
((3+(a
+1)/2)) $
([[ $a == [13] ]] && echo "-gop 24") "$@"
471 #-generate VCD compliant frames on output (default)
472 [[ $2 = doc ||
$2 = help ]] && isarg
$1 $2 direct
476 # generate SVCD compliant frames on output; the following resolutions
477 # are possible: PAL NTSC
481 [[ $2 = doc ||
$2 = help ]] && isarg
$1 $2 direct
484 echo "$2" |
grep -q '^[1-2]$' && frameres
=$2 && shift
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
494 # generate DVD compliant frames on output; the following resolutions
495 # are possible: PAL NTSC
498 # 3 480x576 480x480 (non standard DVD-SVCD)
502 [[ $2 = doc ||
$2 = help ]] && isarg
$1 $2 direct
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]
510 [[ $2 == mpeg
[124] ]] && vcodec
=$2 && [[ ${vcodec:4:1} == [12] ]] && vcodec
=${vcodec}video
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
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"
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"
540 # set the maximum video bit rate; the default is the value of vbr;
541 #-a value of 0 remove the limit
543 [[ $2 -eq 0 ]] && mpegmbr
=
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
553 echo "$2" |
grep -q '^[1-6]$' && mpegchannels
=$2
557 #-normalize to the audio stream(s)
558 [[ $2 = doc ||
$2 = help ]] && isarg
$1 $2 $
([[ $encode ]] && echo Avi ||
echo direct
)
562 # change amplitude of the audio stream; less than 1.0 decreases,
563 #-greater than 1.0 increases (float)
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
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"
581 #-split the resulting MPEG stream in <n> MB chunks.
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:
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
596 # 6 libavcodec/ac3 for m=[2-4] and i>1 turbo is on
599 #- with n=[3-7] b specify the audio bit rate
601 echo "$2" |
grep -qE '^[0-8]:[0-4]:[1-9](,[0-9]+)?$' && encode
=$2
602 isarg
$1 "$2" $
([[ $mpeg ]] && echo mpeg ||
echo Avi
)
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
616 # disable (0) or force (1) turbo mode for the first pass of N pass
618 echo "$2" |
grep -q '^[0-1]$' && turbo
=$2
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
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
637 -vcustom) #<libavcodec options>
638 # specify a custom set of video options for libavcodec, use with
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)
651 -encsid) #<sid0,sid1,...>
652 #-dump the DVD vobsub, sid is the number you give the -sid option
655 isarg
$1 "$2" subtitle
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
662 isarg
$1 "$2" subtitle
665 -encsla) #<sla0,sla1,...>
666 #-see doc/html/subtitle.html
668 isarg
$1 "$2" subtitle
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
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
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
694 #-number of cpu to use, default all the cpu present in the system
696 echo "$2" |
grep -q '^[1-8]$' && cpu
=$2
701 #-turn on optimization for interlaced source video.
702 [[ $2 = doc ||
$2 = help ]] && isarg
$1 $2 $
([[ $mpeg ]] && echo mpeg ||
echo Avi
)
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
710 slidefps
=$
(awk -v a
=$2 'BEGIN{printf("%.3f",1/a)}')
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
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
723 #-do not use the settings in the file $HOME/.encode2mpegrc
724 [[ $2 = doc ||
$2 = help ]] && isarg
$1 $2 rc
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
744 MPLAYEROPT
=( "${MPLAYEROPT[@]}" -srate $2 -af-adv force
=1 )
755 MENCODEROPT
=( "${MENCODEROPT[@]}" $1 )
759 MPLAYEROPT
=( "${MPLAYEROPT[@]}" $vf )
774 MPLAYEROPT
[${#MPLAYEROPT[*]}]=$1
778 PICTSRC
=( ${1#mf://} )
783 [[ ! -f $2 ]] && echo "**ERROR: [$PROGNAME] subtitle file '$2' not found" && exit 1
784 MPLAYEROPT
=( "${MPLAYEROPT[@]}" $1 "$2" )
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
797 MPLAYEROPT[${#MPLAYEROPT[*]}]=$1
806 [[ -s ~/.encode2mpegrc && ! $norc ]] && do_log " INFO
: [$PROGNAME] using .encode2mpegrc settings
: '$(<~/.encode2mpegrc)'"
807 if [[ $subrender || $harddup ]]; 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 }
815 vf="-vf ${subrender:+expand=::::1}${harddup:+${subrender:+,}harddup}"
816 MPLAYEROPT=( "${MPLAYEROPT[@]}" $vf )
820 ###############################################################################
822 ###############################################################################
824 if ((DEBUG & 1)); then
825 #### redirect stdout and stderr to the debug file
827 rm -f "$output".debug.fifo
828 mkfifo "$output".debug.fifo
829 tee "$output".debug <"$output".debug.fifo >&3 &
831 exec &>"$output".debug.fifo
832 trap 'rm "$output".debug.fifo' 0
834 if ((DEBUG & 2)); then
835 #### catch mplayer/mencoder errors
838 ret=$? ; (( ret )) && error_line "$FUNCNAME $
*" || true
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
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
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
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
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"
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
896 ###############################################################################
897 if [[ $encode ]]; then
898 case ${encode%%:*} in
899 0) [[ ! $pictsrc ]] &&
906 [[ $AFMT ]] && eval : ${audioformat:-\${$AFMT:=copy\}}
908 : ${frameformat:=VCD}
909 : ${audioformat:=${AFMT:-mp2}}
911 ((${mpegchannels:-2}>2)) && : ${mp2:=musicin} || : ${mp2:=mp2enc}
918 case ${mpegchannels:-2} in
924 case ${mpegchannels:-2} in
927 [3-6]) : ${abr:=384} ;;
931 case ${mpegchannels:-2} in
937 case ${mpegchannels:-2} in
940 3) : ${abr:=320} ;; # to verify
941 4) : ${abr:=384} ;; # to verify
942 5) : ${abr:=448} ;; # to verify
946 aac) : ${abr:=$((${mpegchannels:-2}*48))} ;;
947 #### mplex fails with asr != 48000 for lpcm
948 lpcm) : ${asr:=48000} ${abr:=$((asr*16*${mpegchannels:-2}/1024))} ;;
950 if [[ ${encode%,*} == 0:?:? && ${!audioformat} = copy ]]; then
952 if [[ ! $multiaudio ]]; then
956 [[ $mpeg && ${encode%,*} == ?:0:? ]] && \
957 vbr=$(($(id_find ID_VIDEO_BITRATE "${MPLAYEROPT[@]}" ${dvddev:+-dvd-device "$dvddev"} "$@
")/1000))
959 DVD) : ${asr:=48000} ;;
960 *) : ${asr:=44100} ;;
967 [[ $frameformat != VCD && ! $vfr && $hispeed -eq 0 && ! $testmca ]] && vfr=1 && telecine=1
971 [[ $encode && ${encode%,*} != ?:0:? ]] && : ${vfr:=2}
972 if [[ ! $ofps ]]; then
974 1) ofps=24000/1001 ;;
977 4) ofps=30000/1001 ;;
980 7) ofps=60000/1001 ;;
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 ###############################################################################
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}')))
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[@]}" )
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 )
1035 if [[ $slideaudio == /dev/null ]]; then
1036 MPLAYEROPT=( "${MPLAYEROPT[@]}" -fps $slidefps )
1037 encode=0:${encode#?:}
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"
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" ;;
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} ;;
1072 1) MPGRES
=${MPGRES:-3} ;;
1073 2) MPGRES
=${MPGRES:-1} ;;
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} ;;
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 ;;
1095 case $frameformat in
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"
1102 [[ $videonorm = n
]] && V_STILL
=480 || V_STILL
=576
1103 ((MPGRES
!=1)) && YUVSCALEROPT
="-O SIZE_${H_RES}x${V_RES} ${YUVSCALEROPT/ -O $frameformat}"
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"
1114 MPLEXOPT
="-f 5 $MPLEXOPT"
1115 VCDIMAGEROPT
="-t svcd $VCDIMAGEROPT"
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}"
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"
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}"
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}"
1145 if [[ $telecine ]]; then
1146 if [[ $vcodec = mpeg2video ||
! $vcodec && $frameformat != VCD
]]; then
1147 if [[ $vfr == [12] ]]; then
1148 [[ $videonorm = n
]] && MUX2
=":telecine" || MUX2
=":film2pal"
1150 do_log
"++ WARN: [$PROGNAME] telecine only works with 24000/1001 or 24 fps, disabling it"
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" ;;
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}))" ;;
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}}"
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[@]}" )
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
"
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"
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#*,}}" ;;
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} ;;
1228 ((turbo
)) && turbo
=:turbo || turbo
=
1230 [[ $normalize && $encode != 0:?
:?
]] && af
=${af:--af }${af:+,}volnorm
1234 ###############################################################################
1236 ###############################################################################
1252 if [[ $2 = set ]]; then
1253 skip
=$
((skip|
1<<bit))
1255 return $(((skip&1<<bit) && 1))
1258 ###############################################################################
1259 file_size () { #fsize,fint,ffrac,fpre
1260 local -a PRE=([1]=K M G T)
1262 fsize=$(ls -l "$1" | awk '{print $5}')
1265 while ((fint>=1024)) ; do
1266 ((fint/1024 < 1024)) && ffrac=$((( fint+=51 )%1024))
1270 ffrac=$((ffrac*10/1024))
1273 ###############################################################################
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
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}')
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)" ;;
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/')"
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
1328 show_file_info "$TYPE" "$i"
1331 ###############################################################################
1333 mencoder "$@" -ovc copy -nosound -o /dev/null -quiet 2>&1 | awk '/^Video stream:/{print $10+$10/$12}'
1335 ###############################################################################
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
1348 ((!EXIT)) && [[ ! $quiet ]] && cat "$output".log
1350 ((DEBUG)) && rm -f "$output".debug.fifo && kill $PROCTEE
1352 ###############################################################################
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 ###############################################################################
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 ###############################################################################
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
1386 8|16|24|32|40|48|56|64|80|96|112|128|144|160) : ;;
1392 16|24|32|40|48|56|64|80|96|112|128|160|192|224|256|288|320) : ;;
1398 32|40|48|56|64|80|96|112|128|160|192|224|256|320|384|448|512|576|640) : ;;
1407 8000|11025|12000|16000|22050|24000)
1409 8|16|24|32|40|48|56|64|80|96|112|128|144|160) : ;;
1415 32|40|48|56|64|80|96|112|128|160|192|224|256|320) : ;;
1426 8|16|24|32|40|48|56|64|80|96|112|128|144|160) : ;;
1432 32|48|56|64|80|96|112|128|160|192|224|256|320|384) : ;;
1443 32|48|56|64|80|96|112|128|144|160|176|192|224|256) : ;;
1449 32|64|96|128|160|192|224|256|288|320|352|384|416|448) : ;;
1459 ###############################################################################
1461 echo "--DEBUG: [$PROGNAME] $2($1) $(eval echo $(sed -n $1p "$PROGFILE" | sed 's/ |.*//;s/.>.*//;s/</\\\</'))" >>"$output".log
1463 ###############################################################################
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"
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"
1478 echo "submit a bugreport if you think this is a bug in $PROGNAME"
1481 ###############################################################################
1482 check_mencoder_abr () {
1484 codec=([4]=mp2 mp3 ac3 mp2 aac)
1485 lib=([4]=libavcodec libavcodec libavcodec lib${TOOLAME} libfaac)
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 ###############################################################################
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"}')
1500 ###############################################################################
1501 #### ERROR if some options conflict is detected part 2/2
1502 ###############################################################################
1503 #### libavcodec codec/asr/abr
1504 #### libtoolame asr/abr
1506 #### no check is done on the other channel in case of multiaudio
1507 if [[ $encode == [2-8]:?:? ]]; then
1508 if [[ $srate ]]; then
1511 r=$(id_find ID_AUDIO_RATE "${MPLAYERINFO[@]}")
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" ;;
1524 if [[ $encode == [4-7]:?:? ]]; then
1525 check_mencoder_abr "$r" ${AUDIOPASS##*=}
1526 elif [[ $encode == 8:?:? ]]; then
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 ;;
1533 8000|11025|12000|16000|22050|24000|32000|44100|48000) : ;;
1534 *) echo "**ERROR: [$PROGNAME] libmp3lame does not support $r Hz sample rate" ; exit 1 ;;
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 ###############################################################################
1548 ###############################################################################
1550 CLEAN[${#CLEAN[*]}]="$output".fifo
1555 ###############################################################################
1556 #### start the log file
1557 ###############################################################################
1558 if [[ ! $resume ]] ; then
1559 { if [[ $LOG ]]; then
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"
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[@]}'"
1578 ###############################################################################
1579 #### WARNING if some requested tools are missing and can be replaced
1580 ###############################################################################
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"
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
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
1614 for ((i=0;i<${#VARS[*]};i++)) ; do
1615 echo -n "${VARS[i]}:${!VARS[i]} "
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"
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]} "
1630 [[ $turbo ]] && echo -n "TURBO:on " || echo -n "TURBO:off "
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)}
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"
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)
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}')
1674 if [[ ${FPS[0]} != ${FPS[i]} ]]; then
1675 do_log "++ WARN: [$PROGNAME] input video frame rate is not exactly ${FPS[i]}fps"
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]}
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{
1732 if(a==1.78||a==1.74)a=16/9
1733 if(a==1.33||a==1.30)a=4/3
1742 if(a>A&&crop==0||a<A&&crop==1){
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
1756 printf(",expand=%d:%d",W,H)
1757 printf(",expand=%d:%d",W,H) >>logfile
1759 printf(",crop=%d:%d",W,H)
1760 printf(",crop=%d:%d",W,H) >>logfile
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 }"',/')
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 ###############################################################################
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]})}"
1802 #### turn on vobsub extraction if encsid is given
1803 if (( ${#encsid[*]} )) ; then
1804 (( ${#encsdx[*]} )) || encsdx=( ${encsid[*]} )
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 || \
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
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"
1840 #### usesbr is in MB
1841 #### remind: 650-800,650-1400,800-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]}"
1853 if [[ ! $extra ]]; then
1854 RAWVIDEO=( -o "$output".$SUF )
1857 PLOG=( -passlogfile "$output".avi2pass.log )
1858 MSG=( -msglevel open=6:demuxer=6:demux=6 )
1860 [[ $encode == 0:?:? && ! $extra ]] && F= || F=$af
1861 if [[ $usesbr && ! $extra ]]; then
1862 ((DEBUG)) && debug_line $((LINENO+2)) "usesbr "
1864 mencoder $OPTIONS -ovc frameno -o /dev/null "${SID[@]}" "${MENCODERARG[@]}" $AID $AUDIOPASS $F &>"$output".fifo
1868 if ((PASS==1)); then
1869 ((DEBUG)) && debug_line $((LINENO+2)) "PASS 1/$PASS"
1871 mencoder $OPTIONS $VIDEOPASS "${RAWVIDEO[@]}" "${SID[@]}" "${MENCODERARG[@]}" $AID $AUDIOPASS $F "${MSG[@]}" >"$output".fifo
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[@]}"
1879 if ((PASS==2)); then
1880 ((DEBUG)) && debug_line $((LINENO+2)) "PASS 2/$PASS"
1882 mencoder $OPTIONS ${VIDEOPASS}:vpass=2 "${RAWVIDEO[@]}" "${SID[@]}" "${MENCODERARG[@]}" $AID $AUDIOPASS $F "${PLOG[@]}" "${MSG[@]}" \
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[@]}"
1891 ((DEBUG)) && debug_line $((LINENO+2)) "PASS $PASS/$PASS"
1893 mencoder $OPTIONS ${VIDEOPASS}:vpass=3 "${RAWVIDEO[@]}" "${SID[@]}" "${MENCODERARG[@]}" $AID $AUDIOPASS $F "${PLOG[@]}" "${MSG[@]}" \
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
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:"){
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);
1926 # .002 is already ok
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 ;
1930 p=gensub(/ [^ ]* /," 00:00:00:000, ",1,p)" #"p}
1936 END{print p}' <"$output".idx >"$output".idx.idx && mv "$output".idx.idx "$output".idx
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
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
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
1990 mv "$output".mpg "$output"01.mpg
1996 ################################################################################
1998 ################################################################################