demux_avi: Add WAVEFORMATEXTENSIBLE support
[mplayer/glamo.git] / TOOLS / mencvcd.sh
blob6e2650f85a090c8e8d7f3952f034e57f396ed20e
1 #!/bin/bash
3 # Version: 0.2.2
5 # Licence: GPL
7 # 2002/09/21 Jürgen Hammelmann <juergen.hammelmann@gmx.de>
9 # Script: MPlayer Sources (DVD) to (S)VCD ripping and burning
11 # requires: mplayer
12 # mjpegtools
13 # vcdimager
14 # cdrdao
15 # lame (optionally)
16 # toolame (optionally)
17 # sox (optionally)
19 for exe in mplayer mpeg2enc mp2enc mplex yuvscaler ; do
20 if [ -z "`which $exe`" ]; then
21 echo "ERROR: $exe must be in your path $PATH!"
22 exit 1
24 done
25 for exe in vcdimager lame cdrdao yuvdenoise sox toolame normalize; do
26 if [ -z "`which $exe`" ]; then
27 echo "WARNING: $exe is not found in your path $PATH!"
29 done
31 ################################################################################
33 # 2002/04/11 v0.1.0: first version
34 # 2002/04/12 v0.1.1:
35 # 2002/04/14 v0.1.2:
36 # - handles now multiple vcd's
37 # - support's mp3 audio as option
38 # - use of mp2enc/lame instead of toolame because of support
39 # of resampling and mp3
40 # 2002/04/16 v0.1.3:
41 # - new option "-burnonly"
42 # - new option "-vbr"
43 # - removes mpg file after mastering cd image
44 # (mplayer can read bin images!)
45 # - bugfixes
46 # 2002/04/22 v0.1.4
47 # - bugfixes / changes
48 # - more options
49 # 2002/05/06 v0.1.5
50 # - new option "-norm"
51 # 2002/05/08 v0.1.6
52 # - bugfixes
53 # - new option "-vfr"
54 # - "-norm" renamed to "-vnorm"
55 # 2002/05/15 v0.1.7
56 # - bugfixes
57 # - more help
58 # 2002/05/26 v0.1.8
59 # - new option "-sync"
60 # 2002/06/19 v0.1.9
61 # - new option "-mpgonly"
62 # 2002/09/21 v0.1.10
63 # - bug fixes
64 # - tests now, if the commands are in path and give warnings
65 # and errors.
66 # 2002/11/01 v0.2
67 # - bug fixes
68 # - new options: "-sox", "-volume", "-toolame"
69 # - renamed "-mpg" to "-mpegsrc"
70 # - default cd size changed to 800mb
71 # 2002/11/18 v0.2.1
72 # - aspect ratio for vcd's, too
73 # - pulldown mode...
74 # 2002/12/01 v0.2.2
75 # - -normalize
77 ################################################################################
79 # global config section, change them to your needs!
81 TMPDIR="." # path to directory for creating temporary files, recommended 2-3GB space
83 CDDRV="generic-mmc" # cdrdao: cdwriter driver
84 CDDEV="--device 0,1,0" # or comment out and create link /dev/cdrecorder
85 # to your cdwriter dev
86 CDMAXSIZE=800
88 ################################################################################
89 AUDIO="audiodump.wav"
90 VIDEO="stream.yuv"
91 VCDMODE=2
92 SVCDMODE=5
93 ################################################################################
95 usage() {
96 echo "Usage: $HOWCALLED <basename> [$HOWCALLED options] [mplayer options]"
97 echo
98 echo " <basename> is used as basename of the resulting mpg/bin files."
99 echo
100 echo "For mplayer options see mplayer help and manual."
101 echo
102 echo "$HOWCALLED options:"
103 echo
104 echo "-?|-h help."
105 echo "-a <n> sets the aspect ratio (for SVCD):"
106 echo " 1 - 1:1 display"
107 echo " 2 - 4:3 display"
108 echo " 3 - 16:9 display"
109 echo " 4 - 2.21:1 display"
110 echo "-abr <n> output audio bitrate in kbs [224]."
111 echo "-asr <n> output audio sample rate in Hz [44100]."
112 echo "-blank cleans cd-rw before burning."
113 echo "-burnonly burn only the premastered <name>*.cue/<name>*.bin images."
114 echo "-cdsize <n> maximal size of cd images [800 = 80min raw cd]."
115 echo "-denoise denoises mpeg stream."
116 echo "-mp3 outputs audio in mp3 instead of mp2 format."
117 echo "-mpegsrc don't encode from source, multiplex/burn the "
118 echo " encoded mpg stream."
119 echo "-mpgonly do only encoding to mpeg 1/2 format."
120 echo "-noburn disables burning."
121 echo "-normalize use 'normalize'."
122 echo "-overburn enables overburning a cd."
123 echo "-pulldown enable pulldown mode in output."
124 echo "-ratio <s> output ratio size of frames, see yuvscaler (1)."
125 echo "-size <X>x<Y> sets output size of frames."
126 echo "-sox use sox for audio resampling."
127 echo "-svcdout encode to SVCD format [VCD default]."
128 echo "-sync <n> set the presentation timestamp offset of video"
129 echo " stream w.r.t. audio stream (video-audio) in mSec."
130 echo "-toolame use toolame instead of mp2enc."
131 echo "-v <volume> change amplitude (floating point); less than 1.0"
132 echo " decreases, greater than 1.0 increases. Use that only"
133 echo " together with sox!"
134 echo "-vbr <n> output video bitrate in kbs [VCD:1152, SVCD:2500]."
135 echo "-vfr <n> sets the frame-rate of the output-stream. Currently"
136 echo " only the standard MPEG rates are supported:"
137 echo " 0 - illegal"
138 echo " 1 - 24000.0/1001.0 (NTSC 3:2 pulldown converted FILM)"
139 echo " 2 - 24.0 (NATIVE FILM)"
140 echo " 3 - 25.0 (PAL/SECAM VIDEO / converted FILM)"
141 echo " 4 - 30000.0/1001.0 (NTSC VIDEO)"
142 echo " 5 - 30.0"
143 echo " 6 - 50.0 (PAL FIELD RATE)"
144 echo " 7 - 60000.0/1001.0 (NTSC FIELD RATE)"
145 echo " 8 - 60.0"
146 echo "-vnorm <p|n|s> sets the input video norm p, n or s:"
147 echo " forces the input stream to be treated as PAL|NTSC|SECAM"
148 echo " regardless of what the stream header might suggest."
149 echo "-w outputs in wide screen format 16:9."
150 echo
151 echo "examples:"
152 echo
153 echo "'$HOWCALLED teneriffa teneriffa.avi'"
154 echo "this creates a VCD from an avi file."
155 echo
156 echo "'$HOWCALLED crazy dvd://3 -w':"
157 echo "encodes and burns dvd title 3 to VCD in 16:9."
158 echo
159 echo "'$HOWCALLED carter dvd://1 -sid 0 -aid 128 -ss 3:00 -frames 1500 \\"
160 echo " -noburn -vnorm n -vfr 1':"
161 echo "this encodes 1500 frames of dvd title 1 with subtitle 0 and audio stream"
162 echo "128 starting from 3:00 with frame rate 29.97 fps and input video norm NTSC"
163 echo "and output frame rate 23.98 fps because of the 3:2 pulldown method in NTSC."
164 echo
165 echo "'$HOWCALLED bj -size 640x272 -vf expand=640:480:0:104:0 \\"
166 echo " -cdsize 645 -noburn bj.avi'"
167 echo "this example above shows you, how to deal with movies which are not"
168 echo "created with aspect ratios of 4:3 or 16:9."
171 HOWCALLED=`basename $0`
172 if [ $# -le 1 ]; then
173 usage
174 exit 1
176 case $1 in
178 usage
179 exit 1
182 NAME=$1
183 shift 1
185 esac
187 cd $TMPDIR
188 rm -f $VIDEO
189 rm -f $AUDIO
190 # create a named pipe for video stream
191 mkfifo -m 660 $VIDEO
193 # some inits
194 sub=""
195 size=""
196 aratio=""
197 ratio=""
198 params=""
199 wide=""
200 blank=0
201 burn=1
202 burnonly=0
203 mp3=0
204 mpgonly=0
205 mkstream=1
206 abr=224
207 abrset=0
208 asr=44100
209 vbr=1152
210 vbrset=0
211 denoise="cat -"
212 vnorm="VCD"
213 mplexnorm="-f $VCDMODE -m 1 -V -b 46"
214 imaget="-t vcd2"
215 yuvin=""
216 framerate=""
217 sync=""
218 sox=0
219 toolame=0
220 volume="1.0"
221 overburn=""
222 pd=""
223 norm=0
225 while [ "$1"x != "x" ]; do
226 case $1 in
228 aratio="-a $2"
229 shift 1
232 wide="-M WIDE2STD"
234 -h|-?)
235 usage
236 exit 0
238 -abr)
239 abr=$2
240 abrset=1
241 shift 1
243 -asr)
244 asr=$2
245 shift 1
247 -cdsize)
248 CDMAXSIZE=$2
249 shift 1
251 -blank)
252 blank=1
254 -noburn)
255 burn=0
257 -burnonly)
258 burnonly=1
259 burn=1
261 -overburn)
262 overburn="--overburn"
264 -pulldown)
265 pd="-p"
267 -vfr)
268 framerate="-F $2"
269 shift 1
271 -mp3)
272 mp3=1
274 -mpegsrc)
275 mkstream=0
277 -mpgonly)
278 mpgonly=1
280 -normalize)
281 norm=1
283 -vnorm)
284 yuvin="-n $2"
285 shift 1
287 -volume)
288 volume="$2"
289 shift 1
291 -denoise)
292 denoise="yuvdenoise"
294 -ratio)
295 ratio=$2
296 shift 1
298 -sid) # mplayer option: have to set vf expand, too!!!
299 sub="-vf pp,expand=-1:-1:-1:-1:1 -sid $2"
300 shift 1
302 -size)
303 size=$2
304 shift 1
306 -svcdout)
307 vnorm="SVCD"
309 -sync)
310 sync="-O $2"
311 shift 1
313 -sox)
314 sox=1
316 -toolame)
317 toolame=1
319 -vbr)
320 vbr=$2
321 vbrset=1
322 shift 1
325 params="$params $1"
327 esac
328 shift 1
329 done
331 # some configs
332 mpegnorm="-f $VCDMODE -b $vbr -B 260 -V 46"
333 if [ "$vnorm" = "SVCD" ]; then
334 [ $vbrset -eq 0 ] && vbr=2500
335 mplexnorm="-f $SVCDMODE -m 2 -V -b 230"
336 mpegnorm="-f $SVCDMODE -b $vbr -B 260 -V 230"
337 imaget="-t svcd"
340 # ratio overwrites wide sizing, size overwrites default frame size
341 [ -n "$ratio" ] && wide="-M $ratio"
342 [ -n "$size" ] && size="-O SIZE_$size"
344 # with mp3 audio set the default audio bitrate to 128 kbs
345 [ $mp3 -eq 1 -a $abrset -eq 0 ] && abr=128
347 # audio sample rate in kHz
348 a=$(($a=$asr / 1000))
349 b=$(($b=$asr % 1000))
350 [ $b -le 9 ] && b="00$b00"
351 [ $b -le 99 ] && b="0$b00"
352 kasr="$a.$b"
354 # start de-/encoding
355 if [ $burnonly -eq 0 ]; then
356 # encode streams
357 if [ $mkstream -eq 1 ]; then
358 # start mplayer
359 command="mplayer -noframedrop -vo yuv4mpeg -ao pcm:waveheader -v -osdlevel 0 $sub $params"
360 echo "$command"
361 $command &
363 # mjpegtools
364 ($denoise < $VIDEO | \
365 yuvscaler -v 0 $wide -O $vnorm $size $yuvin | \
366 mpeg2enc -v 0 -s $mpegnorm $aratio -S $CDMAXSIZE -g 6 -G 15 -r 16 \
367 $pd $framerate $yuvin -4 2 -2 1 -o $NAME.mpv) &
369 # wait for finishing the subprocesses
370 wait
372 # normalize sound
373 [ $norm -eq 1 ] && (echo "normalizing sound..."; normalize $AUDIO)
375 # do resampling with sox
376 if [ $sox -ne 0 ]; then
377 echo "wait, do resampling with sox..."
378 sox $AUDIO -v $volume -r $asr $NAME.wav resample -qs
379 mv -f $NAME.wav $AUDIO
382 if [ $toolame -ne 0 -a $mp3 -eq 0 ]; then
383 # do mp2 encoding with toolame
384 echo "wait, encoding to mp2 audio with toolame..."
385 toolame -b $abr $AUDIO
386 mv -f `basename $AUDIO .wav`.mp2 $NAME.mpa
387 elif [ $toolame -eq 0 -a $mp3 -eq 0 ]; then
388 # mp2enc/lame can't read audiodump.wav directly from named pipe,
389 # we have to read the whole file.
390 echo "wait, encoding to mp2 audio with mp2enc..."
391 mp2enc -b $abr -r $asr -o $NAME.mpa < $AUDIO
392 elif [ $mp3 -ne 0 ]; then
393 echo "wait, encoding to mp3 audio with lame..."
394 lame -b $abr --resample $kasr - $NAME.mpa < $AUDIO
398 # remove wav file, won't need anymore!
399 rm -f $AUDIO
401 # multiplex streams
402 [ -f $NAME.mpv -a -f $NAME.mpa ] || exit 1
403 rm -f ${NAME}*.mpg
404 mplex $mplexnorm $sync $NAME.mpv $NAME.mpa -o ${NAME}%d.mpg
406 # remove pipe, won't need anymore!
407 rm -f $VIDEO
409 # should i create only the mpeg file?
410 [ $mpgonly -eq 1 ] && exit 0
412 # create cd images
413 for mpg in ${NAME}*.mpg; do
414 [ -f $mpg ] || exit 1
415 cue="`basename $mpg .mpg`.cue"
416 bin="`basename $mpg .mpg`.bin"
417 rm -f $cue $bin
418 vcdimager $imaget -c $cue -b $bin $mpg
419 [ -f $bin -a -f $cue ] && rm -f $mpg
420 done
422 # end of streaming/mastering
425 # burn the (s)vcd's
426 [ $burn -eq 0 ] && exit 0
428 for cue in ${NAME}*.cue; do
429 bin="`basename $cue .cue`.bin"
430 [ -f $bin -a -f $cue ] || exit 1
432 echo "please insert a cd in your cdwriter, after a keypress we start:"
433 read -n 1 i
435 if [ $blank -eq 1 ]; then
436 cdrdao blank --reload $CDDEV --driver $CDDRV --blank-mode minimal
438 cdrdao write $overburn --reload $CDDEV --driver $CDDRV $cue
439 done
440 exit 0