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

« back to all changes in this revision

Viewing changes to src/previd

  • 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="[previd]:"
 
3
. tovid-init
 
4
 
 
5
# previd
 
6
# Part of the tovid suite
 
7
# =======================
 
8
# Produce a short fast-forward-style preview of a video file.
 
9
#
 
10
# Project homepage: http://www.tovid.org
 
11
#
 
12
#
 
13
# Copyright (C) 2005 tovid.org <http://www.tovid.org>
 
14
 
15
# This program is free software; you can redistribute it and/or 
 
16
# modify it under the terms of the GNU General Public License 
 
17
# as published by the Free Software Foundation; either 
 
18
# version 2 of the License, or (at your option) any later 
 
19
# version.
 
20
 
21
# This program is distributed in the hope that it will be useful,
 
22
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
23
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
24
# GNU General Public License for more details.
 
25
 
26
# You should have received a copy of the GNU General Public License
 
27
# along with this program; if not, write to the Free Software
 
28
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Or see:
 
29
#
 
30
#           http://www.gnu.org/licenses/gpl.txt
 
31
 
 
32
# Author: Rick Measham aka Woosta: rick@measham.id.au
 
33
 
 
34
USAGE=`cat << EOF
 
35
Usage: previd [OPTIONS] -in {input file} -out {output file}
 
36
 
 
37
OPTIONS may be any of the following:
 
38
 
 
39
    -v, -version    Prints previd version number only
 
40
    -x NUM          
 
41
    -y NUM          Sets width and height. Use multiples of 16 
 
42
    -sstep NUM      Skip NUM seconds between frames 
 
43
    -fps NUM        Show each frame NUM times.
 
44
 
 
45
EOF`
 
46
 
 
47
SEPARATOR="========================================================="
 
48
 
 
49
PREVID_VERSION=0.1
 
50
 
 
51
WIDTH=144
 
52
HEIGHT=128
 
53
STEP=240
 
54
FRAMES_PER_STEP=4
 
55
 
 
56
INFILE=""
 
57
OUTFILE=""
 
58
 
 
59
TMP_DIR=$(tempdir previd)
 
60
CWD=$(pwd)
 
61
# Make temp dir absolute
 
62
TMP_DIR="${CWD}/${TMP_DIR}"
 
63
LOG_FILE="${TMP_DIR}/previd.log"
 
64
 
 
65
# ******************************************************************************
 
66
# Print usage notes and optional error message, then exit.
 
67
# Args: $@ == text string containing error message 
 
68
# ******************************************************************************
 
69
usage_error()
 
70
{
 
71
    printf "%s\n" "$USAGE"
 
72
    printf "%s\n" "$SEPARATOR"
 
73
    echo $@
 
74
    rm -rf $TMP_DIR
 
75
    exit 1
 
76
}
 
77
 
 
78
# Step 0: Get the parameters
 
79
while test $# -gt 0; do
 
80
        case "$1" in
 
81
                "-v" | "-version" )
 
82
                        echo $PREVID_VERSION
 
83
                        rm -rf $TMP_DIR
 
84
                        exit 0
 
85
                        ;;
 
86
 
 
87
                "-in" )
 
88
                        shift
 
89
                        INFILE="$1"
 
90
                        ;;
 
91
 
 
92
                "-out" )
 
93
                        shift
 
94
                        OUTFILE="$1"
 
95
                        ;;
 
96
 
 
97
                "-x" )
 
98
                        shift
 
99
                        WIDTH="$1"
 
100
                        if test $(expr $WIDTH % 16) -ne 0; then
 
101
                                echo "-x must be a multiple of 16"
 
102
                                rm -rf $TMP_DIR
 
103
                                exit 1
 
104
                        fi
 
105
                        ;;
 
106
 
 
107
                "-y" )
 
108
                        shift
 
109
                        HEIGHT="$1"
 
110
                        if test $(expr $HEIGHT % 16) -ne 0; then
 
111
                                echo "-y must be a multiple of 16"
 
112
                                rm -rf $TMP_DIR
 
113
                                exit 1
 
114
                        fi
 
115
                        ;;
 
116
 
 
117
                "-sstep" )
 
118
                        shift
 
119
                        STEP="$1"
 
120
                        ;;
 
121
 
 
122
                "-fps" )
 
123
                        shift
 
124
                        FRAMES_PER_STEP="$1"
 
125
                        ;;
 
126
 
 
127
                "-" )
 
128
                        ;;
 
129
 
 
130
                * )
 
131
                        echo "Unrecognised command-line option $1"
 
132
                        rm -rf $TMP_DIR
 
133
                        exit 1
 
134
        esac
 
135
        shift
 
136
done
 
137
 
 
138
# Make sure infile and outfile were provided
 
139
if test -z "$INFILE" || test -z "$OUTFILE"; then
 
140
    usage_error "Please provide input and output filenames using -in and -out options."
 
141
fi
 
142
 
 
143
if test -e "$INFILE"; then :;
 
144
else
 
145
        echo "Cannot open the input file $INFILE for reading."
 
146
        rm -rf $TMP_DIR
 
147
        exit 1
 
148
fi
 
149
 
 
150
printf "%s\n" "$SEPARATOR"
 
151
echo "Generating JPEGs from mplayer with the following command:"
 
152
GEN_JPEG_CMD="mplayer -nosound -noautosub -vo jpeg:outdir=$TMP_DIR -sstep $STEP -vf scale=$WIDTH:$HEIGHT $INFILE"
 
153
echo "$GEN_JPEG_CMD"
 
154
#eval "$GEN_JPEG_CMD >> $LOG_FILE 2>&1" 
 
155
eval "$GEN_JPEG_CMD" 
 
156
 
 
157
printf "%s\n" "$SEPARATOR"
 
158
echo "Creating symbolic links to get the multiple frames..."
 
159
FRAMECOUNT=$(ls -l $TMP_DIR/*.jpg|wc -l)
 
160
FN=0
 
161
for i in $(seq 1 $FRAMECOUNT); do
 
162
        FI=$(printf "%08d" $i)
 
163
        for j in $(seq 1 $FRAMES_PER_STEP); do
 
164
                FN=$(expr $FN + 1)
 
165
                FND=$(printf "%08d" $FN)
 
166
                $(ln -s ${TMP_DIR}/$FI.jpg ${TMP_DIR}/out.$FND.jpg)
 
167
        done
 
168
done
 
169
 
 
170
 
 
171
echo "Step 3: Turn the symbolic links into an m2v"
 
172
TO_M2V_CMD="jpeg2yuv -v 0 -b 1 -f 24 -j $TMP_DIR/out.%08d.jpg -I p | mpeg2enc -a 1 -n p -f 8 -o $TMP_DIR/$OUTFILE.m2v"
 
173
echo "$TO_M2V_CMD"
 
174
eval "$TO_M2V_CMD"
 
175
 
 
176
echo "Step 4: Turn the m2v into an mpg"
 
177
mplex -o $OUTFILE.mpg $TMP_DIR/$OUTFILE.m2v
 
178
 
 
179
echo "Step 5: Clean up the temporary files"
 
180
rm -rf $TMP_DIR
 
181
 
 
182
echo "$INFILE has been shortened by stepping $STEP and resized to $WIDTH x $HEIGHT."
 
183
echo "Each resulting frame is now $FRAMES_PER_STEP frames of the final file"
 
184
echo "The final file has been saved to $OUTFILE.mpg"
 
185