in24/in32/fl32 little/big-endian QuickTime PCM audio support
[mplayer/glamo.git] / TOOLS / encode2mpeglight
blob6e04bbc43a8e7ce49dc1f5cdd34065b1c6607dc7
1 #!/bin/bash
3 # Version: 0.5.5
5 # Licence: GPL
7 # 2004-05-22 Giacomo Comes <encode2mpeg at users.sourceforge.net>
8 # 2006-01-14 <encode2mpeg at katamail.com>
10 # Pourpose: Convert anything MPlayer can play to AVI/VCD/SVCD/DVD mpeg
12 # This program 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 # This program 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 this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 CVS too, but due to the "instable" nature of
47 # CVS, bugs may suddenly appear. If you find any, please report it 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 -e "$PROGNAME defaults to encode2mpeg's Mpeg Mode, the others mode are not"
72 echo -e "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 -identify -vo null -ao null -nocache "$@" -frames 0 2>/dev/null
97 ###############################################################################
98 get_aspect () {
99 mplayer -vo null -ao null -nocache -frames 4 "$@" 2>/dev/null | sed '/^Movie-Aspect is/!d;s/.*Movie-Aspect is //;s/:.*//'
101 ###############################################################################
102 mlistopt () {
103 mplayer -list-options 2>&1 | awk '$1=="Name"{m=1}{if(m&&$1!="Name"&&$1!=""&&$1!="Total:")print "\""$1"\""}'
105 ###############################################################################
106 pre_log () {
107 echo "$1"
108 WARN="$WARN${WARN:+\n}$1"
110 ###############################################################################
111 isarg () {
112 if [[ ${2:0:1} = - || ! $2 || $2 = help ]]; then
113 [[ ${2:0:1} = - ]] && echo "**ERROR: [$PROGNAME] invalid argument '$2' for option '$1'"
114 [[ ! $2 ]] && echo "**ERROR: [$PROGNAME] invalid null argument for option '$1'"
115 "$PROGFILE" -norc -l | sed '/^ '$1'$/,/^ -/!d' | sed '$d'
116 "$PROGFILE" -norc -l | sed '/^ '$1'[ |]/,/^ -/!d' | sed '$d'
117 exit 1
119 if [[ $2 = doc ]]; then
120 show_html ${3:+$3.html}
121 exit
124 ###############################################################################
125 pr_date () {
126 echo "[$(date '+%Y-%m-%d %H:%M:%S')]"
128 ###############################################################################
129 pr_time () {
130 date '+%j %H %M %S' | awk '{print $1*86400+$2*3600+$3*60+$4}'
132 ###############################################################################
133 get_abr () {
134 local INFO ABR
135 INFO=$(mp_identify "${MPLAYEROPT[@]}" ${dvddev:+-dvd-device "$dvddev"} "$@" | grep '^ID_')
136 ABR=$(echo "$INFO" | grep '^ID_AUDIO_BITRATE' | cut -f2 -d=)
137 case $(echo "$INFO" | grep '^ID_AUDIO_CODEC' | cut -f2 -d=) in
138 dvdpcm) abr=$((abr+ABR/1024)) ;;
139 *) abr=$((abr+ABR/1000)) ;;
140 esac
142 ###############################################################################
143 get_pwd () {
144 pushd &>/dev/null "$1"
145 echo "$PWD/$2"
146 popd &>/dev/null
148 ###############################################################################
149 show_html () {
150 local i LIST HTML PREFIX OPTION INSTDOCDIR SRCDOCDIR
151 INSTDOCDIR=${PROGFILE%/bin/$PROGNAME}/share/doc/encode2mpeg
152 SRCDOCDIR=${PROGFILE%/$PROGNAME}/doc
153 if [[ -f $INSTDOCDIR/encode2mpeg.html ]]; then
154 HTML=$(get_pwd "$INSTDOCDIR" encode2mpeg.html)
155 elif [[ -f $SRCDOCDIR/encode2mpeg.html ]]; then
156 HTML=$(get_pwd "$SRCDOCDIR" encode2mpeg.html)
157 else
158 HTML="http://encode2mpeg.sourceforge.net"
160 if [[ -f $INSTDOCDIR/html/$1 ]]; then
161 HTML=$(get_pwd "$INSTDOCDIR/html" $1)
162 elif [[ -f $SRCDOCDIR/html/$1 ]]; then
163 HTML=$(get_pwd "$SRCDOCDIR/html" $1)
164 elif [[ $1 && ! -d $INSTDOCDIR/html && ! -d $SRCDOCDIR/html ]]; then
165 HTML="http://encode2mpeg.sourceforge.net/html/$1"
167 LIST=(mozilla firefox)
168 [[ ${HTML:0:1} = / ]] && PREFIX=file:// || PREFIX=
169 for ((i=0;i<${#LIST[*]};i++)); do
170 type ${LIST[i]} &>/dev/null && ${LIST[i]} -remote 'openURL('"$PREFIX$HTML"',new-tab)' 2>/dev/null && return
171 done
172 LIST=(mozilla firefox opera konqueror epiphany galeon)
173 if [[ $BROWSER ]]; then
174 type "$BROWSER" &>/dev/null && LIST=("$BROWSER") || echo "++ WARN: default browser '$BROWSER' not found, using builtin browser list"
176 for ((i=0;i<${#LIST[*]};i++)); do
177 if type "${LIST[i]}" &>/dev/null; then
178 case ${LIST[i]} in
179 opera) OPTION=--newpage ;;
180 epiphany|galeon) OPTION=--new-tab ;;
181 *) OPTION= ;;
182 esac
183 "${LIST[i]}" $OPTION "$HTML" &
184 return
186 done
189 ###############################################################################
190 #### variables initialization
191 ###############################################################################
192 abr=;asr=;vbr=;vfr=;videonorm=;frameformat=;output=;audioformat=;mp1=;mp2=;mp3=;ac3=;dts=;lpcm=;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=;ac=;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=
193 unset encsid encsdx encsla addsub addsdx addsla savecache txtsub txtsubopts
194 zoom=0
195 scale=1
196 fast=1
197 MAXSTEP=6 # burn is the default
198 step=$MAXSTEP
199 split=0
200 vbitrate=16000
201 mpegmbr=0
202 overscan=0
203 iter=0
204 bakiter=0
205 hispeed=0
206 BGTITLE=1
207 TANIM=0
208 TFMT=png
209 TMODE=0
210 TKFRM=0
211 TSECS=5
212 TPART=4
213 TPARTSEC=15
214 TFONTSIZE=1
215 TFONTBG=1
216 TLINES=2
217 TCPR=2
218 MENUERR=0
219 DVDPLAY=0
220 MENUOS=0
221 TSETB=1
222 DVDFS=1
223 VTSMBG=0
224 menufix=0
225 cdburndevice=0,0,0
226 dvdburndevice=/dev/cdrecorder
227 fsize=;fint=;ffrac=;fpre=;audiosize=0
228 ASPECT=(1 1 4/3 16/9 2.21)
229 CH6LIST=(l r ls rs c lfe)
230 TOOL=
231 MPEG2ENCOPT=;YUVSCALEROPT=;YUVDENOISE=;MPLEXOPT=;VCDIMAGEROPT=;DVDAUTHOROPT=;CDRDAOOPT=;GROWISOFSOPT=;MUSICINOPT=
232 unset MPLAYEROPT MPLEXSTREAM MENCODEROPT
233 LPCMPAR=
234 SUBLANG=
235 unset SUBTEXT AUDIOTEXT CHAPTERTEXT TITLESET EXTWAV PROFILE MSRC
236 unset CLEAN
237 DEBUG=0
238 LAVC=
239 WARN=
240 LOG=
241 MAXFIX=20
242 timelen=0
243 ocrsub=0
244 slidefps=1
245 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
246 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
247 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
248 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
249 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
250 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
251 hires_inter=$default_inter
252 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
253 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
254 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
255 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
256 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
257 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
258 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
259 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
260 TXTSUBDEF=( languageId nolang delay 0 font arial.ttf size 28 bottom-margin 30 characterset ISO8859-1 movie-height-reduction 0 fps default )
261 AVISUBDEF=( format SubViewer name-extension null fileformat unix version-number off delay 0 fps default suffix default )
263 #### encode2mpeglight defauls
264 mpeg=1
265 encode=7:2:2
267 (($#)) || ! shortusage || exit 1
268 CMD=( "$@" )
270 #### options array
271 MOPT=( $(mlistopt | grep -v -e : -e '*')
272 $(mlistopt | sed -n '/:/s/:.*/"/p' | uniq)
273 $(mplayer -vfhelp 2>&1 | awk '/vf-/{printf("\"%s\"\n",$1)}')
274 $(mplayer -vophelp 2>&1 | awk '/vop-/{printf("\"%s\"\n",$1)}')
275 $(mplayer -zrhelp 2>/dev/null | awk '$1~/^-zr/{printf("\"%s\"\n",substr($1,2))}') )
277 ###############################################################################
278 #### check rc file
279 ###############################################################################
280 if [[ -s ~/.encode2mpegrc ]]; then
281 for ((i=0;i<${#CMD[*]};i++)); do
282 [[ ${CMD[i]} = -norc ]] && norc=1 && break
283 done
284 if [[ ! $norc ]] && ! awk '{if($1~"^#")exit 1}' ~/.encode2mpegrc ; then
285 WARN="$WARN${WARN:+\n}++ WARN: [$PROGNAME] ~/.encode2mpegrc appears to contain comments, ignoring it"
286 norc=1
288 [[ ! $norc ]] && set -- $(<~/.encode2mpegrc) "$@"
291 ###############################################################################
292 #### arguments parsing
293 ###############################################################################
294 while (($#)) ; do
295 #### PARSING
296 case $1 in
297 -h|-help)
298 #-list the available options
299 [[ $2 = doc || $2 = help ]] && isarg $1 $2
300 shortusage
301 exit
303 -l|-longhelp)
304 #-print this help page
305 [[ $2 = doc || $2 = help ]] && isarg $1 $2
306 usage
307 exit
309 -doc|-documentation) #[[<browser>:][<html page>]]
310 #-show the html documentation
311 [[ $2 = doc || $2 = help ]] && isarg $1 $2
312 [[ ${2%:*} != $2 ]] && BROWSER=${2%:*}
313 show_html ${2#*:}
314 exit
316 -noshowlog)
317 #-do not show the log file
318 [[ $2 = doc || $2 = help ]] && isarg $1 $2
319 quiet=1
321 -o|-output) #<filename>
322 #-filename of the output stream
323 echo "$2" | grep -q '/' && [[ ! -d ${2%/*} ]] && echo "**ERROR: [$PROGNAME] directory ${2%/*} argument of '$1' not found" && exit 1
324 output=$2
325 shift
327 -a) #<n>
328 # aspect ratio VCD SVCD DVD
329 # 1 - 1:1 x * x x = mpeg2enc and mencoder
330 # 2 - 4:3 x x x * = mencoder only
331 # 3 - 16:9 x x x
332 #- 4 - 2.21:1 * x
333 MPEG2ENCOPT="$MPEG2ENCOPT -a $2"
334 [[ $2 -eq 0 ]] && MPEG2ENCOPT="${MPEG2ENCOPT% ${2}} 1"
335 mpegaspect=$2
336 isarg $1 "$2" direct
337 shift
339 -mpegfixaspect) #[pad|crop]
340 # force the aspect ratio of the source video to match the one
341 # specified with -a; this can be done padding the video with
342 #-black lines or cropping the video; the default is padding
343 [[ $2 = doc || $2 = help ]] && isarg $1 $2 aspect
344 mpegfixaspect=0
345 if [[ $2 = pad || $2 = crop ]]; then
346 [[ $2 = crop ]] && mpegfixaspect=1
347 shift
350 -rotate) #<0-3>
351 # rotate the source video by 90 degrees; set -mpegfixaspect;
352 # 0 rotate clockwise and flip, 1 rotate clockwise
353 #-2 rotate counterclockwise, 3 rotate counterclockwise and flip
354 rotate=1
355 echo "$2" | grep -q '^[0-3]$' && rotate=$2
356 isarg $1 "$2" aspect
357 shift
358 : ${mpegfixaspect:=0}
360 -overscan) #<n>
361 # shrink the video of n% and surround it with black borders,
362 # n can be 1-99, using 10 should make visible on a TV the full video
363 # image; if n is negative, the result is a magnification of the
364 # central part of the image; n>0 set -mpegfixaspect pad, n<0 set
365 #--mpegfixaspect crop; in Avi Mode n can only be positive
366 echo "$2" | grep -qE '^-?[0-9]?[0-9]$' && overscan=$2
367 [[ ${2:0:1} = - ]] && mpegfixaspect=1 || mpegfixaspect=0
368 isarg $1 "${2#-}" $( ((step==1)) && echo Avi || echo aspect)
369 shift
371 -abr) #<n>
372 #-audio bit rate of the VCD/SVCD/DVD [MP2:224,MP3:128,AC3:448]
373 if [[ $2 = list ]]; then
374 sed '/^check_abr/,/case/!d;/'"$audioformat"':/!d;/[^0-9]'"$asr"'/!d;s/[^)]*)//;s/ */ /g;s/^/'"$audioformat $asr"'/' "$PROGFILE"
375 exit
377 abr=$2
378 isarg $1 "$2" direct
379 shift
381 -asr) #<n>
382 #-audio sample rate of the VCD/SVCD/DVD [MP2/MP3:44100,AC3:48000]
383 asr=$2
384 isarg $1 "$2" direct
385 shift
387 -vbr) #<n>
388 #-video bit rate of the VCD/SVCD/DVD [VCD:1152,SVCD:2500,DVD:7500]
389 vbr=$2
390 isarg $1 "$2" direct
391 shift
393 -vfr) #<n>
394 # video frame rate of the output stream
395 # [NTSC/VCD:4,NTSC/SVCD-DVD:1,PAL:3,AVI:2]
396 # 1 - 24000.0/1001.0 (NTSC 3:2 pulldown converted FILM)
397 # 2 - 24.0 (NATIVE FILM)
398 # 3 - 25.0 (PAL/SECAM VIDEO / converted FILM)
399 # 4 - 30000.0/1001.0 (NTSC VIDEO)
400 # 5 - 30.0
401 # 6 - 50.0 (PAL FIELD RATE)
402 # 7 - 60000.0/1001.0 (NTSC FIELD RATE)
403 #- 8 - 60.0
404 vfr=$2
405 isarg $1 "$2" direct
406 shift
408 -n|-video-norm) #<n|p|s>
409 #-set the video norm of the VCD/SVCD/DVD
410 case $2 in
411 n|N|ntsc|NTSC) videonorm=n ;;
412 p|P|pal|PAL) videonorm=p ;;
413 s|S|secam|SECAM) videonorm=s ;;
414 doc|help) isarg $1 $2 direct ;;
415 *) echo "**ERROR: [$PROGNAME] invalid argument '$2' for option '$1'" ; "$PROGFILE" -norc $1 help ; exit 1 ;;
416 esac
417 shift
419 -p|-pulldown|-telecine) #
420 # set a flag in the output stream of the SVCD/DVD that tell the
421 # decoder to play the movie as NTSC video using "3:2 pull-down"
422 #-instead of "-vfr 4" use "-vfr 1 -p" (smaller output stream)
423 [[ $2 = doc || $2 = help ]] && isarg $1 $2 direct
424 telecine=1
426 -res) #<1-7>
427 # force one of the following video resolutios:
428 # PAL NTSC
429 # 1 352x288 352x240 (VCD)
430 # 2 352x576 352x480 (CVD)
431 # 3 480x576 480x480 (SVCD)
432 # 4 528x576 528x480 (KVCD)
433 # 5 544x576 544x480 (KVCD)
434 # 6 704x576 704x480 (DVB)
435 #- 7 720x576 720x480 (DVD)
436 isarg $1 "$2" direct
437 echo "$2" | grep -q '^[1-7]$' && MPGRES=$2
438 shift
440 -gop) #<n>
441 # set the number of pictures per GOP; the default value is the one
442 #-required by the standard
443 isarg $1 "$2" direct
444 GOP=$2
445 shift
447 -kvcd) #<1-4>
448 # generate KVCD (www.kvcd.net) compliant frames on output; the
449 # following resolutions are possible:
450 # PAL NTSC GOP
451 # 1 528x576 528x480 24
452 # 2 528x576 528x480 def
453 # 3 544x576 544x480 24
454 #- 4 544x576 544x480 def
455 isarg $1 "$2" direct
457 echo "$2" | grep -q '^[1-4]$' && a=$2
458 shift 2
459 set -- " " -qmatrix kvcd -res $((3+(a+1)/2)) $([[ $a == [13] ]] && echo "-gop 24") "$@"
461 -vcd) #
462 #-generate VCD compliant frames on output (default)
463 [[ $2 = doc || $2 = help ]] && isarg $1 $2 direct
464 frameformat=VCD
466 -svcd) #[1-2]
467 # generate SVCD compliant frames on output; the following resolutions
468 # are possible: PAL NTSC
469 # 1 480x576 480x480
470 # 2 352x288 352x240
471 #-default is 1
472 [[ $2 = doc || $2 = help ]] && isarg $1 $2 direct
473 frameformat=SVCD
474 frameres=1
475 echo "$2" | grep -q '^[1-2]$' && frameres=$2 && shift
477 -svcdht) #
478 # enable the VCD header trick; this trick could allow to play SVCD on
479 # DVD player that does not support SVCD. For more details see:
480 #-http://www.videohelp.com/svcd
481 [[ $2 = doc || $2 = help ]] && isarg $1 $2 direct
482 trick=1
484 -dvd) #[1-5]
485 # generate DVD compliant frames on output; the following resolutions
486 # are possible: PAL NTSC
487 # 1 720x576 720x480
488 # 2 704x576 704x480
489 # 3 480x576 480x480 (non standard DVD-SVCD)
490 # 4 352x576 352x480
491 # 5 352x288 352x240
492 #-default is 1
493 [[ $2 = doc || $2 = help ]] && isarg $1 $2 direct
494 frameformat=DVD
495 frameres=1
496 echo "$2" | grep -q '^[1-5]$' && frameres=$2 && shift
498 -vcodec) #<mpeg1|mpeg2|mpeg4>
499 #-force the selected video codec [VCD:mpeg1,SVCD-DVD:mpeg2,AVI:mpeg4]
500 isarg $1 "$2" Avi
501 [[ $2 == mpeg[124] ]] && vcodec=$2 && [[ ${vcodec:4:1} == [12] ]] && vcodec=${vcodec}video
502 shift
504 -qmatrix) #<kvcd|tmpgenc|default|hi-res>
505 # mpeg2enc custom quantization matrices: kvcd produce a smaller
506 #-output stream, hi-res is good for hi-quality source material
507 case $2 in
508 kvcd|tmpgenc|default|hi-res)
509 MPEG2ENCOPT="$MPEG2ENCOPT -K $2"
510 intra_matrix=$(eval echo \$${2/-}_intra)
511 inter_matrix=$(eval echo \$${2/-}_inter)
514 MPEG2ENCOPT="$MPEG2ENCOPT -K default"
515 intra_matrix=
516 inter_matrix=
518 esac
519 isarg $1 "$2" direct
520 shift
522 -mpeg1vbr) #
523 # produce a VCD/MPEG-1 variable bit rate stream, the output stream
524 # is smaller and a complete movie could fit in one VCD; check if
525 #-your hardware player support variable bit rate VCD
526 [[ $2 = doc || $2 = help ]] && isarg $1 $2 direct
527 MPEG2ENCOPT="$MPEG2ENCOPT -q 8"
528 LAVC=":vrc_buf_size=327"
530 -mpegmbr) #<n>
531 # set the maximum video bit rate; the default is the value of vbr;
532 #-a value of 0 remove the limit
533 mpegmbr=$2
534 [[ $2 -eq 0 ]] && mpegmbr=
535 isarg $1 "$2" mpeg
536 shift
538 -mpegchannels) #<1-6>
539 # number of channels of the mpeg audio stream, 1-6 for ac3 and lpcm;
540 # 1-2 for mp1, mp2 and mp3; 3-6 for mp2 Multichannel Audio; for the
541 #-avi audio stream use MPlayer's option -channels
542 mpegchannels=2
543 isarg $1 "$2" direct
544 echo "$2" | grep -q '^[1-6]$' && mpegchannels=$2
545 shift
547 -normalize)
548 #-normalize to the audio stream(s)
549 [[ $2 = doc || $2 = help ]] && isarg $1 $2 $([[ $encode ]] && echo Avi || echo direct)
550 normalize=1
552 -volume) #<n>
553 # change amplitude of the audio stream; less than 1.0 decreases,
554 #-greater than 1.0 increases (float)
555 volume=$2
556 isarg $1 "$2" direct
557 shift
559 -noscale) #
560 # encode2mpeg automatically scales the video according to the mpeg
561 # profile chosen, this option disable this feature; used mainly
562 #-for debug purpose
563 [[ $2 = doc || $2 = help ]] && isarg $1 $2 direct
564 scale=
566 -monochrome)
567 #-create B/W video or avoid color artifacts in B/W source streams
568 [[ $2 = doc || $2 = help ]] && isarg $1 $2 direct
569 YUVSCALEROPT="$YUVSCALEROPT -O MONOCHROME"
571 -split) #<n>
572 #-split the resulting mpeg stream in <n> MB chunks.
573 split=$2
574 isarg $1 "$2" direct
575 shift
577 -encode) #<n:m:i[,b]>
578 # the parameter n:m:i selects the audio codec, video codec options
579 # and number of pass for mencoder, possible values are:
580 # n m i
581 # 0 copy 0 copy 1
582 # 1 pcm 1 libavcodec/mpeg 2
583 # 2 mp3lame/fast 2 as 1 + mbd=2 3
584 # 3 mp3lame/standard 3 as 1 + compression opts
585 # 4 libavcodec/mp2 4 as 1 + quality opts
586 # 5 libavcodec/mp3
587 # 6 libavcodec/ac3 for m=[2-4] and i>1 turbo is on
588 # 7 toolame/mp2
589 #- with n=[3-7] b specify the audio bit rate
590 encode=5:3:2
591 echo "$2" | grep -qE '^[0-7]:[0-4]:[1-9](,[0-9]+)?$' && encode=$2
592 isarg $1 "$2" $([[ $mpeg ]] && echo mpeg || echo Avi)
593 shift
595 -telecinesrc) #
596 # if you use -encode n:0:i in Mpeg Mode, encode2mpeg needs to know
597 # if the source NTSC mpeg is telecined, otherwise the stream copy may
598 # not work properly; normally encode2mpeg is able to detect telecined
599 # sources, but, if the source mpeg is mixed, part not telecined and
600 # part telecined, encode2mpeg may fail to detect it. In such case,
601 #-you can use this option to specify a telecined source.
602 [[ $2 = doc || $2 = help ]] && isarg $1 $2 mpeg
603 telesrc=1
605 -turbo) #<0-1>
606 # disable (0) or force (1) turbo mode for the first pass of N pass
607 #-encoding (N>1)
608 echo "$2" | grep -q '^[0-1]$' && turbo=$2
609 isarg $1 "$2" Avi
610 shift
612 -hispeed) #
613 # increase the encoding speed of 25-50% (with -encode n:1:1); the
614 # output video stream will be bigger and can have poor quality; this
615 # option is mainly intented for testing, but it can be used if you
616 #-want to create more quickly an mpeg4/avi or a DVD
617 [[ $2 = doc || $2 = help ]] && isarg $1 $2 Avi
618 hispeed=1
620 -bframes) #<0-4>
621 # specify the number of B frames; if -encode is n:3:i the default 2,
622 #-otherwise the default is no B frames; for VCD the default is 2
623 echo "$2" | grep -q '^[0-4]$' && bframes=$2
624 isarg $1 "$2" Avi
625 shift
627 -vcustom) #<libavcodec options>
628 # specify a custom set of video options for libavcodec, use with
629 #--encode n:1:i
630 vcustom=$2
631 isarg $1 "$2" Avi
632 shift
634 -acustom) #<mp3lame options>
635 #-specify a custom set of lame options, use with -encode 2:m:i
636 acustom=$2
637 isarg $1 "$2" Avi
638 shift
640 -encsid) #<sid0,sid1,...>
641 #-dump the DVD vobsub, sid is the number you give the -sid option
642 # of MPlayer.
643 encsid=( ${2//,/ } )
644 isarg $1 "$2" subtitle
645 shift
647 -encsdx) #<sid0,sid1,...>
648 # if you dump subtitle 2 and 4 and you want them to have id 0 and 1
649 #-use -encsid 2,4 -encsdx 0,1
650 encsdx=( ${2//,/ } )
651 isarg $1 "$2" subtitle
652 shift
654 -encsla) #<sla0,sla1,...>
655 #-see doc/html/subtitle.html
656 encsla=( ${2//,/ } )
657 isarg $1 "$2" subtitle
658 shift
660 -usespeed) #
661 # do frame rate conversion changing the playback speed; this option
662 # can be used if you are converting from NTSC 24fps, with or without
663 # telecine, to PAL 25fps and viceversa; during normal frame rate
664 # conversion, frames are skipped or duplicated as needed; with this
665 #-option all the frames are encoded
666 [[ $2 = doc || $2 = help ]] && isarg $1 $2 mpeg
667 usespeed=1
669 -usesbr) #[1-6|>100]
670 # use the suggested video bitrate to set the output stream size
671 # 1 - for 650MB CD 4 - for 2 x 650MB CD
672 # 2 - for 700MB CD (default) 5 - for 2 x 700MB CD
673 # 3 - for 800MB CD 6 - for 2 x 800MB CD
674 # n where n > 100 will set the file size to n MB
675 #-with -multiaudio you must to set the streamsize in MB
676 usesbr=2
677 SBR=(650 700 800 "2 x 650" "2 x 700" "2 x 800")
678 echo "$2" | grep -q '^[1-6]$' && usesbr=$2 && shift
679 echo "$2" | grep -qE '^[1-9][0-9]{2,}$' && usesbr=$2 && shift
680 [[ $2 = doc || $2 = help ]] && isarg $1 $2 Avi
682 -cpu) #<n>
683 #-number of cpu to use, default all the cpu present in the system
684 cpu=1
685 echo "$2" | grep -q '^[1-8]$' && cpu=$2
686 isarg $1 "$2" Avi
687 shift
689 -interlaced)
690 #-turn on optimization for interlaced source video.
691 [[ $2 = doc || $2 = help ]] && isarg $1 $2 $([[ $mpeg ]] && echo mpeg || echo Avi)
692 interlaced=1
694 -slidefps) #<n>
695 # fps to use when creating video from pictures, default is 1; if
696 # there is a audio file associated with a picture, the duration of
697 #-the audio file is used
698 slidefps=$(awk -v a=$2 'BEGIN{printf("%.3f",1/a)}')
699 isarg $1 "$2" images
700 shift
702 -slideaudio) #<audio file>
703 # use the content of <audio file> as audio stream when creating video
704 #-from pictures; it works correctly only if used with mf://singlepic
705 slideaudio=$2
706 if [[ ! -f $2 && $2 != /dev/null ]]; then
707 [[ $2 = doc || $2 = help || ! $2 ]] && isarg $1 "$2" images || ! echo "**ERROR: [$PROGNAME] slide audio file '$2' not found" || exit 1
709 shift
711 -norc)
712 #-do not use the settings in the file $HOME/.encode2mpegrc
713 [[ $2 = doc || $2 = help ]] && isarg $1 $2 rc
715 -debug)
716 # make a more verbose log file and creates a debug file; if you are
717 # submitting a bug report, use this option and compress and send the
718 #-log file and the debug file
719 [[ $2 = doc || $2 = help ]] && isarg $1 $2
720 ((DEBUG)) && DEBUG=2 || DEBUG=1
722 (-frames)
723 frames=$2
724 isarg $1 $2
725 shift
727 (-channels)
728 channels=$2
729 isarg $1 $2
730 shift
732 (-srate)
733 MPLAYEROPT=( "${MPLAYEROPT[@]}" -srate $2 -af-adv force=1 )
734 srate=$2
735 isarg $1 $2
736 shift
738 (-endpos)
739 endpos=$2
740 isarg $1 $2
741 shift
743 (-hr-edl-seek)
744 MENCODEROPT=( "${MENCODEROPT[@]}" $1 )
746 (-vf)
747 vf="-vf $2"
748 MPLAYEROPT=( "${MPLAYEROPT[@]}" $vf )
749 isarg $1 $2
750 shift
752 (-dvd-device)
753 dvddev=$2
754 isarg $1 $2
755 shift
757 (mf://*)
758 MPLAYEROPT[${#MPLAYEROPT[*]}]=$1
759 pictsrc=1
760 a=$IFS
761 IFS=,
762 PICTSRC=( ${1#mf://} )
763 IFS=$a
764 srate=48000
766 (-mpeg|-mpegonly|-nosplit) ;;
768 if [[ ! $TOOL ]]; then
769 [[ ${1:0:1} = - ]] && ! echo "${MOPT[@]}" | grep -q "\"${1#-}\"" && \
770 echo -e "Unknown option $1\nIf this is a valid MPlayer option add '-toolopts mplayer' in front of it" && exit 1
771 MPLAYEROPT[${#MPLAYEROPT[*]}]=$1
772 else
773 case $TOOL in
775 MPLAYEROPT[${#MPLAYEROPT[*]}]=$1
777 esac
780 esac
781 shift
782 done
784 [[ -s ~/.encode2mpegrc && ! $norc ]] && pre_log " INFO: [$PROGNAME] using .encode2mpegrc settings: '$(<~/.encode2mpegrc)'"
786 ###############################################################################
787 #### redirect stdout and stderr to the debug file
788 ###############################################################################
789 if ((DEBUG)); then
790 exec 3>&1
791 rm -f "$output".debug.fifo
792 mkfifo "$output".debug.fifo
793 tee "$output".debug <"$output".debug.fifo >&3 &
794 PROCTEE=$!
795 exec &>"$output".debug.fifo
796 ((DEBUG==2)) && set -x
799 ###############################################################################
800 #### ERROR if some options conflict is detected part 1/2
801 ###############################################################################
802 #### mplayer/mencoder
803 for a in mplayer mencoder ; do
804 type $a &>/dev/null || ! echo "**ERROR: [$PROGNAME] $a missing, install $(echo ${a:0:2} | tr '[:lower:]' '[:upper:]')${a:2}" || exit 1
805 done
806 #### output stream name check
807 [[ ! $output ]] && echo "**ERROR: [$PROGNAME] name of the output stream missing (-o name)" && exit 1
808 #### unspecified video norm
809 [[ ! $videonorm && step -gt 1 && ! ( $mpeg && ${encode%,*} == ?:0:? && ! $menu ) ]] && \
810 echo "**ERROR: [$PROGNAME] you must specify a video norm (-n n|p|s)" && exit 1
811 #### pictsrc
812 if [[ $pictsrc ]]; then
813 [[ $slideaudio != /dev/null && ${encode%,*} == 0:?:? && $mpeg ]] && \
814 echo "**ERROR: [$PROGNAME] -encode 0:m:i is not compatible with mf:// in Mpeg Mode" && exit 1
815 [[ $audioonly ]] && echo "**ERROR: [$PROGNAME] -audioonly does not work with mf://" && exit 1
817 #### -encode 1:m:i is not allowed
818 [[ ${encode%,*} == 1:?:? ]] && echo "**ERROR: [$PROGNAME] do not use -encode 1:m:i" && exit 1
820 ###############################################################################
821 #### WARN if some options conflict is detected
822 ###############################################################################
823 #### missing toolame support
824 if [[ ${encode%,*} == 7:?:? ]]; then
825 if ! mencoder -oac help 2>/dev/null | grep -q toolame ; then
826 encode=4:${encode#?:}
827 pre_log "++ WARN: [$PROGNAME] missing toolame support in mencoder, setting -encode $encode"
831 ###############################################################################
832 #### set default values for unspecified parameters
833 ###############################################################################
834 if [[ ! $multiaudio ]]; then
835 audiostream=1
837 ###############################################################################
838 if [[ $encode ]]; then
839 case ${encode%%:*} in
840 0) [[ ! $pictsrc ]] &&
841 AFMT=acopy ;;
842 2|3|5) AFMT=mp3 ;;
843 4|7) AFMT=mp2 ;;
844 6) AFMT=ac3 ;;
845 esac
846 [[ $AFMT ]] && eval : ${audioformat:-\${$AFMT:=copy}}
848 : ${frameformat:=VCD}
849 : ${audioformat:=${AFMT:-mp2}}
850 : ${mp1:=mp2enc}
851 ((${mpegchannels:-2}>2)) && : ${mp2:=musicin} || : ${mp2:=mp2enc}
852 : ${mp3:=lame}
853 : ${ac3:=mencoder}
854 : ${dts:=copy}
855 case $audioformat in
856 mp1)
857 case ${mpegchannels:-2} in
858 1) : ${abr:=192} ;;
859 2) : ${abr:=384} ;;
860 esac
862 mp2)
863 case ${mpegchannels:-2} in
864 1) : ${abr:=112} ;;
865 2) : ${abr:=224} ;;
866 [3-6]) : ${abr:=384} ;;
867 esac
869 mp3)
870 case ${mpegchannels:-2} in
871 1) : ${abr:=64} ;;
872 2) : ${abr:=128} ;;
873 esac
875 ac3)
876 case ${mpegchannels:-2} in
877 1) : ${abr:=96} ;;
878 2) : ${abr:=192} ;;
879 3) : ${abr:=320} ;; # to verify
880 4) : ${abr:=384} ;; # to verify
881 5) : ${abr:=448} ;; # to verify
882 6) : ${abr:=448} ;;
883 esac
885 #### mplex fails with asr != 48000 for lpcm
886 lpcm) : ${asr:=48000} ${abr:=$((asr*16*${mpegchannels:-2}/1024))} ;;
887 esac
888 if [[ ${encode%,*} == 0:?:? && ${!audioformat} = copy ]]; then
889 abr=0
890 if [[ ! $multiaudio ]]; then
891 get_abr
894 [[ $mpeg && ${encode%,*} == ?:0:? ]] && \
895 vbr=$(($(mp_identify "${MPLAYEROPT[@]}" ${dvddev:+-dvd-device "$dvddev"} "$@" | grep '^ID_VIDEO_BITRATE' | cut -f2 -d=)/1000))
896 case $frameformat in
897 DVD) : ${asr:=48000} ;;
898 *) : ${asr:=44100} ;;
899 esac
900 case $videonorm in
901 p|s)
902 : ${vfr:=3}
905 #FIXME remove the encode 3 check once the mpeg muxer is fixed
906 [[ $frameformat != VCD && ! $vfr && $hispeed -eq 0 && ! $testmca && ${encode%,*} != ?:3:? ]] && vfr=1 && telecine=1
907 : ${vfr:=4}
909 esac
910 [[ $encode ]] && : ${vfr:=2}
911 if [[ ! $ofps ]]; then
912 case $vfr in
913 1) ofps=24000/1001 ;;
914 2) ofps=24 ;;
915 3) ofps=25 ;;
916 4) ofps=30000/1001 ;;
917 5) ofps=30 ;;
918 6) ofps=50 ;;
919 7) ofps=60000/1001 ;;
920 8) ofps=60 ;;
921 esac
923 if [[ $split && $split -eq 0 ]]; then
924 [[ $mpeg ]] && split= || split=800
927 ###############################################################################
928 #### get MPlayer version
929 ###############################################################################
930 mver=$(mencoder 2>/dev/null | awk '$1=="MEncoder"{print $2;exit}')
932 ###############################################################################
933 #### threads check
934 ###############################################################################
935 if [[ ! $cpu && -f /proc/cpuinfo ]]; then
936 cpu=$(grep -c ^processor /proc/cpuinfo)
937 #### if there are 2 logical cpu but 1 physical (hyperthreading)
938 #### use only one thread. With kernel 2.4.x it is more efficent.
939 ((cpu==2)) && [[ $(uname -r | cut -f 1-2 -d .) = 2.4 ]] && \
940 [[ $(grep ^siblings /proc/cpuinfo | uniq | wc -l) -eq 1 ]] && \
941 cpu=$((cpu/$(grep ^siblings /proc/cpuinfo | uniq | awk 'END{print $NF}')))
943 ((cpu<2)) && cpu=
945 ###############################################################################
946 #### -ao pcm arguments
947 ###############################################################################
948 PCM=(pcm:waveheader:file="$output".wav)
949 PCMTMP=(pcm:waveheader:file="$output".tmp)
950 PCMNOWYUV=(pcm:nowaveheader:file=/dev/fd/4)
952 [[ $dvddev ]] && MPLAYEROPT=( "${MPLAYEROPT[@]}" -dvd-device "$dvddev" )
954 ###############################################################################
955 #### mplayer/mencoder/mjpegtools options
956 ###############################################################################
958 #### mencoder output suffix
959 if [[ $encode ]]; then
960 [[ $mpeg ]] && SUF=mpeg || SUF=avi
963 #### MPLAYERINFO is used for [info]
964 MPLAYERINFO=( "${MPLAYEROPT[@]}" )
966 #### mf:// case
967 if [[ $pictsrc ]]; then
968 if [[ $slideaudio && $slideaudio != /dev/null ]]; then
969 [[ $(mp_identify "$slideaudio" | grep ID_AUDIO_RATE | cut -f2 -d=) != $asr ]] && SRATE="-srate $asr -af-adv force=1" || SRATE=
970 CLEAN[${#CLEAN[*]}]="$output".wav
971 mplayer "$slideaudio" $SRATE -vo null -vc dummy -ao "${PCM[@]}" $VOL $ac $afm ${mpegchannels:+-channels $mpegchannels -af channels=$mpegchannels}
972 MPLAYEROPT=( "${MPLAYEROPT[@]}" -fps 1/$(mp_identify "$output".wav | sed -n '/^ID_LENGTH=/s/.*=//p') -audiofile "$output".wav )
973 else
974 if [[ $slideaudio == /dev/null ]]; then
975 MPLAYEROPT=( "${MPLAYEROPT[@]}" -fps $slidefps )
976 encode=0:${encode#?:}
977 else
978 MPLAYEROPT=( "${MPLAYEROPT[@]}" -fps $slidefps -audiofile /dev/zero -audio-demuxer 20 -rawaudio format=0x1:rate=48000 )
983 #### MENCODERARG is used for mencoding, vobsub dumping
984 MENCODERARG=( "${MPLAYEROPT[@]}" ${frames:+-frames $frames} )
986 MENCODERARG=( "${MENCODERARG[@]}" "${MENCODEROPT[@]}" ${endpos:+-endpos $endpos} )
987 if [[ $mpeg && $mpegchannels ]]; then
988 MENCODERARG=( "${MENCODERARG[@]}" -channels $mpegchannels )
989 else
990 MENCODERARG=( "${MENCODERARG[@]}" ${channels:+-channels $channels} )
992 #### if video copy then no ofps
993 [[ ${encode%,*} != ?:0:? ]] && MENCODERARG=( "${MENCODERARG[@]}" ${ofps:+-ofps $ofps} )
995 YUVSCALEROPT="-v 1 -n $videonorm ${scale:+-O $frameformat} $YUVSCALEROPT"
997 MPEG2ENCOPT="${cpu:+-M $cpu }-v 1 -S ${split:-50000} -n $videonorm -F $vfr -s -r 16 ${telecine:+-p} $MPEG2ENCOPT"
998 case $vfr in
999 1|2) MPEG2ENCOPT="-g 6 -G ${GOP:-12} $MPEG2ENCOPT" ;;
1000 3|6) MPEG2ENCOPT="-g 6 -G ${GOP:-15} $MPEG2ENCOPT" ;;
1001 4|5|7|8) MPEG2ENCOPT="-g 9 -G ${GOP:-18} $MPEG2ENCOPT" ;;
1002 esac
1003 [[ $frameformat = VCD ]] && MPEG2ENCOPT="-R 2 $MPEG2ENCOPT"
1004 echo "$MPEG2ENCOPT" | grep -q -e '-K hi-res' && YUVSCALEROPT="-M BICUBIC $YUVSCALEROPT" && \
1005 MPEG2ENCOPT="-4 1 -2 1 $MPEG2ENCOPT" || MPEG2ENCOPT="-4 2 -2 1 $MPEG2ENCOPT"
1007 case $frameformat in
1008 VCD) MPGRES=${MPGRES:-1} ;;
1009 SVCD)
1010 case $frameres in
1011 1) MPGRES=${MPGRES:-3} ;;
1012 2) MPGRES=${MPGRES:-1} ;;
1013 esac
1015 DVD)
1016 case $frameres in
1017 1) MPGRES=${MPGRES:-7} ;;
1018 2) MPGRES=${MPGRES:-6} ;;
1019 3) MPGRES=${MPGRES:-3} ;;
1020 4) MPGRES=${MPGRES:-2} ;;
1021 5) MPGRES=${MPGRES:-1} ;;
1022 esac
1024 esac
1025 case $MPGRES in
1026 1) H_RES=352 ; [[ $videonorm = n ]] && V_RES=240 || V_RES=288 ;;
1027 2) H_RES=352 ; [[ $videonorm = n ]] && V_RES=480 || V_RES=576 ;;
1028 3) H_RES=480 ; [[ $videonorm = n ]] && V_RES=480 || V_RES=576 ;;
1029 4) H_RES=528 ; [[ $videonorm = n ]] && V_RES=480 || V_RES=576 ;;
1030 5) H_RES=544 ; [[ $videonorm = n ]] && V_RES=480 || V_RES=576 ;;
1031 6) H_RES=704 ; [[ $videonorm = n ]] && V_RES=480 || V_RES=576 ;;
1032 7) H_RES=720 ; [[ $videonorm = n ]] && V_RES=480 || V_RES=576 ;;
1033 esac
1034 case $frameformat in
1035 VCD)
1036 : ${vbr:=1152}
1037 MPEG2ENCOPT="-f 2 -b $vbr -V 46 -B $(((abr*audiostream*101875-vbr*2819+3980000)/100000)) $MPEG2ENCOPT"
1038 MPLEXOPT="-f 2 -V -b 46 -r $(((abr*audiostream+vbr)*4)) $MPLEXOPT"
1039 VCDIMAGEROPT="-t vcd2 $VCDIMAGEROPT"
1040 H_STILL=704
1041 [[ $videonorm = n ]] && V_STILL=480 || V_STILL=576
1042 ((MPGRES!=1)) && YUVSCALEROPT="-O SIZE_${H_RES}x${V_RES} ${YUVSCALEROPT/ -O $frameformat}"
1043 MAXBR=1394
1045 SVCD)
1046 : ${vbr:=2500}
1047 MPEG2ENCOPT="-f 5 -b $vbr -V 113 -B $(((abr*audiostream*101250+vbr*742+1665400)/100000)) $MPEG2ENCOPT"
1048 MPLEXOPT="-V -b 113 -r $(((abr*audiostream+vbr)*4)) $MPLEXOPT"
1049 if [[ $trick ]]; then
1050 MPLEXOPT="-f 2 $MPLEXOPT"
1051 VCDIMAGEROPT="-t vcd2 $VCDIMAGEROPT"
1052 else
1053 MPLEXOPT="-f 5 $MPLEXOPT"
1054 VCDIMAGEROPT="-t svcd $VCDIMAGEROPT"
1056 H_STILL=704
1057 [[ $videonorm = n ]] && V_STILL=480 || V_STILL=576
1058 ((frameres>1||MPGRES!=3)) && YUVSCALEROPT="-O SIZE_${H_RES}x${V_RES} ${YUVSCALEROPT/ -O $frameformat}"
1059 MAXBR=2778
1061 DVD)
1062 : ${vbr:=7500}
1063 MPEG2ENCOPT="-f 8 -b $vbr -V 230 -B $(((abr*audiostream*101250+vbr*742+1665400)/100000)) $MPEG2ENCOPT"
1064 MPLEXOPT="-f 8 -V -b 230 -r $(((abr*audiostream+vbr)*4)) $MPLEXOPT"
1065 H_STILL=720
1066 [[ $videonorm = n ]] && V_STILL=480 || V_STILL=576
1067 ((frameres>1||MPGRES!=7)) && YUVSCALEROPT="-O SIZE_${H_RES}x${V_RES} ${YUVSCALEROPT/ -O $frameformat}"
1068 MAXBR=10080
1070 esac
1072 ###############################################################################
1073 #### mencoder audio/video/pass options
1074 ###############################################################################
1075 if [[ $encode ]]; then
1076 OPTIONS="-noskiplimit -sws $((hispeed?1:7))"
1077 if [[ $mpeg ]]; then
1078 # mencoder can put in the mpeg container:
1079 # video: mpeg1, mpeg2, mpeg4
1080 # audio: mp1, mp2, mp3, ac3, aac (not yet: lpcm, dts)
1081 [[ ${encode%,*} != ?:0:? ]] && : ${mpegaspect:=2}
1082 #MUX="-mpegopts ${mpegaspect:+vaspect=${ASPECT[mpegaspect]}:}:vbitrate=${vbr}"
1083 MUX="-mpegopts ${mpegaspect:+vaspect=${ASPECT[mpegaspect]}:}"
1084 MUX2="${telecine:+:telecine}"
1085 case $frameformat in
1086 VCD) MUX="${MUX}format=xvcd" LAVC="vcodec=${vcodec:-mpeg1video}${LAVC:-:vrc_buf_size=327:vrc_minrate=${vbr}:vrc_maxrate=${vbr}}" ;;
1087 SVCD) if [[ $trick ]]; then MUX="${MUX}format=xvcd" ; else
1088 MUX="${MUX}format=xsvcd" ; fi ;
1089 LAVC="vcodec=${vcodec:-mpeg2video}:vrc_buf_size=917" ;;
1090 DVD) MUX="${MUX}format=dvd" LAVC="vcodec=${vcodec:-mpeg2video}:vrc_buf_size=1835" ;;
1091 esac
1092 case $vfr in
1093 1|2) LAVC="$LAVC:keyint=$((hispeed?1:${GOP:-12}))" ;;
1094 3|6) LAVC="$LAVC:keyint=$((hispeed?1:${GOP:-15}))" ;;
1095 4|5|7|8) LAVC="$LAVC:keyint=$((hispeed?1:${GOP:-18}))" ;;
1096 esac
1097 [[ $frameformat = VCD && ! $bframes ]] && LAVC="$LAVC:vmax_b_frames=2"
1098 [[ $mpegmbr ]] && [[ $mpegmbr -lt $vbr ]] && mpegmbr=$vbr
1099 #### -a 1 really means aspect undefined (do not scale), not aspect 1:1
1100 [[ $vcodec != mpeg4 || $mpegaspect != 1 ]] && LAVC="${LAVC}:aspect=${ASPECT[mpegaspect]}"
1101 LAVC="${LAVC}${mpegmbr:+:vrc_maxrate=$mpegmbr}${inter_matrix:+:inter_matrix=${inter_matrix}}${intra_matrix:+:intra_matrix=${intra_matrix}}"
1102 OF="-of mpeg"
1103 NOSKIP=-noskip
1104 vbitrate=$vbr
1105 if [[ ${encode%,*} != ?:0:? ]]; then
1106 for ((a=0;a<${#MENCODERARG[*]};a++)); do
1107 while [[ ${MENCODERARG[a]} = -vf ]]; do
1108 unset MENCODERARG[a] MENCODERARG[a+1]
1109 MENCODERARG=( "${MENCODERARG[@]}" )
1110 done
1111 done
1112 MENCODERARG=( "${MENCODERARG[@]}" ${vf:--vf }${vf:+,}${scale:+scale=${H_RES}:${V_RES}${interlaced:+:1},}harddup )
1116 BASIC="$LAVC:vbitrate=${vbitrate}${cpu:+:threads=$cpu}${bframes:+:vmax_b_frames=$bframes}"
1117 ((hispeed)) && BASIC="$BASIC:vme=0" || BASIC="$BASIC:psnr"
1118 [[ $mpeg && $frameformat = VCD ]] || BASIC="$BASIC${interlaced:+:ildct:ilme}"
1119 echo "$YUVSCALEROPT" | grep -q -e '-O MONOCHROME' && BASIC="$BASIC:gray"
1121 HQ="${BASIC}:mbd=2"
1123 if [[ $mpeg ]]; then
1124 #### dia=6 could be added
1125 BESTSIZE="${BASIC}:mbd=1:loop:mv0:vlelim=-4:vcelim=7:trell:precmp=1:cmp=1:subcmp=1"
1126 [[ ! $bframes ]] && BESTSIZE="$BESTSIZE:vmax_b_frames=2"
1128 #### last_pred=1-2 could be added
1129 BESTQUALITY="${BASIC}:mbd=2:mv0:precmp=6:cmp=6:subcmp=6"
1130 [[ $vbitrate -ge 3500 && $frameformat != VCD && $vcodec != mpeg1video ]] && BESTQUALITY="$BESTQUALITY:dc=9"
1133 case $encode in
1134 0:?:?|0:?:?,*) AUDIOPASS="-oac copy" ;;
1135 1:?:?|1:?:?,*) AUDIOPASS="-oac pcm" ;;
1136 2:?:?|2:?:?,*) AUDIOPASS="-oac mp3lame -lameopts ${acustom:-fast}" ;;
1137 3:?:?) AUDIOPASS="-oac mp3lame -lameopts preset=standard" ;;
1138 4:?:?) AUDIOPASS="-oac lavc -lavcopts acodec=mp2:abitrate=$abr" ;;
1139 5:?:?) AUDIOPASS="-oac lavc -lavcopts acodec=mp3:abitrate=$abr" ;;
1140 6:?:?) AUDIOPASS="-oac lavc -lavcopts acodec=ac3:abitrate=$abr" ;;
1141 7:?:?) AUDIOPASS="-oac toolame -toolameopts br=$abr" ;;
1142 3:?:?,*) AUDIOPASS="-oac mp3lame -lameopts preset=${encode#*,}" ;;
1143 4:?:?,*) AUDIOPASS="-oac lavc -lavcopts acodec=mp2:abitrate=${encode#*,}" ;;
1144 5:?:?,*) AUDIOPASS="-oac lavc -lavcopts acodec=mp3:abitrate=${encode#*,}" ;;
1145 6:?:?,*) AUDIOPASS="-oac lavc -lavcopts acodec=ac3:abitrate=${encode#*,}" ;;
1146 7:?:?,*) AUDIOPASS="-oac toolame -toolameopts br=${encode#*,}" ;;
1147 esac
1148 encode=${encode%,*}
1149 case $encode in
1150 ?:0:?) VIDEOPASS="$OF $MUX -ovc copy $NOSKIP" ; encode=${encode%%:*}:0:1 ; turbo=0 ;; # copy uses only one pass
1151 ?:1:?) VIDEOPASS="$OF $MUX$MUX2 -ovc lavc -lavcopts ${BASIC}${vcustom:+:$vcustom}" ; : ${turbo:=0} ;;
1152 ?:2:?) VIDEOPASS="$OF $MUX$MUX2 -ovc lavc -lavcopts ${HQ}" ; : ${turbo:=1} ;;
1153 ?:3:?) VIDEOPASS="$OF $MUX$MUX2 -ovc lavc -lavcopts ${BESTSIZE}" ; : ${turbo:=1} ;;
1154 ?:4:?) VIDEOPASS="$OF $MUX$MUX2 -ovc lavc -lavcopts ${BESTQUALITY}" ; : ${turbo:=1} ;;
1155 esac
1156 ((turbo)) && turbo=:turbo || turbo=
1157 PASS=${encode##*:}
1158 [[ $normalize && $encode != 0:?:? ]] && AUDIOPASS="-af volnorm $AUDIOPASS"
1162 ###############################################################################
1163 #### more functions
1164 ###############################################################################
1165 status_bit () {
1166 local a
1167 case $1 in
1168 avi) bit=0 ;;
1169 mpv) bit=1 ;;
1170 mpa) bit=2 ;;
1171 mpg) bit=3 ;;
1172 img) bit=4 ;;
1173 sub) bit=5 ;;
1174 ach) bit=6 ;;
1175 fno) bit=7 ;;
1176 ch0) bit=8 ;;
1177 sbm) bit=9 ;;
1178 spl) bit=10 ;;
1179 esac
1180 if [[ $2 = set ]]; then
1181 skip=$((skip|1<<bit))
1182 else
1183 return $(((skip&1<<bit) && 1))
1186 ###############################################################################
1187 file_size () { #fsize,fint,ffrac,fpre
1188 local -a PRE=([1]=K M G T)
1189 local i=0
1190 fsize=$(ls -l "$1" | awk '{print $5}')
1191 fint=$fsize
1192 ffrac=0
1193 while ((fint>=1024)) ; do
1194 ((fint/1024 < 1024)) && fint=$((fint+51))
1195 i=$((++i))
1196 ffrac=$((fint%1024))
1197 fint=$((fint/1024))
1198 done
1199 ffrac=$((ffrac*10/1024))
1200 fpre=${PRE[i]}
1202 ###############################################################################
1203 show_file_info () {
1204 file_size "$2"
1205 echo " $1: $2 is $fsize bytes, $fint.$ffrac ${fpre}B" >>"$output".log
1207 ###############################################################################
1208 show_finalvideo_info () {
1209 local codec TYPE i VIDEO OUT ASPECT
1210 VIDEO="$(mplayer -nocache -frames 0 -vo null -nosound "$2" 2>/dev/null | grep "^VIDEO:")"
1211 if [[ ! $VIDEO ]]; then
1212 # mpegs with mpeg4 video do not show all the video informations in one line,
1213 # assebmling the informations (kbps is missing):
1214 OUT="$(mplayer -nocache -frames 1 -vo null -nosound -v "$2" 2>/dev/null)"
1215 if echo "$OUT" | grep -q '^\[V].*fourcc:0x10000004' ; then
1216 VIDEO="VIDEO: MPEG4 $(echo "$OUT" | awk '$1=="VO:"&&$2=="[null]"{print $3}')"
1217 ASPECT=$(echo "$OUT" | awk '$1=="Movie-Aspect"{print $3}')
1218 case $ASPECT in
1219 undefined) VIDEO="$VIDEO (aspect 1)" ;;
1220 1.33:1) VIDEO="$VIDEO (aspect 2)" ;;
1221 1.78:1) VIDEO="$VIDEO (aspect 3)" ;;
1222 2.21:1) VIDEO="$VIDEO (aspect 4)" ;;
1223 *) VIDEO="$VIDEO (aspect $ASPECT)" ;;
1224 esac
1225 VIDEO="$VIDEO $(echo "$OUT" | awk '$1=="[V]"{print $5}')"
1228 echo " $1: $VIDEO" >>"$output".log
1229 #### removed -vc dummy
1230 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
1231 echo -n " $1: AUDIO[$i]: " >>"$output".log
1232 codec=$(mplayer -ac mp3, -nocache -frames 0 -v -ao null -vo null "$2" -aid $i 2>/dev/null | sed '/Selected audio codec:/!d;s/[^(]*(//;s/).*//;s/.* //')
1233 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' | \
1234 grep -e '^AC3:' -e '^MPEG' -e '^AUDIO:' | sed 's/AUDIO/'"$codec"'/;q' >>"$output".log
1235 done
1236 TYPE=$1
1237 shift
1238 for i ; do
1239 show_file_info "$TYPE" "$i"
1240 done
1242 ###############################################################################
1243 video_duration () {
1244 mencoder "$@" -ovc copy -nosound -o /dev/null -quiet 2>&1 | awk '/^Video stream:/{print $10+$10/$12}'
1246 ###############################################################################
1247 job_exit () {
1248 EXIT=$?
1249 status_bit mpg || [[ ! -f ${output}.mpg && ! -f ${output}01.mpg ]] || show_finalvideo_info "MPEG" "${output}"*.mpg
1250 sec=$(($(pr_time)-STARTTIME))
1251 echo " JOBEND: $output $(pr_date) ($((sec/3600))h$((sec/60%60))m$((sec%60))s)" >>"$output".log
1252 rm -f "${CLEAN[@]}" psnr_??????.log
1253 ((DEBUG)) && exec >&1- >&2- && exec >&3
1254 if [[ -f $output.yuvscaler.log || -f $output.mpeg2enc.log ]]; then
1255 echo
1256 else
1257 ((!EXIT)) && [[ ! $quiet ]] && cat "$output".log
1259 ((DEBUG)) && rm -f "$output".debug.fifo && kill $PROCTEE
1261 ###############################################################################
1262 mplayer_log () {
1263 tee -a /dev/stderr | sed 's/.*\r//' | \
1264 awk '/^PSNR:|^M[EP][ln]|^There are |^\[open]|^==> |^Recommended video bitrate/{sub(/^/," INFO: ['"$1"'] ");print}' >>"$output".log
1266 ###############################################################################
1267 mp_single_log () {
1268 tee -a /dev/stderr | sed 's/.*\r//;/'"$2"'/!d;s/^/ INFO: ['$1'] /' >>"$output".log
1270 ###############################################################################
1271 check_abr () {
1272 # abr permitted:
1273 # ac3: ( 8000/11025/12000) 8 16 24 32 40 48 56 64 80 96 112 128 144 160
1274 # ac3: (16000/22050/24000) 16 24 32 40 48 56 64 80 96 112 128 160 192 224 256 288 320
1275 # ac3: (32000/44100/48000) 32 40 48 56 64 80 96 112 128 160 192 224 256 320 384 448 512 576 640
1276 # mp3: ( 8000/11025/12000) 8 16 24 32 40 48 56 64 80 96 112 128 144 160
1277 # mp3: (16000/22050/24000) 8 16 24 32 40 48 56 64 80 96 112 128 144 160
1278 # mp3: (32000/44100/48000) 32 40 48 56 64 80 96 112 128 160 192 224 256 320
1279 # mp2: ( 8000/11025/12000)
1280 # mp2: (16000/22050/24000) 8 16 24 32 40 48 56 64 80 96 112 128 144 160
1281 # mp2: (32000/44100/48000) 32 48 56 64 80 96 112 128 160 192 224 256 320 384
1282 # mp1: ( 8000/11025/12000)
1283 # mp1: (16000/22050/24000) 32 48 56 64 80 96 112 128 144 160 176 192 224 256
1284 # mp1: (32000/44100/48000) 32 64 96 128 160 192 224 256 288 320 352 384 416 448
1285 case $1 in
1286 ac3)
1287 case $2 in
1288 8000|11025|12000)
1289 case $3 in
1290 8|16|24|32|40|48|56|64|80|96|112|128|144|160) : ;;
1291 *) return 1 ;;
1292 esac
1294 16000|22050|24000)
1295 case $3 in
1296 16|24|32|40|48|56|64|80|96|112|128|160|192|224|256|288|320) : ;;
1297 *) return 1 ;;
1298 esac
1300 32000|44100|48000)
1301 case $3 in
1302 32|40|48|56|64|80|96|112|128|160|192|224|256|320|384|448|512|576|640) : ;;
1303 *) return 1 ;;
1304 esac
1306 *) return 1 ;;
1307 esac
1309 mp3)
1310 case $2 in
1311 8000|11025|12000|16000|22050|24000)
1312 case $3 in
1313 8|16|24|32|40|48|56|64|80|96|112|128|144|160) : ;;
1314 *) return 1 ;;
1315 esac
1317 32000|44100|48000)
1318 case $3 in
1319 32|40|48|56|64|80|96|112|128|160|192|224|256|320) : ;;
1320 *) return 1 ;;
1321 esac
1323 *) return 1 ;;
1324 esac
1326 mp2)
1327 case $2 in
1328 16000|22050|24000)
1329 case $3 in
1330 8|16|24|32|40|48|56|64|80|96|112|128|144|160) : ;;
1331 *) return 1 ;;
1332 esac
1334 32000|44100|48000)
1335 case $3 in
1336 32|48|56|64|80|96|112|128|160|192|224|256|320|384) : ;;
1337 *) return 1 ;;
1338 esac
1340 *) return 1 ;;
1341 esac
1343 mp1)
1344 case $2 in
1345 16000|22050|24000)
1346 case $3 in
1347 32|48|56|64|80|96|112|128|144|160|176|192|224|256) : ;;
1348 *) return 1 ;;
1349 esac
1351 32000|44100|48000)
1352 case $3 in
1353 32|64|96|128|160|192|224|256|288|320|352|384|416|448) : ;;
1354 *) return 1 ;;
1355 esac
1357 *) return 1 ;;
1358 esac
1360 *) return 1 ;;
1361 esac
1363 ###############################################################################
1364 debug_line () {
1365 echo "--DEBUG: [$PROGNAME] $2($1) $(eval echo $(sed -n $1p "$PROGFILE" | sed 's/ |.*//;s/.>.*//;s/</\\\</'))" >>"$output".log
1367 ###############################################################################
1368 check_mencoder_abr () {
1369 local codec lib ASR
1370 codec=([4]=mp2 mp3 ac3 mp2)
1371 lib=([4]=libavcodec libavcodec libavcodec libtoolame)
1372 ASR=${encode%%:*}
1373 check_abr ${codec[ASR]} $1 $2 || ! echo "**ERROR: [$PROGNAME] ${lib[ASR]} does not support $2 kbps / $1 Hz for ${codec[ASR]}" || exit 1
1376 ###############################################################################
1377 #### ERROR if some options conflict is detected part 2/2
1378 ###############################################################################
1379 #### libavcodec codec/asr/abr
1380 #### libtoolame asr/abr
1381 #### libmp3lame asr
1382 #### no check is done on the other channel in case of multiaudio
1383 if [[ $encode == [2-7]:?:? ]]; then
1384 if [[ $srate ]]; then
1385 r=$srate
1386 else
1387 r=$(mp_identify "${MPLAYERINFO[@]}" | sed -n '/^ID_AUDIO_RATE=/s/.*=//p')
1388 if [[ $mpeg && ! $usespeed ]]; then
1389 case $frameformat in
1390 *VCD) ((r != 44100)) && pre_log "++ WARN: [$PROGNAME] $frameformat standard requires 44100kHz audio, add -srate 44100" ;;
1391 DVD) ((r != 48000)) && pre_log "++ WARN: [$PROGNAME] $frameformat standard requires 48000kHz audio, add -srate 48000" ;;
1392 esac
1395 if [[ $encode == [4-7]:?:? ]]; then
1396 check_mencoder_abr $r ${AUDIOPASS##*=}
1397 else
1398 case $r in
1399 8000|11025|12000|16000|22050|24000|32000|44100|48000) : ;;
1400 *) echo "**ERROR: [$PROGNAME] libmp3lame does not support $r Hz sample rate" ; exit 1 ;;
1401 esac
1404 #### copy of non mpeg audio in a VCD
1405 if [[ $step -gt 1 && $frameformat = VCD && $encode == 0:?:? && ( $mpeg || ${!audioformat} = copy ) && ! $testmca && ! $pictsrc ]]; then
1406 a=$(mp_identify "${MPLAYERINFO[@]}" | sed -n '/^ID_AUDIO_CODEC=/s/.*=//p')
1407 [[ $a != mp3 ]] && echo "**ERROR: [$PROGNAME] you cannot copy $a audio in a $frameformat" && exit 1
1409 #### mpegchannels > 2 only with ac3
1410 [[ $mpeg && ${mpegchannels:-2} -gt 2 && $encode == [2-57]:?:? ]] && CODEC=([2]=mp3 mp3 mp2 mp3 [7]=mp2) && \
1411 echo "**ERROR: [$PROGNAME] audio codec ${CODEC[${encode%%:*}]} selected with -encode $encode do not support more than 2 audio channels" && exit 1
1412 ###############################################################################
1413 #### set cleanup
1414 ###############################################################################
1415 trap 'job_exit' 0
1416 CLEAN[${#CLEAN[*]}]="$output".fifo
1418 skip=0
1420 ###############################################################################
1421 #### start the log file
1422 ###############################################################################
1423 if [[ ! $resume ]] ; then
1424 { if [[ $LOG ]]; then
1425 echo "$LOG"
1426 else
1427 STARTTIME=$(pr_time)
1428 echo "### LOG: $output $(pr_date)"
1430 echo -n " INFO: [$PROGNAME] version $VERSION running in "
1431 if ((${#TITLESET[*]})); then
1432 echo -n "Titleset Mode"
1433 else
1434 if [[ $mpeg ]]; then
1435 [[ $testmca ]] && echo -n "Testmca Mode" || echo -n "Mpeg Mode"
1438 echo "${cpu:+ (cpu=$cpu)}"
1439 echo " INFO: [$PROGNAME] command line: '${CMD[@]}'"
1440 } >"$output".log
1443 ###############################################################################
1444 #### WARNING if some requested tools are missing and can be replaced
1445 ###############################################################################
1446 #### WARN
1447 if [[ $WARN ]]; then
1448 echo -e "$WARN" >>"$output".log
1450 #### volume and audio copy
1451 if [[ $volume ]]; then
1452 [[ $encode == 0:?:? && ( ${!audioformat} = copy || $step -eq 1 || $mpeg ) || ${!audioformat} = copy && ! $encode ]] && \
1453 echo "++ WARN: [$PROGNAME] you cannot modify the volume of the output audio stream if you are making a copy the input audio stream" | \
1454 tee -a "$output".log
1456 #### cpu and bframes
1457 if [[ $cpu && $bframes ]]; then
1458 ((bframes)) && echo "++ WARN: [$PROGNAME] with bframes>0 the encoding will be faster with cpu=1" | tee -a "$output".log
1460 #### -usespeed
1461 if [[ $usespeed && ( $encode == 0:?:? || $encode == ?:0:? ) ]]; then
1462 echo -en "++ WARN: [$PROGNAME] -usespeed may not work if you do not encode both audio and video.\nPress return to proceed" | tee -a "$output".log && read
1464 #### total br
1465 [[ $encode != ?:0:? ]] && ((step>1&&abr*audiostream*1024/1000+vbr>MAXBR)) && \
1466 echo "++ WARN: [$PROGNAME] total video+audio bitrate ($vbr+$((abr*audiostream*1024/1000))kbps) exceed $frameformat specifications (${MAXBR}kbps)" | \
1467 tee -a "$output".log
1468 #### -slideaudio/single picture
1469 if [[ $slideaudio && $slideaudio != /dev/null && $pictsrc ]]; then
1470 ((${#PICTSRC[*]}!=1||$(ls ${PICTSRC[0]} | wc -l)!=1)) && \
1471 echo "++ WARN: [$PROGNAME] you should use only one source image if you use the option -slideaudio" | tee -a "$output".log
1473 #FIXME remove once the mpeg muxer is fixed
1474 #### NTSC telecined with B-frames
1475 if [[ $mpeg && $telecine && $videonorm = n && ( $frameformat = DVD || $frameformat = SVCD ) && ( $bframes -gt 0 || ! $bframes && $encode == ?:3:? ) ]]; then
1476 echo "++ WARN: [$PROGNAME] mpeg telecined and with B-frames are not created correctly by mencoder, do not use telecine or do not use B-frames" | \
1477 tee -a "$output".log
1478 echo -n "Press return to proceed" && read
1481 ###############################################################################
1482 #### dump some info in the log file
1483 ###############################################################################
1484 { VARS=(frameformat ${split:+split} vfr vbr abr asr ${mpegchannels:+mpegchannels} ${GOP:+GOP} audioformat) # audioformat must be the last
1485 VARS[${#VARS[*]}]=${!VARS[${#VARS[*]}-1]}
1486 [[ $volume && ! $encode ]] && VARS[${#VARS[*]}]=volume
1487 echo -n " MPEG: "
1488 for ((i=0;i<${#VARS[*]};i++)) ; do
1489 echo -n "${VARS[i]}:${!VARS[i]} "
1490 done
1491 echo -n "${multiaudio:+multiaudio:${multiaudio// /,} }"
1492 echo -n "mpegencoder:$([ $mpeg ] && echo mencoder || echo mpeg2enc)"
1493 [[ $mpegaspect ]] && echo -n " aspect:${ASPECT[mpegaspect]}"
1494 [[ $cdi && $frameformat = VCD ]] && echo -n " CD-i"
1495 [[ $telecine ]] && echo -n " telecine"
1496 echo
1497 VARS=(encode ${vcodec:+vcodec} ${volume:+volume} ${usesbr:+usesbr} ${avisplit:+avisplit} ${channels:+channels} AUDIOPASS VIDEOPASS PASS)
1498 if [[ $encode ]]; then
1499 echo -n " $([ $mpeg ] && echo MPEG || echo \ AVI): "
1500 for ((i=0;i<${#VARS[*]};i++)) ; do
1501 echo -n "${VARS[i]}:${!VARS[i]} "
1502 done
1503 if ((PASS>1)); then
1504 [[ $turbo ]] && echo -n "TURBO:on " || echo -n "TURBO:off "
1506 txt=
1507 [[ $(echo $encode | cut -f 2 -d:) = 1 && $vcustom ]] && txt="libavcodec"
1508 [[ ${encode%%:*} = 2 && $acustom ]] && txt=${txt:+${txt} and} && txt="$txt lame"
1509 txt=${txt:+(custom ${txt} options)}
1510 echo $txt
1512 [[ ! $resume ]] && { [[ $audioonly ]] && mp_identify "$audioonly" || mp_identify "${MPLAYERINFO[@]}" ; } | sed -n '/^ID_/s/^/ INFO: [identify] /p' | uniq
1513 VARS=(${encode:+MENCODERARG} MPLAYERYUVOPT)
1514 VARS=(${encode:+MENCODERARG})
1515 for ((i=0;i<${#VARS[*]};i++)) ; do
1516 s="INFO: [${VARS[i]}] \${${VARS[i]}[*]}"
1517 eval echo "\ \ \ $s"
1518 done
1519 } >>"$output".log
1520 h_res=$(grep ID_VIDEO_WIDTH "$output".log | tail -1 | cut -f2 -d=)
1521 v_res=$(grep ID_VIDEO_HEIGHT "$output".log | tail -1 | cut -f2 -d=)
1522 if [[ $pictsrc ]]; then
1523 v_res=$(mplayer -vo null "${MPLAYERINFO[@]}" -frames 1 2>/dev/null | awk '/^VO:/{print $3}' | head -n 1)
1524 h_res=${v_res%x*}
1525 v_res=${v_res#*x}
1527 [[ $mpeg && ${encode%,*} == ?:0:? ]] && H_RES=$h_res && V_RES=$v_res
1529 ###############################################################################
1530 #### put the volume in DB
1531 ###############################################################################
1532 [[ $volume ]] && volume="-af volume=$(awk -v a=$volume 'BEGIN{if(a>0) print 20*log(a)/log(10) ; else print 0}')"
1534 ###############################################################################
1535 #### NTSC telecined mpeg copy/speed encoding change
1536 ###############################################################################
1537 if [[ $mpeg && ( $encode == ?:0:? || $usespeed ) || $usespeed && ! $encode && $step -gt 1 ]]; then
1538 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)
1539 for ((i=1;i<9;i++)); do
1540 a=$(awk -v a=${FPS[0]} -v b=${FPS[i]} 'BEGIN{if (sqrt((a-b)*(a-b))<.02) print b}')
1541 if [[ $a ]]; then
1542 if [[ ${FPS[0]} != ${FPS[i]} ]]; then
1543 echo "++ WARN: [$PROGNAME] input video frame rate is not exactly ${FPS[i]}fps" | tee -a "$output".log
1544 FPS[0]=${FPS[i]}
1546 FPS[10]=$i
1547 break
1549 done
1550 [[ $usespeed && $i -eq 9 ]] && \
1551 echo "++ WARN: [$PROGNAME] input video frame rate is not a valid NTSC/PAL value; disabling -usespeed" | tee -a "$output".log && usespeed=
1553 if [[ ${FPS[0]} = ${FPS[4]} || ${FPS[0]} = ${FPS[5]} ]]; then
1554 FPS[9]=$(mplayer -nocache -quiet "${MPLAYERINFO[@]}" -vo null -nosound -benchmark -frames 60 2>/dev/null | awk '/^demux_mpg:/{print $2}')
1555 [[ ${FPS[9]} = 24fps || ${FPS[9]} = 24000/1001fps ]] && FPS[10]=$((FPS[10]-3))
1557 if [[ $usespeed ]]; then
1558 if ((vfr!=${FPS[10]})); then
1559 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 )
1560 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 )
1561 DCOEF=([1]=-2 3 7 10 12 13 13)
1562 if ((vfr>${FPS[10]})); then
1563 #### black magic here ;-)
1564 n=$((vfr+${FPS[10]}+${DCOEF[${FPS[10]}]}))
1565 a=${NSPEEDCOEF[n]}/${MSPEEDCOEF[n]}
1566 else
1567 n=$((vfr+${FPS[10]}+${DCOEF[vfr]}))
1568 a=${MSPEEDCOEF[n]}/${NSPEEDCOEF[n]}
1570 MENCODERARG=( -speed $a -srate $asr -af-adv force=1 "${MENCODERARG[@]}" )
1571 MPLAYERYUVOPT=("${MPLAYERYUVOPT[@]}" -speed $a )
1572 echo " INFO: [usespeed] using speed factor $a" | tee -a "$output".log
1574 elif [[ ${FPS[0]} = ${FPS[4]} || ${FPS[0]} = ${FPS[5]} ]]; then
1575 if [[ ${FPS[9]} = 24fps || ${FPS[9]} = 24000/1001fps || $telesrc ]]; then
1576 { echo -n " INFO: [$PROGNAME] "
1577 [[ ${FPS[9]} = 24fps || ${FPS[9]} = 24000/1001fps ]] && echo -n "detected" || echo -n "user selected"
1578 echo " telecined source"
1579 } | tee -a "$output".log
1580 MENCODERARG=( "${MENCODERARG[@]}" -ofps 24000/1001 -mc 0 )
1585 ###############################################################################
1586 #### scale and expand/crop to adapt the aspect ratio
1587 #### added rotation and overscan
1588 ###############################################################################
1589 if [[ $mpegfixaspect && $step -gt 1 ]]; then
1590 a=$(get_aspect "${MPLAYERINFO[@]}")
1591 [[ ${a:0:9} = undefined ]] && a=$(awk -v a=$h_res -v b=$v_res 'BEGIN{printf("%f",a/b)}')
1592 [[ $mpegaspect == 1 ]] && b=$(awk -v a=$H_RES -v b=$V_RES 'BEGIN{printf("%f",a/b)}') || b=${ASPECT[${mpegaspect:-2}]}
1593 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{
1594 ko=(1-o/100)
1595 if(a==1.78)a=16/9
1596 if(a==1.33)a=4/3
1597 if(A=="4/3")A=4/3
1598 if(A=="16/9")A=16/9
1599 if(r!=""){
1600 A=1/A
1601 tmp=W
1603 H=tmp
1605 if(a>A&&crop==0||a<A&&crop==1){
1606 Eh=A*H/a
1607 Ew=W
1608 }else{
1609 Ew=W*a/A
1610 Eh=H
1612 Ew=2*int(Ew*ko/2+0.50)
1613 Eh=2*int(Eh*ko/2+0.50)
1614 printf("-vf scale=%d:%d",Ew,Eh)
1615 printf(" INFO: [mpegfixaspect] -vf scale=%d:%d",Ew,Eh) >>logfile
1616 if(i==1)printf(":1")
1617 if(i==1)printf(":1") >>logfile
1618 if(crop==0){
1619 printf(",expand=%d:%d",W,H)
1620 printf(",expand=%d:%d",W,H) >>logfile
1621 }else{
1622 printf(",crop=%d:%d",W,H)
1623 printf(",crop=%d:%d",W,H) >>logfile
1625 if(r!=""){
1626 printf(",rotate=%d",r)
1627 printf(",rotate=%d",r) >>logfile
1629 if(o!=0) printf(" [overscan=%d]",o) >>logfile
1630 printf("\n") >>logfile
1632 if [[ $mpeg ]]; then
1633 for ((a=0;a<${#MENCODERARG[*]};a++)); do
1634 if [[ ${MENCODERARG[a]} = -vf ]]; then
1635 MENCODERARG[a+1]=$(echo ${MENCODERARG[a+1]} | sed 's/scale=[^,]*,//;s/^/'"${vfilter#-vf }"',/')
1637 done
1641 ###############################################################################
1642 #### dvd vobsub
1643 ###############################################################################
1644 #### function to select the vobsub to extract
1645 next_vobsub_idx () {
1646 if ((${#SID[*]})); then
1647 if ((idx < ${#encsid[*]})); then
1648 SID=(-sid ${encsid[idx]} -vobsuboutindex ${encsdx[idx]} ${encsla:+-vobsuboutid ${encsla[idx]}} -vobsubout "$output")
1649 echo " INFO: [$PROGNAME] dumping subtitle ${encsid[idx]} to vobsub ${encsdx[idx]}${encsla:+ (${encsla[idx]})}" | tee -a "$output".log
1650 idx=$((idx+1))
1651 else
1652 unset SID
1656 #### turn on vobsub extraction if encsid is given
1657 if (( ${#encsid[*]} )) ; then
1658 (( ${#encsdx[*]} )) || encsdx=( ${encsid[*]} )
1659 idx=0
1660 SID=(0000)
1661 next_vobsub_idx
1662 else
1663 unset SID
1665 status_bit sub || unset SID
1667 ###############################################################################
1668 #### test condition "extra"
1669 ###############################################################################
1670 IDACOD=$(grep "ID_AUDIO_CODEC" "$output".log | tail -1 | cut -f2 -d=)
1671 [[ $mpeg && ! $pictsrc && ( $encode == 1:?:? || $multiaudio || $encode == 0:?:? && $IDACOD != mp3 && $IDACOD != a52 ) ]] && extra=1 || extra=
1672 [[ $extra ]] && echo "**ERROR: [$PROGNAME] output stream: unsupported audio codec $IDACOD" | tee -a "$output".log && exit 1
1674 CLEAN[${#CLEAN[*]}]="$output".tmp
1676 ###############################################################################
1677 #### avi/mpeg section
1678 ###############################################################################
1679 if [[ $encode ]]; then
1680 if status_bit avi ; then
1681 find_sbr () {
1682 local kv k ka
1683 if [[ $mpeg ]]; then
1684 kv=9888 ; k=33 ; ka=996
1686 if ((usesbr<=6)); then
1687 sbr=$(awk '/for '"${SBR[usesbr-1]}"'MB CD/{print $NF}' <"$output".log)
1688 [[ $sbr ]] && ((sbr<vbitrate)) && VIDEOPASS=${VIDEOPASS/vbitrate=$vbitrate:/vbitrate=$sbr:} && \
1689 echo " INFO: [mencoder] using vbitrate=$sbr" | tee -a "$output".log
1690 else
1691 #### usesbr is in MB
1692 #### remind: 650-800,650-1400,800-1400
1693 sbr[0]=650
1694 sbr[1]=1400
1695 sbr[2]=$(awk '/for '"${SBR[0]}"'MB CD/{print $NF}' <"$output".log)
1696 sbr[3]=$(awk '/for '"${SBR[4]}"'MB CD/{print $NF}' <"$output".log)
1697 [[ ${sbr[2]} && ${sbr[3]} ]] && \
1698 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])))
1699 [[ ${sbr[4]} ]] && ((sbr[4]<vbitrate && sbr[4]>0)) && VIDEOPASS=${VIDEOPASS/vbitrate=$vbitrate:/vbitrate=${sbr[4]}:} && \
1700 echo " INFO: [mencoder] using vbitrate=${sbr[4]}" | tee -a "$output".log
1703 AID=
1704 #### start mencoder
1705 PLOG=( -passlogfile "$output".avi2pass.log )
1706 rm -f frameno.avi
1707 if ((PASS==1)); then
1708 if [[ $usesbr && ! $extra ]]; then
1709 mencoder $OPTIONS -ovc frameno -o /dev/null "${SID[@]}" "${MENCODERARG[@]}" $AID $AUDIOPASS 2>&1 | \
1710 mp_single_log mencoder '^Recommended video bitrate'
1711 find_sbr
1712 next_vobsub_idx
1714 ((DEBUG)) && debug_line $((LINENO+1)) "PASS 1/$PASS"
1715 mencoder $OPTIONS $VIDEOPASS -o "$output".$SUF "${SID[@]}" "${MENCODERARG[@]}" $AID $AUDIOPASS $volume -v | mplayer_log mencoder
1716 next_vobsub_idx
1717 else
1718 #### N pass
1719 if [[ $usesbr && ! $extra ]]; then
1720 mencoder $OPTIONS -ovc frameno -o /dev/null "${SID[@]}" "${MENCODERARG[@]}" $AID $AUDIOPASS 2>&1 | \
1721 mp_single_log mencoder '^Recommended video bitrate'
1722 find_sbr
1723 next_vobsub_idx
1725 CLEAN[${#CLEAN[*]}]="$output".avi2pass.log
1726 ((DEBUG)) && debug_line $((LINENO+1)) "PASS 1/$PASS"
1727 mencoder $OPTIONS ${VIDEOPASS}:vpass=1$turbo -o /dev/null "${SID[@]}" "${MENCODERARG[@]}" $AID $AUDIOPASS $volume "${PLOG[@]}"
1728 next_vobsub_idx
1729 if ((PASS==2)); then
1730 ((DEBUG)) && debug_line $((LINENO+1)) "PASS 2/$PASS"
1731 mencoder $OPTIONS ${VIDEOPASS}:vpass=2 -o "$output".$SUF "${SID[@]}" "${MENCODERARG[@]}" $AID $AUDIOPASS $volume "${PLOG[@]}" -v | \
1732 mplayer_log mencoder
1733 next_vobsub_idx
1734 else
1735 for ((a=2;a<PASS;a++)); do
1736 ((DEBUG)) && debug_line $((LINENO+1)) "PASS $a/$PASS"
1737 mencoder $OPTIONS ${VIDEOPASS}:vpass=3 -o /dev/null "${SID[@]}" "${MENCODERARG[@]}" $AID $AUDIOPASS $volume "${PLOG[@]}"
1738 next_vobsub_idx
1739 done
1740 ((DEBUG)) && debug_line $((LINENO+1)) "PASS $PASS/$PASS"
1741 mencoder $OPTIONS ${VIDEOPASS}:vpass=3 -o "$output".$SUF "${SID[@]}" "${MENCODERARG[@]}" $AID $AUDIOPASS $volume "${PLOG[@]}" -v | \
1742 mplayer_log mencoder
1743 next_vobsub_idx
1746 status_bit avi set
1750 ###############################################################################
1751 #### if there are still vobsub to dump, do it now
1752 ###############################################################################
1753 while ((${#SID[*]})) ; do
1754 ((DEBUG)) && debug_line $((LINENO+1)) vobsub
1755 mencoder -ovc copy -o /dev/null $AID "${SID[@]}" "${MENCODERARG[@]}" -nosound
1756 next_vobsub_idx
1757 done
1758 if status_bit sub ; then
1759 if [[ -f $output.sub && -f $output.idx && ${#encsid[*]} -gt 0 ]]; then
1760 #### reset the subtitles with the wrong timestamp
1761 awk -v logfile="$(echo "$output" | sed 's/\\/\\\\/g')".log '{
1762 if($1=="id:")id=" ("$0")"
1763 if($1=="timestamp:"){
1764 n=$2;
1765 sub(/,/,"",n);
1766 # convert the timestamp in seconds
1767 m=3600*substr(n,1,index(n,":")-1);
1768 sub(/[0-9]*:/,"",n);
1769 m=m+60*substr(n,1,index(n,":")-1);
1770 sub(/[0-9]*:/,"",n);
1771 m=m+substr(n,1,index(n,":")-1);
1772 sub(/[0-9]*:/,"",n);
1773 m=m+n/1000;
1774 # .002 is already ok
1775 if(m+.004<t){
1776 printf("++ WARN: [encsid] reset bad timestamp sequence: %s %s%s\n",gensub(/[^ ]* /,"",1,gensub(/,.*/,"",1,p)),substr($2,1,length($2)-1),id) >>logfile ;
1777 id="";
1778 p=gensub(/ [^ ]* /," 00:00:00:000, ",1,p)" #"p}
1779 t=m}
1780 else t=0;
1781 if(NR>1)print p;
1782 p=$0
1784 END{print p}' <"$output".idx >"$output".idx.idx && mv "$output".idx.idx "$output".idx
1785 status_bit sub set
1789 ###############################################################################
1790 #### if fast mode skip multiplexing
1791 ###############################################################################
1792 if [[ $mpeg && $fast ]]; then
1793 if status_bit mpg ; then
1794 if [[ ! $extra ]]; then
1795 rm -f "$output".mpg "${output}"[0-9][0-9].mpg
1796 status_bit mpv set
1797 status_bit mpa set
1798 status_bit mpg set
1799 mv "$output".$SUF "$output".mpg
1802 #FIXME remove once the mpeg muxer is fixed
1803 #### NTSC dvd warn
1804 [[ $videonorm = n && $frameformat = DVD ]] && \
1805 echo "++ WARN: [$PROGNAME] mencoder does not multiplex correctly NTSC mpeg, you may want to use encode2mpeg instead" | tee -a "$output".log
1808 ###############################################################################
1809 #### split the mpeg stream (for Mpeg Mode)
1810 ###############################################################################
1811 if status_bit spl ; then
1812 #### split for Mpeg Mode (mpeg4 codec not supported)
1813 #### mencoder does not support split as it does mpeg2enc/mplex
1814 #### this solution is slow, but it seems quite accurate
1815 if [[ $mpeg && $split && $vcodec != mpeg4 ]]; then
1816 [[ ! $fast ]] && mv "$output"01.mpg "$output".mpg
1817 file_size "$output".mpg
1818 if ((split*1024*1024<fsize)); then
1819 chunks=$((fsize/(split*1024*1024)+1))
1820 CLEAN[${#CLEAN[*]}]="$output".framelist
1821 mplayer "$output".mpg -vf framestep=I -vo null -nosound -benchmark 2>/dev/null | tr '\015' '\012' | \
1822 awk '{if($1=="I!")print t,substr(f,1,index(f,"/")-1);t=$2;f=$3}' >"$output".framelist
1824 #### removed -vc dummy
1825 [[ $multiaudio ]] && a=$(mplayer -vo null -ao null -frames 1 -v "$output".mpg 2>/dev/null | awk '/==> Found audio str/{print $NF}' | \
1826 sort -n | tr '\012' ',' | sed 's/,$/\n/')
1827 for ((i=0;i<chunks;i++)); do
1828 if ((i<chunks-1)); then
1829 n=$(mplayer "$output".mpg -vf framestep=I -vo null -nosound -benchmark 2>/dev/null -sb $(((i+1)*split*1024*1024)) -frames 30 | \
1830 tr '\015' '\012' | awk '{if($1=="I!"){print t;exit};t=$2}')
1831 n=$(awk '/^'"$n"'/{print $2-1;exit}' "$output".framelist) #should be -2
1832 else
1835 ((DEBUG)) && debug_line $((LINENO+1)) "mpeg_split $((i+1))/$chunks"
1836 "$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
1837 rm "$output"$(printf "%02d" $((i+1))).log
1838 m=$n
1839 done
1840 rm "$output".mpg
1841 else
1842 mv "$output".mpg "$output"01.mpg
1844 status_bit spl set
1848 ################################################################################
1849 #### done
1850 ################################################################################
1851 exit