~ubuntu-branches/ubuntu/karmic/tovid/karmic

« back to all changes in this revision

Viewing changes to src/postproc

  • Committer: Bazaar Package Importer
  • Author(s): Matvey Kozhev
  • Date: 2008-01-24 22:04:40 UTC
  • Revision ID: james.westby@ubuntu.com-20080124220440-x7cheljduf1rdgnq
Tags: upstream-0.31
ImportĀ upstreamĀ versionĀ 0.31

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env bash
 
2
ME="[postproc]:"
 
3
. tovid-init
 
4
 
 
5
# postproc
 
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
 
10
# adjust A/V sync.
 
11
#
 
12
# Project homepage: http://www.tovid.org
 
13
#
 
14
#
 
15
# Copyright (C) 2005 tovid.org <http://www.tovid.org>
 
16
 
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 
 
21
# version.
 
22
 
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.
 
27
 
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:
 
31
#
 
32
#           http://www.gnu.org/licenses/gpl.txt
 
33
 
 
34
SCRIPT_NAME=`cat << EOF
 
35
--------------------------------
 
36
postproc
 
37
Video postprocessing script
 
38
Part of the tovid suite, version $TOVID_VERSION
 
39
$TOVID_HOME_PAGE
 
40
--------------------------------
 
41
EOF`
 
42
 
 
43
USAGE=`cat << EOF
 
44
Usage: postproc [OPTIONS] {input file} {output file}
 
45
 
 
46
Where OPTIONS may be any of the following:
 
47
 
 
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
 
54
 
 
55
See the postproc manual page ('man postproc') for additional documentation.
 
56
 
 
57
EOF`
 
58
 
 
59
SEPARATOR="========================================================="
 
60
 
 
61
# Default values
 
62
TMP_FILE="postproc.fileinfo.$PPID"
 
63
AUDIO_DELAY=""
 
64
SHRINK_FACTOR=""
 
65
DO_NORM=false
 
66
AUDIO_AMPLITUDE=""
 
67
DO_SHRINK=false
 
68
MUX_OPTS=""
 
69
DEBUG=false
 
70
# File to use for saving postprocessing statistics
 
71
STAT_DIR=$HOME/.tovid
 
72
STAT_FILE="$STAT_DIR/stats.postproc"
 
73
SHRINK_PERCENT="0"
 
74
PARALLEL=false
 
75
# Video format in use
 
76
VIDEO_FORMAT="DVD"
 
77
BACKGR=""
 
78
 
 
79
# Print script name, usage notes, and optional error message, then exit.
 
80
# Args: $1 == text string containing error message
 
81
usage_error ()
 
82
{
 
83
  echo "$USAGE"
 
84
  echo "$SEPARATOR"
 
85
  echo "$@"
 
86
  exit 1
 
87
}
 
88
 
 
89
# Remove temporary files
 
90
cleanup ()
 
91
{
 
92
    echo "Cleaning up..."
 
93
    rm -fv video_shrink video_dump audio_dump normed_audio
 
94
 
 
95
}
 
96
 
 
97
 
 
98
# ***********************************
 
99
# EXECUTION BEGINS HERE
 
100
# ***********************************
 
101
 
 
102
echo "$SCRIPT_NAME"
 
103
 
 
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."
 
107
fi
 
108
 
 
109
while test ${1:0:1} = "-"; do
 
110
    case "$1" in
 
111
        "-audiodelay" )
 
112
            shift
 
113
            AUDIO_DELAY="-O ${1}ms"
 
114
            ;;
 
115
        "-shrink" )
 
116
            shift
 
117
            DO_SHRINK=:
 
118
            SHRINK_FACTOR="$1"
 
119
            assert_dep "$transcode" "You are missing dependencies required for shrinking video!"
 
120
            ;;
 
121
        "-parallel" )
 
122
            PARALLEL=:
 
123
            cleanup
 
124
            mkfifo video_dump
 
125
            mkfifo audio_dump
 
126
            mkfifo video_shrink
 
127
            BACKGR="&"
 
128
            ;;
 
129
        "-normalize" )
 
130
            DO_NORM=:
 
131
            ;;
 
132
        "-amplitude" )
 
133
            shift
 
134
            AUDIO_AMPLITUDE="--amplitude=$1"
 
135
            DO_NORM=:
 
136
            ;;
 
137
        "-debug" )
 
138
            DEBUG=:
 
139
            ;;
 
140
    esac
 
141
 
 
142
    # Get next argument
 
143
    shift
 
144
done
 
145
 
 
146
 
 
147
IN_FILE="$1"
 
148
shift
 
149
OUT_FILE="$1"
 
150
 
 
151
echo $SEPARATOR
 
152
 
 
153
if $DO_NORM && $PARALLEL; then
 
154
   echo "Cannot normalize audio in parallel mode! Turning off -parallel."
 
155
   PARALLEL=false
 
156
   cleanup
 
157
fi
 
158
 
 
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"
 
164
    exit 1
 
165
fi
 
166
 
 
167
if test $ID_AUDIO_RATE -eq "48000"; then
 
168
    MUX_OPTS="-V -f 8"
 
169
    VIDEO_FORMAT="DVD"
 
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
 
173
        MUX_OPTS="-f 1"
 
174
        VIDEO_FORMAT="VCD"
 
175
    elif test $ID_VIDEO_WIDTH -eq "480"; then
 
176
        MUX_OPTS="-V -f 4"
 
177
        VIDEO_FORMAT="SVCD"
 
178
    fi
 
179
fi
 
180
 
 
181
echo "Dumping audio and video streams with the following commands:"
 
182
 
 
183
# Dump audio and video
 
184
if $DO_NORM; then
 
185
    AUDIO_DUMP="mplayer -quiet -vc null -vo null -ao pcm:waveheader:file=audio_dump \"$IN_FILE\""
 
186
else
 
187
    AUDIO_DUMP="mplayer \"$IN_FILE\" -quiet -dumpaudio -dumpfile audio_dump"
 
188
fi
 
189
VIDEO_DUMP="mplayer \"$IN_FILE\" -quiet -dumpvideo -dumpfile video_dump"
 
190
VID_STREAM="video_dump"
 
191
 
 
192
echo "$AUDIO_DUMP"
 
193
echo "$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
 
197
 
 
198
# Let the audio process finish if not in parallel mode (should always finish 
 
199
# before video, so this is just precautionary)
 
200
$PARALLEL || wait
 
201
 
 
202
# Normalize/gain
 
203
if $DO_NORM; then
 
204
   AUDIO_NORM="normalize $AUDIO_AMPLITUDE audio_dump"
 
205
   
 
206
   echo "Normalizing audio/applying gain with the following command:"
 
207
   echo "$AUDIO_NORM"
 
208
   eval $AUDIO_NORM
 
209
 
 
210
   # Re-encode audio
 
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"
 
216
 
 
217
   echo "Encoding the normalized/gained audio stream with the following command:"
 
218
   echo "$AUDIO_ENC"
 
219
   eval $AUDIO_ENC
 
220
 
 
221
   mv -v audio_dump.$ID_AUDIO_CODEC audio_dump
 
222
fi
 
223
 
 
224
# Shrink, if requested
 
225
if $DO_SHRINK; then
 
226
    # Can't shrink VCD
 
227
    if test $VIDEO_FORMAT = "VCD"; then
 
228
        echo $SEPARATOR
 
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."
 
232
        echo $SEPARATOR
 
233
    else
 
234
        START_SIZE=$( ls -l video_dump | awk '{print $5}' )
 
235
        echo $SEPARATOR
 
236
        echo "Shrinking video stream by a factor of $SHRINK_FACTOR"
 
237
        SHRINK_CMD="tcrequant -i video_dump -o video_shrink -f $SHRINK_FACTOR"
 
238
        echo "$SHRINK_CMD"
 
239
        eval $SHRINK_CMD $BACKGR
 
240
        VID_STREAM="video_shrink"
 
241
        if $PARALLEL; then
 
242
            :
 
243
        else
 
244
            wait
 
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%"
 
248
        fi
 
249
    fi
 
250
fi
 
251
 
 
252
echo $SEPARATOR
 
253
 
 
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"
 
257
echo "$MPLEX_CMD"
 
258
eval $MPLEX_CMD >> $TMP_FILE 2>&1
 
259
 
 
260
# For parallel, compare infile and outfile sizes
 
261
if $PARALLEL; then
 
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%"
 
266
fi  
 
267
  
 
268
echo $SEPARATOR
 
269
 
 
270
cleanup
 
271
 
 
272
# Create stats directory and save video statistics
 
273
mkdir -p $STAT_DIR
 
274
FINAL_STATS=`cat << EOF
 
275
postproc $TOVID_VERSION
 
276
File: $OUT_FILE
 
277
Shrink factor: $SHRINK_FACTOR
 
278
Shrunk by: $SHRINK_PERCENT%
 
279
EOF`
 
280
echo $"$FINAL_STATS" >> $STAT_FILE
 
281
 
 
282
# If doing debugging, keep log and print message
 
283
if $DEBUG; then
 
284
  echo "All output has been saved in the file: $TMP_FILE."
 
285
else
 
286
  rm $TMP_FILE
 
287
fi
 
288
 
 
289
echo "Done. Thanks for using postproc!"