1#!/usr/local/bin/bash
2
3# (c) 2003 Vajna Miklos <mainroot@freemail.hu>
4# divx2svcd for MPlayer
5# distributed under GPL License
6
7# simple utility that creates a SVCD from a video in an AVI container
8
9# The newest version of this utility can be found at
10# http://vmiklos.uw.hu/divx2svcd/divx2svcd
11# MPlayer available at
12# http://www.mplayerhq.hu/MPlayer/releases/MPlayer-1.0pre3try2.tar.bz2
13
14###changelog###
15#nobody cares about it :-)
16cat >/dev/null <<EOF
170.5.1
18- faster code by not re-mplexing one-cd-size or smaller videos
19
200.5.0
21- needless for dumpvideo patch ;-)
22
230.4.9
24- changed default bitrate to 1600 for better quality
25- fix for burning more than one cd
26- fix for wrong parameter help
27
280.4.8
29- small fixes
30
310.4.7
32- fixed bug, when there is no sub available
33
340.4.6
35- support for burning the svcd with cdrecord
36- lots of paranoid options for better quality from Denes Balatoni
37
380.4.5
39- support for filenames including spaces
40
410.4.4
42- support for checking all applications this script uses
43- this changelog
44
450.4.3
46- advanced detectation of movie aspect (mpeg4 codec, mpeg container)
47
480.4.2
49- advanced vf options for movies with non-standard aspect
50
510.4.1
52- checking for available sub
53
540.4.0
55- support for tcmplex-panteltje
56- support for libavcodec audio encoder
57
580.3.1-0.3.2
59- small fixes
60
610.3
62- almost totally rewritten from scratch
63  based on the idea of Denes Balatoni <pnis@coder.hu>
64- support for toolame instead of mp2enc
65- suppert for libavcodec mpeg2video codec instead of mpeg2enc
66
670.2
68- support for tcmplex instead of mplex
69
700.1rc2-rc4
71- small bugfixes
72
730.1rc1
74- initial release
75
76EOF
77
78
79###preparing###
80#help
81
82usage()
83{
84	cat <<EOF
85Usage: `basename $0` input_avi [options]
86
87Options:
88-b|--bitrate xx	bitrate of mp2 video stream [1375]
89-s|--cdsize xx	size of the cd we split the video to [795]
90-w|--writecd	enables burning [disable]
91-d|--device xx	scsi cd-recording device if you are using linux 2.4.x [0,0,0]
92-c|--clean	clean up svcd images you just created
93-h|--help	this help screen
94EOF
95
96}
97
98#initializating constants
99version='0.5.1'
100bitrate=1375
101cdsize=795
102burning=0
103cleaning=0
104dev4='0,0,0'
105firstcd=1
106
107#paranoid options
108paraopts='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'
109
110#header
111echo "DivX2SvcD $version (C) 2003-2004 Vajna Miklos"
112echo
113
114#checking for ls
115ls=`which ls`
116
117#checking for bc
118which bc >/dev/null 2>&1
119bcbin=`which bc 2>/dev/null`
120if [ $? != 0 ]; then
121	cat <<EOF
122ERROR: Can't find bc. You can download it at
123ftp://ftp.ibiblio.org/pub/gnu/bc/bc-1.06.tar.gz
124EOF
125exit 1
126fi
127
128#checking for vcdimager
129which vcdimager >/dev/null 2>&1
130bcbin=`which vcdimager 2>/dev/null`
131if [ $? != 0 ]; then
132	cat <<EOF
133ERROR: Can't find vcdimager. You can download it at http://www.vcdimager.org
134/pub/vcdimager/vcdimager-0.7_UNSTABLE/vcdimager-0.7.14.tar.gz
135EOF
136exit 1
137fi
138
139#checking which mplex utility we have to use
140which tcmplex-panteltje >/dev/null 2>&1
141if [ $? = 0 ]; then
142	tcp_path=`which tcmplex-panteltje 2>&1`
143else
144	tcp_path="x"
145fi
146which tcmplex >/dev/null 2>&1
147if [ $? = 0 ]; then
148	tc_path=`which tcmplex 2>&1`
149else
150	tc_path="x"
151fi
152
153if [ -x $tcp_path ]; then
154	tcbin=tcmplex-panteltje
155	tcopt=-0
156elif [ -x $tc_path ]; then
157	tcbin=tcmplex
158	tcopt=-p
159else
160	cat <<EOF
161ERROR: Can't find any sutable mplex utility. You can download
162tcmplex-panteltje at http://sunsite.rediris.es/
163sites2/ibiblio.org/linux/apps/video/tcmplex-panteltje-0.3.tgz
164EOF
165exit 1
166fi
167
168#pharsing parameters
169
170if [ $# -le 0 ]; then
171	echo "Missing parameter!"
172	usage
173	exit 1
174fi
175
176case $1 in
177	-h)
178		usage
179		exit 1
180	;;
181	-*)
182		echo "Missing parameter!"
183		usage
184		exit 1
185	;;
186	*)
187		input=`echo $1 |sed 's/\\ / /'`
188		if [ "$input" = "`basename "$input"`" ]; then
189		        input="`pwd`/$1"
190		fi
191		nev=`basename "$input" .avi`
192		shift 1
193	;;
194esac
195
196while [ "$1"x != "x" ]; do
197   case $1 in
198      -b|--bitrate)
199        bitrate=$2
200	shift 1
201        ;;
202      -s|--cdsize)
203      	cdsize="$2"
204	shift 1
205	;;
206      -d|--device)
207        dev4="$2"
208	shift 1
209	;;
210      -w|--write)
211        burning=1
212	;;
213      -c|--clean)
214        cleaning=1
215	;;
216      -h|--help)
217      usage
218        exit 0
219	;;
220   esac
221   shift 1
222done
223
224#checking for cd-recording device
225if [ "$burning" = 1 ]; then
226echo -n "Searching for cdrecorder device... "
227
228if [ `uname -r |cut -d '.' -f 2` = 4 ]; then
229	#linux 2.4.x
230	dev="dev=$dev4"
231	echo "$dev4"
232elif [ `uname -r |cut -d '.' -f 2` = 6 ]; then
233	#linux 2.6.x
234	if [ -e /dev/cdrecorder ]; then
235		dev='dev=/dev/cdrecorder'
236		echo "/dev/cdrecorder"
237	else
238		cat <<EOF
239ERROR: Device file /dev/cdrecorder not found. Please link your
240cd-recording device to /dev/cdrecorder!
241Example: 'cd /dev; ln -s hdc cdrecorder'
242EOF
243		exit 1
244	fi
245else
246	cat <<EOF
247ERROR: Linux 2.4 or 2.6 series not found. You can download it at
248http://www.kernel.org/ ;-)
249EOF
250	exit 1
251fi
252
253#checking for cdrecord
254which cdrecord >/dev/null 2>&1
255cdrbin=`which cdrecord 2>/dev/null`
256if [ $? != 0 ]; then
257	cat <<EOF
258ERROR: Can't find cdrecord. You can download it at
259ftp://ftp.berlios.de/pub/cdrecord/alpha/cdrtools-2.01a20.tar.gz
260EOF
261exit 1
262else #checking for version >= 2.01a14
263	echo -n "Checking for cdrecord version >= 2.01a14... "
264	$cdrbin cuefile=a 2>&1 |grep 'Bad Option' >/dev/null 2>&1
265	if [ "$?" = 0 ]; then
266	cat <<EOF
267ERROR: Can't find cdrecord version >= 2.01a14. You can download it at
268ftp://ftp.berlios.de/pub/cdrecord/alpha/cdrtools-2.01a20.tar.gz
269EOF
270	else
271	        echo "`$cdrbin -version |cut -d ' ' -f 2`"
272	fi
273fi
274fi
275
276#checking for sub avariable
277
278if [ -f "$nev.sub" ]; then
279	subopts=$nev.sub
280else
281	subopts=''
282fi
283
284if [ "x$subopts" = "x" ]; then
285	subs=''
286else
287	subs='-sub '
288fi
289
290#checking for what height needed
291inputwidth=`mplayer -vo null -ao null "$input" -frames 1 2>/dev/null |grep '=>'|cut -d ' ' -f 5|cut -d x -f 1`
292inputheight=`mplayer -vo null -ao null "$input" -frames 1 2>/dev/null |grep '=>'|cut -d ' ' -f 5|cut -d x -f 2`
293svcdaspect=`echo -e "scale=10\n1.596/($inputwidth/$inputheight)"|bc /dev/stdin`
294height=`echo -e "scale=10\n$svcdaspect*480"|bc /dev/stdin|cut -d . -f 1`
295
296#checking for ratios less than 1.33
297istoohigh=`expr $height \> 577`
298if [ "$istoohigh" = 1 ]; then
299	height=576
300fi
301
302#find out the vf options
303if [ "$height" = 576 ]; then
304	vfopts='-vf scale=480:576'
305else
306	#-vf processes filters in reverse order
307	exy=`echo -e "scale=10\n(576-$height)/2"|bc /dev/stdin|cut -d . -f 1`
308	vfopts="-vf scale=480:$height,expand=480:576:0:$exy:1"
309	echo "Using filter options: '$vfopts'"
310fi
311
312#finish displaying informations
313if [ "$burning" = 1 ]; then
314#asking for cd
315cat <<EOF
316
317Please insert a blank cd in your cdwriter.
318(If you are using a rewritable media,
319don't forgot to blank it before using divx2svcd.)
320Press any key when your are ready.
321EOF
322read -n 1 i
323fi
324
325
326###start working###
327#encoding
328mencoder -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"
329
330videosize=`$ls -l "${nev}2.avi"|tr -s ' '|cut -d ' ' -f5`
331if ! [ `echo $(( $cdsize*1048576 < $videosize ))` = "1" ]; then
332	#video is smaller, than $cdsize
333	mv ${nev}2.avi ${nev}00.mpg
334else
335	#splitting
336	mplayer -dumpvideo -dumpfile "$nev.m2v" "${nev}2.avi"
337	mplayer -dumpaudio -dumpfile "$nev.mp2" "${nev}2.avi"
338	rm "${nev}2.avi"
339	echo "maxFileSize = $cdsize" > template
340	$tcbin -i "$nev.m2v" $tcopt "$nev.mp2" -o "$nev.mpg" -m s -F template
341	rm template
342	rm "$nev.m2v" "$nev.mp2"
343fi
344
345for i in *mpg
346do
347	nev2=`basename "$i" .mpg`
348	#creating images
349	vcdimager -t svcd -c "$nev2.cue" -b "$nev2.bin" "$i"
350	#burning if needs
351	if [ "$burning" = 1 ]; then
352		if [ "$firstcd" != 1 ]; then
353			cat <<EOF
354
355Please insert an another blank cd in your cdwriter.
356Press any key when your are ready.
357EOF
358			read -n 1 i
359		else
360			firstcd=2
361		fi
362		$cdrbin -v -dao $dev speed=12 gracetime=2 driveropts=burnfree -eject cuefile="$nev2.cue"
363	fi
364	#cleaning if needs
365	if [ "$cleaning" = 1 ]; then
366		rm -f "$nev2.cue" "$nev2.bin"
367	fi
368done
369rm -f "$nev"*mpg
370