66f6ca66fe30012e81f36de6ae9c1909d2010c6a
[video-handling.git] / transcode-all
1 #!/bin/bash
2 #
3 # Calin-Andrei Burloiu, 2010, calin.burloiu@gmail.com
4 #
5 # This script transcodes all input videos with a specified extension to a new
6 # format with a given resolutin and video bitrate. The audio bitrate is hard
7 # coded to 192 kb/s. The output_path parameter is optional. If not given it
8 # puts all output files in the same folder as the corresponding input.
9 #
10
11 if test $# -lt 4; then
12     echo "Usage: $0 input-path input-extension output-format resolution video-bitrate [output-suffix] [output_path]"
13     exit 1
14 fi
15
16 in_path="$1"
17 in_ext="$2"
18 out_format="$3"
19 resolution="$4"
20 bitrate="$5"
21 if [ ! -z "$6" ]; then
22     out_suffix="$6"
23 else
24     out_suffix=""
25 fi
26 if [ ! -z "$7" ]; then
27     out_path="$7"
28 fi
29
30 FFMPEG=/usr/bin/ffmpeg
31 ABITRATE=192k
32
33 (
34 IFS=$'\n'
35 for filename in $(find "$in_path" -name "*.$in_ext"); do
36     if [ ! -z "$out_path" ]; then
37         new_filename="${out_path}"$(basename "$filename" ."$in_ext")"$out_suffix"."$out_format"
38     else
39         new_filename=$(dirname "$filename")/$(basename "$filename" ."$in_ext")"$out_suffix"."$out_format"
40     fi
41
42     case "$out_format" in
43         "avi" )
44             $FFMPEG -i "$filename" -f avi -acodec libmp3lame -ab $ABITRATE -ar 44100 -ac 2 -vcodec libx264 -vpre normal -b "$bitrate" -r 25 -s $resolution -threads 0 "$new_filename"
45             ;;
46         "flv" )
47             $FFMPEG -i "$filename" -f flv -acodec libmp3lame -ab $ABITRATE -ar 44100 -ac 2 -vcodec libx264 -vpre normal -b "$bitrate" -r 25 -s $resolution -threads 0 "$new_filename"
48             ;;
49         "mp4" )
50             $FFMPEG -i "$filename" -f mp4 -acodec libmp3lame -ab $ABITRATE -ar 44100 -ac 2 -vcodec libx264 -vpre normal -b "$bitrate" -r 25 -s "$resolution" -threads 0 "$new_filename"
51             ;;
52         "ogg" )
53             $FFMPEG -i "$filename" -f ogg -acodec libvorbis -ab $ABITRATE -ar 44100 -ac 2 -vcodec libtheora -b "$bitrate" -r 25 -s $resolution -threads 0 "$new_filename"
54             ;;
55         "webm" )
56             $FFMPEG -i "$filename" -f webm -acodec libvorbis -ab $ABITRATE -ar 44100 -ac 2 -vcodec libvpx -b "$bitrate" -r 25 -s $resolution -threads 0 "$new_filename"
57             ;;
58         "ts" )
59             $FFMPEG -i "$filename" -f mpegts -acodec libmp3lame -ab $ABITRATE -ar 44100 -ac 2 -vcodec libx264 -vpre normal -b "$bitrate" -r 25 -s $resolution -threads 0 "$new_filename"
60             ;;
61
62         * )
63             echo "Format $out_format is not implemented!" 1>&2
64             exit 1
65             ;;
66     esac
67
68 done
69 )