summaryrefslogtreecommitdiffstats
path: root/TOOLS/mencvcd
blob: d4bb7f91f132647d753fe07e0ffa8a5801a1ae96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/bin/sh
#
# Version:          0.1.8
#
# Licence:          GPL
#
# 2002/04/14        Jürgen Hammelmann <juergen.hammelmann@gmx.de>
#
# Script:           MPlayer Sources (DVD) to (S)VCD ripping and burning
#
# requires:         newest mplayer cvs version
#                   mjpegtools v1.6 beta
#                   vcdimager
#                   cdrdao
#                   lame
#
################################################################################
#
# 2002/04/11        v0.1.0: first version
# 2002/04/12        v0.1.1: 
# 2002/04/14        v0.1.2:
#                     - handles now multiple vcd's
#                     - support's mp3 audio as option
#                     - use of mp2enc/lame instead of toolame because of support
#                       of resampling and mp3
# 2002/04/16        v0.1.3:
#                     - new option "-burnonly"
#                     - new option "-vbr"
#                     - removes mpg file after mastering cd image
#                       (mplayer can read bin images!)
#                     - bugfixes
# 2002/04/22        v0.1.4
#                     - bugfixes / changes
#                     - more options
# 2002/05/06        v0.1.5
#                     - new option "-norm"
# 2002/05/08        v0.1.6
#                     - bugfixes
#                     - new option "-vfr"
#                     - "-norm" renamed to "-vnorm"
# 2002/05/15        v0.1.7
#                     - bugfixes
#                     - more help
# 2002/05/26        v0.1.8
#                     - new option "-sync"
#
################################################################################
#
# global config section, change them to your needs!

TMPDIR="." # path to directory for creating temporary files, recommended 2-3GB space

CDDRV="generic-mmc" # cdrdao: cdwriter driver
CDDEV="--device 0,1,0" # or comment out and create link /dev/cdrecorder to your cdwriter dev


################################################################################
AUDIO="audiodump.wav"
VIDEO="stream.yuv"
VCDMODE=2
SVCDMODE=5
################################################################################

function usage() {
   echo "usage: $HOWCALLED <name> [options] [mplayer options]"
   echo
   echo "$HOWCALLED options:"
   echo
   echo "-?|-h         help."
   echo "-abr <n>      output audio bitrate in kbs [224]."
   echo "-asr <n>      output audio sample rate in Hz [44100]."
   echo "-blank        cleans cd-rw before burning."
   echo "-burnonly     burn only the premastered <name>*.cue/<name>*.bin images."
   echo "-cdsize <n>   maximal size of cd images [646]."
   echo "-denoise      denoises mpeg stream."
   echo "-mp3          outputs audio in mp3 instead of mp2 format."
   echo "-mpg          don't encode from source, multiplex/burn."
   echo "              only the encoded mpg stream."
   echo "-noburn       disables burning."
   echo "-ratio <s>    output ratio size of frames, see yuvscaler (1)."
   echo "-size <X>x<Y> sets output size of frames."
   echo "-svcdout      encode to SVCD format [VCD default]."
   echo "-sync <n>     set the presentation timestamp offset of video"
   echo "              stream w.r.t. audio stream (video-audio) in mSec."
   echo "-vbr <n>      output video bitrate in kbs [VCD:1152, SVCD:2500]."
   echo "-vfr <n>      sets the frame-rate of the output-stream. Currently"
   echo "              only the standard MPEG rates are supported."
   echo "              0 - illegal"
   echo "              1 - 24000.0/1001.0 (NTSC 3:2 pulldown converted FILM)"
   echo "              2 - 24.0 (NATIVE FILM)"
   echo "              3 - 25.0 (PAL/SECAM VIDEO / converted FILM)"
   echo "              4 - 30000.0/1001.0 (NTSC VIDEO)"
   echo "              5 - 30.0"
   echo "              6 - 50.0 (PAL FIELD RATE)"
   echo "              7 - 60000.0/1001.0 (NTSC FIELD RATE)"
   echo "              8 - 60.0"
   echo "-vnorm p|n|s  sets the input video norm p, n or s:"
   echo "              forces the input stream to be treated as PAL|NTSC|SECAM"
   echo "              regardless  of  what the stream header might suggest."
   echo "-w            outputs in wide screen format 16:9."
   echo
   echo "examples:"
   echo
   echo "'$HOWCALLED crazy -dvd 3 -w':"
   echo "encodes and burns dvd title 3 to VCD in 16:9."
   echo
   echo "'mencvcd carter -dvd 1 -sid 0 -aid 128 -ss 3:00 -frames 1500 \\"
   echo "        -noburn -vnorm n -vfr 1':"
   echo "this encodes 1500 frames of dvd title 1 with subtitle 0 and audio stream"
   echo "128 starting from 3:00 with frame rate 29.97 fps and input video norm NTSC"
   echo "and output frame rate 23.98 fps because of the 3:2 pulldown method in NTSC."
}

HOWCALLED=`basename $0`
NAME=$1
if [ $# -le 1 ]; then
   usage 
   exit 1
fi
shift 1

cd $TMPDIR
rm -f $VIDEO
rm -f $AUDIO
# create a named pipe for video stream
mkfifo -m 660 $VIDEO

# some inits
sub=""
size=""
ratio=""
params=""
wide=""
blank=0
burn=1
burnonly=0
mp3=0
mkstream=1
abr=224
abrset=0
asr=44100
vbr=1152
vbrset=0
denoise="cat -"
norm="VCD"
mplexnorm="-f $VCDMODE -m 1 -V -b 46"
max=646
mpegnorm="-f $VCDMODE -b $vbr -B 260 -V 46"
imaget="-t vcd2"
yuvin=""
framerate=""
sync=""

while [ "$1"x != "x" ]; do
   case $1 in
      -w)
      	wide="-M WIDE2STD"
	;;
      -h|-?)
        usage
        exit 0
	;;
      -abr)
      	abr=$2
        abrset=1
	shift 1
	;;
      -asr)
        asr=$2
        shift 1
        ;;
      -cdsize)
        max=$2
        shift 1
        ;;
      -blank)
      	blank=1
	;;
      -noburn)
        burn=0
	;;
      -burnonly)
        burnonly=1
	burn=1
	;;
      -vfr)
        framerate="-F $2"
	shift 1
	;;
      -mp3)
        mp3=1
        ;;
      -mpg)
      	mkstream=0
	;;
      -vnorm)
        yuvin="-n $2"
	shift 1
	;;
      -denoise)
        denoise="yuvdenoise"
        ;;
      -ratio)
        ratio=$2
        shift 1
        ;;
      -sid) # mplayer option: have to set vop expand, too!!!
        sub="-vop pp,expand=-1:-1:-1:-1:1 -sid $2"
        shift 1
        ;;
      -size)
        size=$2
        shift 1
        ;;
      -svcdout)
        norm="SVCD"
	;;
      -sync)
        sync="-O $2"
	shift 1
	;;
      -vbr)
        vbr=$2
        vbrset=1
	shift 1
	;;
      *)
	params="$params $1"
	;;
   esac
   shift 1
done

# some configs
if [ "$norm" == "SVCD" ]; then
   [ $vbrset -eq 0 ] && vbr=2500
   mplexnorm="-f $SVCDMODE -m 2 -V -b 230"
   if [ -n "$wide" ]; then
     wide=""
     mpegnorm="-f $SVCDMODE -b $vbr -B 260 -V 230 -a 3"
   else
     mpegnorm="-f $SVCDMODE -b $vbr -B 260 -V 230 -a 2"
   fi
   imaget="-t svcd"
fi

# ratio overwrites wide sizing, size overwrites default frame size
[ -n "$ratio" ] && wide="-M $ratio"
[ -n "$size" ] && size="-O SIZE_$size"

# with mp3 audio set the default audio bitrate to 128 kbs
[ $mp3 -eq 1 -a $abrset -eq 0 ] && abr=128

# audio sample rate in kHz
((a=$asr / 1000))
((b=$asr % 1000))
[ $b -le 9 ] && b="00$b00"
[ $b -le 99 ] && b="0$b00"
kasr="$a.$b"

# start de-/encoding
if [ $burnonly -eq 0 ]; then
   # encode streams
   if [ $mkstream -eq 1 ]; then
      # start mplayer
      mplayer -noframedrop -vo yuv4mpeg -ao pcm -waveheader \
              -v -osdlevel 0 $sub $params &

      # mjpegtools
      ($denoise < $VIDEO | \
         yuvscaler -v 0 $wide -O $norm $size $yuvin | \
         mpeg2enc -v 0 -s $mpegnorm -S $max -g 6 -G 15 -r 16 \
	          $framerate $yuvin -4 2 -2 1 -o $NAME.mpv) &
   
      # wait for finishing the subprocesses
      wait

      if [ $mp3 -eq 0 ]; then
        # mp2enc/lame can't read audiodump.wav directly from named pipe,
        # we have to read the whole file.
        mp2enc -b $abr -r $asr -o $NAME.mpa < $AUDIO
      else
        lame -b $abr --resample $kasr - $NAME.mpa < $AUDIO
      fi
   fi

   # remove wav file, won't need anymore!
   rm -f $AUDIO

   # multiplex streams
   [ -f $NAME.mpv -a -f $NAME.mpa ] || exit 1
   rm -f ${NAME}*.mpg
   mplex $mplexnorm $sync $NAME.mpv $NAME.mpa -o ${NAME}%d.mpg

   # create cd images
   for mpg in ${NAME}*.mpg; do 
      [ -f $mpg ] || exit 1
      cue="`basename $mpg .mpg`.cue"
      bin="`basename $mpg .mpg`.bin"
      rm -f $cue $bin
      vcdimager $imaget -c $cue -b $bin $mpg
      [ -f $bin -a -f $cue ] && rm -f $mpg
   done

# end of streaming/mastering
fi

# remove pipe, won't need anymore!
rm -f $VIDEO

# burn the (s)vcd's
[ $burn -eq 0 ] && exit 0

for cue in ${NAME}*.cue; do
   bin="`basename $cue .cue`.bin"
   [ -f $bin -a -f $cue ] || exit 1

   echo "please insert a cd in your cdwriter, after a keypress we start:"
   read -n 1 i

   if [ $blank -eq 1 ]; then
      cdrdao blank --reload $CDDEV --driver $CDDRV --blank-mode minimal
   fi
   cdrdao write --reload $CDDEV --driver $CDDRV $cue
done
exit 0