Skip to content

Instantly share code, notes, and snippets.

@faleev
Created August 22, 2012 21:04
Show Gist options
  • Save faleev/3429342 to your computer and use it in GitHub Desktop.
Save faleev/3429342 to your computer and use it in GitHub Desktop.
Convert video for HTC Desire
#!/bin/sh
# Convert widescreen-clips to mp4 for android
# http://www.linux.org.ru/forum/mobile/5042707
# How to compile FFMPEG:
# https://ffmpeg.org/trac/ffmpeg/wiki/UbuntuCompilationGuide
INPUT="$1"
if [ ! -f "$INPUT" ] ; then
echo File $INPUT not found
exit 1
fi
WIDTH=800
HEIGHT=480
# For multiple sound tracks - specify the one to use
#AUDIO_CHANNEL="${2:-0.1}"
AUDIO_CHANNEL="0:1"
BITRATE="700k"
ffmpeg -i "$INPUT" 2>&1 | \
awk -v fsw=${WIDTH} -v fsh=${HEIGHT} '/Video/{
n=match ($0, "([0-9]+)x([0-9]+)", a);
w=a[1]; h=a[2];
sw=fsw; sh=1.0*sw*h/w;
# if (sh == fsh) {
# printf "# Exact scale\n";
# } else if (sh < fsh) {
# sh = int(sh / 4) * 4;
# delta = fsh - sh;
# p1 = int(delta / 8) * 4;
# p2 = delta - p1;
# printf "# Add horizontal bars: %d\n", fsh - sh;
# printf "PADDING_OPTIONS=\"-padtop %s -padbottom %s\"\n", p1, p2;
# } else {
# sh=fsh; sw=1.0*sh*w/h;
# sw = int(sw / 4) * 4;
# delta = fsw - sw;
# p1 = int(delta / 8) * 4;
# p2 = delta - p1;
# printf "# Add vertical bars %d\n", fsw - sw;
# printf "PADDING_OPTIONS=\"-padleft %s -padright %s\"\n", p1, p2;
# }
printf "# %sx%s -> %s %s (%s %s)\n", w, h, fsw, fsh, sw, sh;
printf "WIDTH_HEIGHT=%sx%s\n", sw, sh;
}' > .params
cat .params
. ./.params
# Padding options depricated.
vp="-s $WIDTH_HEIGHT -vcodec libx264 -bt $BITRATE -maxrate 768k -bufsize 2M -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 13 -coder 0 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -flags +loop -cmp +chroma -b $BITRATE"
ap="-acodec libfaac -ac 2 -ar 48000 -ab 96k -map 0:0 -map $AUDIO_CHANNEL"
cp="-threads 0 $vp $ap"
rm -f "$INPUT".mp4
time ffmpeg -i "$INPUT" $cp -pass 1 \
-partitions 0 -me_method epzs -subq 1 -trellis 0 -refs 1 \
-f rawvideo -y /dev/null && \
time ffmpeg -i "$INPUT" $cp -pass 2 \
-partitions +parti4x4+partp8x8+partb8x8 \
-me_method umh -subq 5 \
-trellis 1 -refs 5 \
"$INPUT".mp4
# Clean up logfiles from multi-pass runs
rm ffmpeg2pass-0.log
rm ffmpeg2pass-0.log.mbtree
rm .params
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment