Last active
December 20, 2015 15:49
-
-
Save brimston3/6157029 to your computer and use it in GitHub Desktop.
No guarantee that this still works; last modified date says 2009. I used it to resize video to less than 400x704 for display on my nokia n810. You can probably play with the parameters too.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#Requires: | |
# mplayer, mencoder. mkvmerge | |
# libx264, liblame | |
HaveOpt() { | |
local needle=$1 | |
shift | |
while [[ $1 == -* ]]; do | |
case "$1" in | |
--) return 1;; # by convention, -- is end of options | |
$needle) return 0;; | |
esac | |
shift | |
done | |
return 1 | |
} | |
# This function isn't used. Honestly, I don't remember why it didn't work and why | |
# I didn't use `mktemp "/tmp/$(basename $0).XXXXXX.out"`, which is roughly the same thing | |
RandFile() { | |
FILE="/tmp/$(basename $0).$RANDOM.out" | |
while [ -e $FILE ] | |
do | |
FILE="/tmp/$(basename $0).$RANDOM.out" | |
done | |
} | |
#RandFile | |
FILE=`mktemp` | |
echo Using file $FILE | |
# Bad sed removed due to \"; shinanigans: | |
# sed -e 's/"/\\"/g' -e 's/=\(.*\)$/="\1"/' | |
# New sed uglier (love those backslashes), but proper. | |
mplayer -quiet -vo null -ao null -frames 0 -identify "$1" 2>/dev/null | grep "^ID_" | sed -e "s/'/'\\\\''/g" -e 's/=\(.*\)$/='\''\1'\''/' > $FILE | |
source $FILE | |
rm $FILE | |
echo "Video Width: " $ID_VIDEO_WIDTH | |
echo "Video Height:" $ID_VIDEO_HEIGHT | |
# for n8x0, 420x200 is the optimal size. | |
# 704x400 is the optimal size for the android device I target most recently. | |
TARGET_WIDTH_PIXELS=704 | |
TARGET_HEIGHT_PIXELS=400 | |
# This variable is only so I don't recompute the greater scaling axis for display: | |
AUTOSCALE_AXIS=$(bc <<< "scale=3; $ID_VIDEO_WIDTH/$TARGET_WIDTH_PIXELS - $ID_VIDEO_HEIGHT/$TARGET_HEIGHT_PIXELS") | |
#echo "Scaling axis (negative means Y_{new}/Y_{old} > X_{new}/X_{old}): " $AUTOSCALE_AXIS | |
if [[ "$AUTOSCALE_AXIS" = -* ]]; then | |
#echo "Scaling to height" | |
SCALE_WIDTH=-2 | |
SCALE_HEIGHT=$TARGET_HEIGHT_PIXELS | |
else | |
#echo "Scaling to width" | |
SCALE_WIDTH=$TARGET_WIDTH_PIXELS | |
SCALE_HEIGHT=-2 | |
fi | |
#RandFile | |
#VOUT=$FILE | |
VOUT=`mktemp` | |
echo VOUT=$VOUT | |
rm -f divx2pass.log | |
#mencoder "$1" -o $VOUT -nosound -ovc lavc -lavcopts vcodec=mpeg4:autoaspect:vbitrate=500:vpass=1 -vf scale=$SCALE_WIDTH:$SCALE_HEIGHT -ffourcc DIVX -idx | |
#mencoder "$1" -o $VOUT -oac copy -ovc lavc -lavcopts vcodec=mpeg4:autoaspect:vbitrate=500:vpass=2 -vf scale=$SCALE_WIDTH:$SCALE_HEIGHT -ffourcc DIVX -idx | |
X264_BOTHPASS_OPTS="crf=21:bframes=8:b_pyramid:8x8dct:threads=auto" | |
X264_FIRSTPASS="turbo=0:pass=1" | |
X264_SECONDPASS="pass=2" | |
AUDIO_FIRSTPASS=( -nosound ) | |
AUDIO_SECONDPASS=( -oac mp3lame -lameopts br=128 ) | |
VIDEOSCALE_OPTS=( -vf scale=$SCALE_WIDTH:$SCALE_HEIGHT ) | |
mencoder -ni "$1" -o $VOUT "${AUDIO_FIRSTPASS[@]}" -ovc x264 -x264encopts $X264_FIRSTPASS:$X264_BOTHPASS_OPTS "${VIDEOSCALE_OPTS[@]}" | |
mencoder -ni "$1" -o $VOUT "${AUDIO_SECONDPASS[@]}" -ovc x264 -x264encopts $X264_SECONDPASS:$X264_BOTHPASS_OPTS "${VIDEOSCALE_OPTS[@]}" | |
#mencoder "$1" -o $VOUT -oac mp3lame -lameopts br=128 -ovc x264 -x264encopts qp=19:bframes=8:b_pyramid:8x8dct -vf scale=$SCALE_WIDTH:$SCALE_HEIGHT | |
XCODED_VID_PREFIX="nit-" | |
if [[ "$1" = *.mkv ]]; then | |
mkvmerge -o "$XCODED_VID_PREFIX""`basename "$1" .mkv`".mkv $VOUT -A -D -B "$1" | |
rm $VOUT | |
elif [[ "$1" = *.ogm ]]; then | |
mkvmerge -o "$XCODED_VID_PREFIX""`basename "$1" .ogm`".mkv $VOUT -A -D -B "$1" | |
rm $VOUT | |
else | |
mv $VOUT "$XCODED_VID_PREFIX""`basename "$1" .avi`".avi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated a little. Surprisingly, this still works, at least in one of my VMs. Metadata is still discarded, but the point is to save flash space. I did eventually remember why RandFile shouldn't be used; VOUT isn't properly quoted anywhere.