summaryrefslogtreecommitdiffstats
path: root/TOOLS/qepdvcd.sh
blob: a436e17c375c7e449b36bd300a45887ef2e6d9ef (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
#!/bin/bash
#
# QEPDVCD
#
# Most of this stuff comes straight from MPlayer documentation.
# Options are limited only to a small useful subset, if you 
# want more control, RTFM and DIY.
#
# Version:          0.1
#
# Licence:          GPL
#
# Author:           Reynaldo H. Verdejo Pinochet <reynaldo@opendot.cl> 
#
# Script:           MPlayer Sources. Anything supported to vcd/svcd pal/ntsc 
#
# requires:         mencoder
#
# sugests:          vcdimager / cdrecord 
#
# Thanks to:        Carlos Navarro Salas - name author ;)
#					
#

# Defaults

MYNAME=`basename $0`
TARGET="svcd"
ENCQ="2"
ABPS="224"
VBPS="2000"
NORM="NTSC"
SPLIT="0"
TOLERANCE="85"
OUTNAME="mencodedvcd"
SUBFILENAME=0

EDLFILENAME="pass.edl"
HAVESUB=0
WORKDIR="."
RATIOX=4
RATIOY=3

OPTIONS="INPUTFILENAME TARGET ENCQ ABPS VBPS NORM SPLIT TOLERANCE OUTNAME"

usage()
{
echo ""
echo "usage $MYNAME inputfile [options]"
echo ""
echo "Options: [Default value]"
echo ""
echo "-t	Target svcd|vcd [svcd]"
echo "-q	Encoding quality 0|1|2 [2]"
echo "-a	Audio bitrate in kbps [224]"
echo "-v	Video bitrate in kbps [2000 For SVCD, 1150 For VCD]"
echo "-n	Norm NTSC|PAL [NTSC]"
echo "-d	Divide/split movie at given times time1:time2:... [no split]"
echo "-s	Shitty TV screen tolerance %, afects subtitle positioning [85]"
echo "-u	Subtitle file name [No subtitle]"
echo "-o	Output basename [mencodedvcd]"
echo ""
echo "In case you want to use -a/-v please read:"
echo "http://www.mplayerhq.hu/DOCS/HTML/en/menc-feat-vcd-dvd.html"
echo ""
}

test_needed()
{

for i in mencoder; do
	if [ -z "`which $i`" ]; then
		echo "[ERROR] mencoder not found in $PATH!"
		exit 1
	fi
done

}

test_sugested()
{

for i in vcdimager cdrecord; do
	if [ -z "`which $i`" ]; then
		echo "[WARNING] $i not found in $PATH!"
		echo "You'll likely need it after we finish."
		exit 1
	fi
done
}

test_needed
test_sugested

if [ $# -lt 1 ]; then
	echo ""
	echo "[ERROR] Input filename parameter is mandatory."
	echo ""
	usage
	exit 1
fi

case $1 in
	-*)
 		usage
		exit 1
		;;
	*)
		INPUTFILENAME=$1
		shift 1
		;;
esac

while [ "$1"x != "x" ]; do
	case $1 in
		-t)
		TARGET=$2
		shift 1
		;;
		-q)
		ENCQ=$2
		shift 1
		;;
		-a)
		ABPS=$2
		shift 1
		;;
		-v)
		VBPS=$2
		shift 1
		;;
		-n)
		NORM=$2
		shift 1
		;;
		-d)
		SPLIT=$2
		shift 1
		;;
		-s)
		TOLERANCE=$2
		shift 1
		;;
		-u)
		SUBFILENAME="$2"
		HAVESUB=1
		shift 1
		;;
		-o)
		OUTNAME=$2
		shift 1
		;;
	esac
	shift 1
done

echo ""
echo "[STATUS] Will re-encode using the following parameters:"
echo ""
for i in $OPTIONS ; do
	echo "$i ${!i}";
done

# Parameter Sanity Check ###########################################
# We need to check supplied params against known format constraints
####################################################################

if [ $TARGET = "svcd" ]; then
	if [ $ABPS -gt 384 ]; then
		echo "[ERROR] SVCD maximum abitrate is 384kbps."
		exit 1
	fi
	if [ $VBPS -gt 2600 ]; then
		echo "[ERROR] SVCD maximum vbitrate is 2600kbps."
		exit 1
	fi
else [ $TARGET = "vcd" ]
	if [ $ABPS -eq 224 ]; then
		echo "[ERROR] VCD abitrate must be 224kbps."
		exit 1
	fi
	if [ $VBPS -gt 1150 ]; then
		echo "[ERROR] VCD maximum vbitrate is 1150kbps."
		exit 1
	fi
fi

# Set encoding options ##############################################

if [ $TARGET = "svcd" ]; then
	FORMAT="xsvcd"
	VCODEC="mpeg2video"
	VRCMINRATE=4
	VRCMAXRATE=2500
	VRCBUFSIZE=917
	if [ $NORM = "NTSC" ]; then
		SCALEX=480
		SCALEY=480
		KEYINT=18
		OFPS="24000/1001"
		TELECINE=":telecine"
	else [ $NORM = "PAL" ]
		SCALEX=480
		SCALEY=576
		KEYINT=15
		OFPS=25
	fi
else [ $TARGET = "vcd" ]
	FORMAT="xvcd"
	VCODEC="mpeg1video"
	VRCMINRATE=$VBPS
	VRCMAXRATE=$VBPS
	VRCBUFSIZE=327
	if [ $NORM = "NTSC" ]; then
		SCALEX=352
		SCALEY=240
		KEYINT=18
		OFPS="24000/1001"
	else [ $NORM = "PAL" ]
		SCALEX=352
		SCALEY=288
		KEYINT=15
		OFPS=25
	fi
fi

# Start reencoding ###################################################

cd $WORKDIR

if [ $(($HAVESUB+1)) -eq 1 ]; then
	SUBTITLESTRING="/dev/null"
else
	SUBTITLESTRING="$SUBFILENAME"
fi

if [ $SPLIT = "0" ]; then
	CICLES=0
	TIMESTRING=""
else
	if [ -e $EDLFILENAME ]; then
		echo "[ERROR]"
		echo "The -d option needs to generate a temporary file called"
		echo "$EDLFILENAME. You already have one in this directory,"
		echo "please remove/rename it and run $MYNAME again."
		echo ""
		exit 1
	else
		EDLSTRING=$(echo $SPLIT | sed -e s/:/' '/g)
		EDLARRAY=($EDLSTRING)
		CICLES=$(echo $EDLSTRING | wc -w)
		TIMESTRING="-edl $EDLFILENAME -hr-edl-seek"
	fi
fi


for j in $(seq 0 $CICLES); do

	NEWNAME=$OUTNAME"_PART"$j".mpg"
	echo ""
	echo "Making $NEWNAME, wish me luck ;-)"
	echo ""

# Create EDLFILENAME #################################################

	if [ $CICLES -ge 1 ]; then
		for i in $(seq 0 $CICLES)
		do
			if [ $i -eq $j ]; then
				if [ $j -ne 0 ]; then
					echo "0 ${EDLARRAY[$(($i-1))]} 0" > $EDLFILENAME
				fi
				if [ $i -ne $CICLES ]; then
					echo "${EDLARRAY[$(($i))]} 999999 0" >> $EDLFILENAME
				fi
			fi
		done
	fi
		
# Mencoder Time ;-) ###################################################

mencoder \
-ovc lavc -oac lavc -vf expand=:::::$RATIOX/$RATIOY:1,scale=$SCALEX:$SCALEY,harddup \
-srate 44100 -af lavcresample=44100 -lavcopts acodec=mp2:abitrate=$ABPS:vcodec=$VCODEC:\
vbitrate=$VBPS:keyint=$KEYINT:mbd=$ENCQ:vrc_buf_size=$VRCBUFSIZE:vrc_maxrate=$VRCMAXRATE:\
vrc_minrate=$VRCMINRATE:vi_qfactor=0.1:vi_qoffset=1.5:aspect=$RATIOX/$RATIOY -of mpeg \
-mpegopts format=$FORMAT$TELECINE -sub $SUBTITLESTRING -subpos $TOLERANCE -subwidth \
$TOLERANCE -ofps $OFPS $TIMESTRING -o $NEWNAME $INPUTFILENAME

echo "Encoding of $NEWNAME finished."
echo "Run vcdimager -t svcd/vcd $NEWNAME and burn with cdrecord."

done

echo "$(($CICLES+1)) VCD/SVCD file(s) created!!!"
echo "Happy to be of some help ;-) have fun."
exit 0