This file looks large and may slow your browser down if we attempt
to syntax highlight it, so we are showing it without any
pretty colors.
Highlight
it anyway.
| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
if [ -z "$1" ]; then |
| 4 |
echo "Usage: $0 <input video file> [output avi file]" >&2 |
| 5 |
exit 1 |
| 6 |
fi |
| 7 |
|
| 8 |
in="$1" |
| 9 |
if [ ! -z "$2" ]; then |
| 10 |
if [ -d "$2" ]; then |
| 11 |
out="$2/${in%.*}.mp4" |
| 12 |
else |
| 13 |
out="$2" |
| 14 |
fi |
| 15 |
else |
| 16 |
out="${in%.*}.mp4" |
| 17 |
fi |
| 18 |
|
| 19 |
[ -z "$video2mobile_rm" ] && export video2mobile_rm="rm -i" |
| 20 |
|
| 21 |
OUT_WIDTH=176 |
| 22 |
OUT_HEIGHT=144 |
| 23 |
|
| 24 |
OUT_ASPECT="$(echo "scale=3; $OUT_WIDTH/$OUT_HEIGHT" | bc)" |
| 25 |
|
| 26 |
info="$(ffmpeg -i "$in" 2>&1)" |
| 27 |
width=$(echo "$info" | grep -m1 Video: | perl -pe 's/^.* (\d+)x(\d+)(,.*)?$/$1/') |
| 28 |
height=$(echo "$info" | grep -m1 Video: | perl -pe 's/^.* (\d+)x(\d+)(,.*)?$/$2/') |
| 29 |
aspect="$(echo "scale=3; $width/$height" | bc)" |
| 30 |
|
| 31 |
new_width=$OUT_WIDTH |
| 32 |
new_height=$OUT_HEIGHT |
| 33 |
padding_ap="" |
| 34 |
|
| 35 |
if expr "$aspect" '<' "$OUT_ASPECT" >/dev/null; then |
| 36 |
# Black padding left and right |
| 37 |
padding="$(echo "($OUT_WIDTH - $OUT_HEIGHT * $aspect)/2" | bc)" |
| 38 |
padding_ap="-padleft $padding -padright $padding" |
| 39 |
new_width=$[$new_width-$padding*2] |
| 40 |
elif expr "$aspect" '>' "$OUT_ASPECT" >/dev/null; then |
| 41 |
# Black padding top and bottom |
| 42 |
padding="$(echo "($OUT_HEIGHT - $OUT_WIDTH / $aspect)/2" | bc)" |
| 43 |
padding_ap="-padtop $padding -padbottom $padding" |
| 44 |
new_height=$[$new_height-$padding*2] |
| 45 |
fi |
| 46 |
|
| 47 |
ffmpeg -i "$in" -s "${new_width}x${new_height}" -r 15 -ab 64k -acodec aac -b 95k -vcodec mpeg4 -strict experimental -aspect "$OUT_ASPECT" $padding_ap "$out" #&& $video2mobile_rm "$in" |