6
# Part of the tovid suite
7
# =======================
8
# A bash script for doing post-encoding processing on
9
# MPEG video files. Can requantize (shrink) video and
12
# Project homepage: http://www.tovid.org
15
# Copyright (C) 2005 tovid.org <http://www.tovid.org>
17
# This program is free software; you can redistribute it and/or
18
# modify it under the terms of the GNU General Public License
19
# as published by the Free Software Foundation; either
20
# version 2 of the License, or (at your option) any later
23
# This program is distributed in the hope that it will be useful,
24
# but WITHOUT ANY WARRANTY; without even the implied warranty of
25
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
# GNU General Public License for more details.
28
# You should have received a copy of the GNU General Public License
29
# along with this program; if not, write to the Free Software
30
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Or see:
32
# http://www.gnu.org/licenses/gpl.txt
34
SCRIPT_NAME=`cat << EOF
35
--------------------------------
37
Video postprocessing script
38
Part of the tovid suite, version $TOVID_VERSION
40
--------------------------------
44
Usage: postproc [OPTIONS] {input file} {output file}
46
Where OPTIONS may be any of the following:
48
-audiodelay NUM Delay audio by NUM milliseconds
49
-normalize Normalize the audio
50
-amplitudeNUM[dB] Apply a gain of NUM (0 - 1.0, add dB for decibel gain)
51
-shrink NUM Shrink video by factor NUM (1.0 - 2.0)
52
-parallel Run all processes in parallel (can save time)
53
-debug Keep a log of actions and output
55
See the postproc manual page ('man postproc') for additional documentation.
59
SEPARATOR="========================================================="
62
TMP_FILE="postproc.fileinfo.$PPID"
70
# File to use for saving postprocessing statistics
72
STAT_FILE="$STAT_DIR/stats.postproc"
79
# Print script name, usage notes, and optional error message, then exit.
80
# Args: $1 == text string containing error message
89
# Remove temporary files
93
rm -fv video_shrink video_dump audio_dump normed_audio
98
# ***********************************
99
# EXECUTION BEGINS HERE
100
# ***********************************
104
# Make sure there are at least two arguments (infile and outfile)
105
if test $# -lt 2; then
106
usage_error "Error: Please provide an input filename and an output filename."
109
while test ${1:0:1} = "-"; do
113
AUDIO_DELAY="-O ${1}ms"
119
assert_dep "$transcode" "You are missing dependencies required for shrinking video!"
134
AUDIO_AMPLITUDE="--amplitude=$1"
153
if $DO_NORM && $PARALLEL; then
154
echo "Cannot normalize audio in parallel mode! Turning off -parallel."
159
# Figure out what format it is. 44.1khz audio can be VCD or SVCD,
160
# 48khz audio is DVD.
161
echo "Gathering file information. This may take a while."
162
if eval $(idvid -terse "$IN_FILE"); then :; else
163
echo "Could not identify source video: $IN_FILE"
167
if test $ID_AUDIO_RATE -eq "48000"; then
170
elif test $ID_AUDIO_RATE -eq "44100"; then
171
# VCD is 352 wide, SVCD is 480 wide
172
if test $ID_VIDEO_WIDTH -eq "352"; then
175
elif test $ID_VIDEO_WIDTH -eq "480"; then
181
echo "Dumping audio and video streams with the following commands:"
183
# Dump audio and video
185
AUDIO_DUMP="mplayer -quiet -vc null -vo null -ao pcm:waveheader:file=audio_dump \"$IN_FILE\""
187
AUDIO_DUMP="mplayer \"$IN_FILE\" -quiet -dumpaudio -dumpfile audio_dump"
189
VIDEO_DUMP="mplayer \"$IN_FILE\" -quiet -dumpvideo -dumpfile video_dump"
190
VID_STREAM="video_dump"
194
# Should always be safe to put audio decode in the background
195
eval $AUDIO_DUMP > $TMP_FILE 2>&1 &
196
eval $VIDEO_DUMP >> $TMP_FILE 2>&1 $BACKGR
198
# Let the audio process finish if not in parallel mode (should always finish
199
# before video, so this is just precautionary)
204
AUDIO_NORM="normalize $AUDIO_AMPLITUDE audio_dump"
206
echo "Normalizing audio/applying gain with the following command:"
211
mv -v audio_dump normed_audio
212
AUDIO_BITRATE=$(expr $ID_AUDIO_BITRATE / 1000)
213
AUDIO_ENC="ffmpeg -i normed_audio -vn -ab ${AUDIO_BITRATE}k -ar $ID_AUDIO_RATE"
214
AUDIO_ENC="$AUDIO_ENC -ac $ID_AUDIO_NCH -acodec $ID_AUDIO_CODEC"
215
AUDIO_ENC="$AUDIO_ENC -y audio_dump.$ID_AUDIO_CODEC"
217
echo "Encoding the normalized/gained audio stream with the following command:"
221
mv -v audio_dump.$ID_AUDIO_CODEC audio_dump
224
# Shrink, if requested
227
if test $VIDEO_FORMAT = "VCD"; then
229
echo "This file appears to be in VCD format. Unfortunately, the"
230
echo "VCD specification does not allow shrinking (requantization)."
231
echo "Shrinking will be skipped."
234
START_SIZE=$( ls -l video_dump | awk '{print $5}' )
236
echo "Shrinking video stream by a factor of $SHRINK_FACTOR"
237
SHRINK_CMD="tcrequant -i video_dump -o video_shrink -f $SHRINK_FACTOR"
239
eval $SHRINK_CMD $BACKGR
240
VID_STREAM="video_shrink"
245
SHRINK_SIZE=$( ls -l video_shrink | awk '{print $5}' )
246
SHRINK_PERCENT=$(expr 100 \- \( 100 \* $SHRINK_SIZE \/ $START_SIZE \))
247
echo "Video stream was shrunk by $SHRINK_PERCENT%"
254
# Multiplex audio and video back together
255
echo "Multiplexing audio and video streams with the following command:"
256
MPLEX_CMD="mplex $VID_STREAM audio_dump -o \"$OUT_FILE\" $MUX_OPTS $AUDIO_DELAY"
258
eval $MPLEX_CMD >> $TMP_FILE 2>&1
260
# For parallel, compare infile and outfile sizes
262
START_SIZE=$( ls -l "$IN_FILE" | awk '{print $5}' )
263
SHRINK_SIZE=$( ls -l "$OUT_FILE" | awk '{print $5}' )
264
SHRINK_PERCENT=$(expr 100 \- \( 100 \* $SHRINK_SIZE \/ $START_SIZE \))
265
echo "Video stream was shrunk by $SHRINK_PERCENT%"
272
# Create stats directory and save video statistics
274
FINAL_STATS=`cat << EOF
275
postproc $TOVID_VERSION
277
Shrink factor: $SHRINK_FACTOR
278
Shrunk by: $SHRINK_PERCENT%
280
echo $"$FINAL_STATS" >> $STAT_FILE
282
# If doing debugging, keep log and print message
284
echo "All output has been saved in the file: $TMP_FILE."
289
echo "Done. Thanks for using postproc!"