aboutsummaryrefslogtreecommitdiffhomepage
path: root/TOOLS/divx2svcd.sh
blob: 4782dd679c697e581a09880167e3689f32fa50f7 (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/bin/bash

# (c) 2003 Vajna Miklos <mainroot@freemail.hu>
# divx2svcd for MPlayer
# distributed under GPL License

# simple utility that creates a SVCD from a video in an AVI container

# The newest version of this utility can be found at
# http://vmiklos.uw.hu/divx2svcd/divx2svcd
# MPlayer available at
# http://www.mplayerhq.hu/MPlayer/releases/MPlayer-1.0pre3try2.tar.bz2

###changelog###
#nobody cares about it :-)
cat >/dev/null <<EOF
0.5.1
- faster code by not re-mplexing one-cd-size or smaller videos

0.5.0
- needless for dumpvideo patch ;-)

0.4.9
- changed default bitrate to 1600 for better quality
- fix for burning more than one cd
- fix for wrong parameter help

0.4.8
- small fixes

0.4.7
- fixed bug, when there is no sub available

0.4.6
- support for burning the svcd with cdrecord
- lots of paranoid options for better quality from Denes Balatoni

0.4.5
- support for filenames including spaces

0.4.4
- support for checking all applications this script uses
- this changelog

0.4.3
- advanced detectation of movie aspect (mpeg4 codec, mpeg container)

0.4.2
- advanced vf options for movies with non-standard aspect

0.4.1
- checking for available sub

0.4.0
- support for tcmplex-panteltje
- support for libavcodec audio encoder

0.3.1-0.3.2
- small fixes

0.3
- almost totally rewritten from scratch
  based on the idea of Denes Balatoni <pnis@coder.hu>
- support for toolame instead of mp2enc
- suppert for libavcodec mpeg2video codec instead of mpeg2enc

0.2
- support for tcmplex instead of mplex

0.1rc2-rc4
- small bugfixes

0.1rc1
- initial release

EOF


###preparing###
#help

usage()
{
	cat <<EOF
Usage: `basename $0` input_avi [options]

Options:
-b|--bitrate xx	bitrate of mp2 video stream [1375]
-s|--cdsize xx	size of the cd we split the video to [795]
-w|--writecd	enables burning [disable]
-d|--device xx	scsi cd-recording device if you are using linux 2.4.x [0,0,0]
-c|--clean	clean up svcd images you just created
-h|--help	this help screen
EOF

}

#initializating constants
version='0.5.1'
bitrate=1375
cdsize=795
burning=0
cleaning=0
dev4='0,0,0'
firstcd=1

#paranoid options
paraopts='vrc_override=1,10,708:vqcomp=0.1:vratetol=10000000:vrc_buf_size=917:vrc_maxrate=2500:intra_matrix=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,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:inter_matrix=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,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'

#header
echo "DivX2SvcD $version (C) 2003-2004 Vajna Miklos"
echo

#checking for ls
ls=`which ls`

#checking for bc
which bc >/dev/null 2>&1
bcbin=`which bc 2>/dev/null`
if [ $? != 0 ]; then
	cat <<EOF
ERROR: Can't find bc. You can download it at
ftp://ftp.ibiblio.org/pub/gnu/bc/bc-1.06.tar.gz
EOF
exit 1
fi

#checking for vcdimager
which vcdimager >/dev/null 2>&1
bcbin=`which vcdimager 2>/dev/null`
if [ $? != 0 ]; then
	cat <<EOF
ERROR: Can't find vcdimager. You can download it at http://www.vcdimager.org
/pub/vcdimager/vcdimager-0.7_UNSTABLE/vcdimager-0.7.14.tar.gz
EOF
exit 1
fi

#checking which mplex utility we have to use
which tcmplex-panteltje >/dev/null 2>&1
if [ $? = 0 ]; then
	tcp_path=`which tcmplex-panteltje 2>&1`
else
	tcp_path="x"
fi
which tcmplex >/dev/null 2>&1
if [ $? = 0 ]; then
	tc_path=`which tcmplex 2>&1`
else
	tc_path="x"
fi

if [ -x $tcp_path ]; then
	tcbin=tcmplex-panteltje
	tcopt=-0
elif [ -x $tc_path ]; then
	tcbin=tcmplex
	tcopt=-p
else
	cat <<EOF
ERROR: Can't find any sutable mplex utility. You can download
tcmplex-panteltje at http://sunsite.rediris.es/
sites2/ibiblio.org/linux/apps/video/tcmplex-panteltje-0.3.tgz
EOF
exit 1
fi

#pharsing parameters

if [ $# -le 0 ]; then
	echo "Missing parameter!"
	usage
	exit 1
fi

case $1 in
	-h)
		usage
		exit 1
	;;
	-*)
		echo "Missing parameter!"
		usage
		exit 1
	;;
	*)
		input=`echo $1 |sed 's/\\ / /'`
		if [ "$input" = "`basename "$input"`" ]; then
		        input="`pwd`/$1"
		fi
		nev=`basename "$input" .avi`
		shift 1
	;;
esac

while [ "$1"x != "x" ]; do
   case $1 in
      -b|--bitrate)
        bitrate=$2
	shift 1
        ;;
      -s|--cdsize)
      	cdsize="$2"
	shift 1
	;;
      -d|--device)
        dev4="$2"
	shift 1
	;;
      -w|--write)
        burning=1
	;;
      -c|--clean)
        cleaning=1
	;;
      -h|--help)
      usage
        exit 0
	;;
   esac
   shift 1
done

#checking for cd-recording device
if [ "$burning" = 1 ]; then
echo -n "Searching for cdrecorder device... "

if [ `uname -r |cut -d '.' -f 2` = 4 ]; then
	#linux 2.4.x
	dev="dev=$dev4"
	echo "$dev4"
elif [ `uname -r |cut -d '.' -f 2` = 6 ]; then
	#linux 2.6.x
	if [ -e /dev/cdrecorder ]; then
		dev='dev=/dev/cdrecorder'
		echo "/dev/cdrecorder"
	else
		cat <<EOF
ERROR: Device file /dev/cdrecorder not found. Please link your
cd-recording device to /dev/cdrecorder!
Example: 'cd /dev; ln -s hdc cdrecorder'
EOF
		exit 1
	fi
else
	cat <<EOF
ERROR: Linux 2.4 or 2.6 series not found. You can download it at
http://www.kernel.org/ ;-)
EOF
	exit 1
fi

#checking for cdrecord
which cdrecord >/dev/null 2>&1
cdrbin=`which cdrecord 2>/dev/null`
if [ $? != 0 ]; then
	cat <<EOF
ERROR: Can't find cdrecord. You can download it at
ftp://ftp.berlios.de/pub/cdrecord/alpha/cdrtools-2.01a20.tar.gz
EOF
exit 1
else #checking for version >= 2.01a14
	echo -n "Checking for cdrecord version >= 2.01a14... "
	$cdrbin cuefile=a 2>&1 |grep 'Bad Option' >/dev/null 2>&1
	if [ "$?" = 0 ]; then
	cat <<EOF
ERROR: Can't find cdrecord version >= 2.01a14. You can download it at
ftp://ftp.berlios.de/pub/cdrecord/alpha/cdrtools-2.01a20.tar.gz
EOF
	else
	        echo "`$cdrbin -version |cut -d ' ' -f 2`"
	fi
fi
fi

#checking for sub avariable

if [ -f "$nev.sub" ]; then
	subopts=$nev.sub
else
	subopts=''
fi

if [ "x$subopts" = "x" ]; then
	subs=''
else
	subs='-sub '
fi

#checking for what height needed
inputwidth=`mplayer -vo null -ao null "$input" -frames 1 2>/dev/null |grep '=>'|cut -d ' ' -f 5|cut -d x -f 1`
inputheight=`mplayer -vo null -ao null "$input" -frames 1 2>/dev/null |grep '=>'|cut -d ' ' -f 5|cut -d x -f 2`
svcdaspect=`echo -e "scale=10\n1.596/($inputwidth/$inputheight)"|bc /dev/stdin`
height=`echo -e "scale=10\n$svcdaspect*480"|bc /dev/stdin|cut -d . -f 1`

#checking for ratios less than 1.33
istoohigh=`expr $height \> 577`
if [ "$istoohigh" = 1 ]; then
	height=576
fi

#find out the vf options
if [ "$height" = 576 ]; then
	vfopts='-vf scale=480:576'
else
	#-vf processes filters in reverse order
	exy=`echo -e "scale=10\n(576-$height)/2"|bc /dev/stdin|cut -d . -f 1`
	vfopts="-vf scale=480:$height,expand=480:576:0:$exy:1"
	echo "Using filter options: '$vfopts'"
fi

#finish displaying informations
if [ "$burning" = 1 ]; then
#asking for cd
cat <<EOF

Please insert a blank cd in your cdwriter.
(If you are using a rewritable media, 
don't forgot to blank it before using divx2svcd.)
Press any key when your are ready.
EOF
read -n 1 i
fi


###start working###
#encoding
mencoder -ofps 25 -oac lavc "$input" -ovc lavc -lavcopts vcodec=mpeg2video:vbitrate=$bitrate:acodec=mp2:abitrate=128:keyint=25:aspect=4/3:$paraopts -o "${nev}2.avi" -srate 44100 -of mpeg -channels 2 $vfopts $subs "$subopts"

videosize=`$ls -l "${nev}2.avi"|tr -s ' '|cut -d ' ' -f5`
if ! [ `echo $(( $cdsize*1048576 < $videosize ))` = "1" ]; then
	#video is smaller, than $cdsize
	mv ${nev}2.avi ${nev}00.mpg
else
	#splitting
	mplayer -dumpvideo -dumpfile "$nev.m2v" "${nev}2.avi"
	mplayer -dumpaudio -dumpfile "$nev.mp2" "${nev}2.avi"
	rm "${nev}2.avi"
	echo "maxFileSize = $cdsize" > template
	$tcbin -i "$nev.m2v" $tcopt "$nev.mp2" -o "$nev.mpg" -m s -F template
	rm template
	rm "$nev.m2v" "$nev.mp2"
fi

for i in *mpg
do
	nev2=`basename "$i" .mpg`
	#creating images
	vcdimager -t svcd -c "$nev2.cue" -b "$nev2.bin" "$i"
	#burning if needs
	if [ "$burning" = 1 ]; then
		if [ "$firstcd" != 1 ]; then
			cat <<EOF

Please insert an another blank cd in your cdwriter.
Press any key when your are ready.
EOF
			read -n 1 i
		else
			firstcd=2
		fi
		$cdrbin -v -dao $dev speed=12 gracetime=2 driveropts=burnfree -eject cuefile="$nev2.cue"
	fi
	#cleaning if needs
	if [ "$cleaning" = 1 ]; then
		rm -f "$nev2.cue" "$nev2.bin"
	fi
done
rm -f "$nev"*mpg