Skip to content

Instantly share code, notes, and snippets.

@oneman
Created May 10, 2010 21:09
Show Gist options
  • Select an option

  • Save oneman/396546 to your computer and use it in GitHub Desktop.

Select an option

Save oneman/396546 to your computer and use it in GitHub Desktop.
#!/bin/sh
# CONVERT VIDEO FOR DROID
# LOOKS LIKE FRIGGIN PERFECT
# ANY VIDEO MPLAYER CAN PLAY
# 848x480 1700k h264 / 192k aac
# Sanity check
if [ -z "${2}" ]; then
echo "2-pass x264 video converter for Motorola Droid"
echo "Usage: droidenc.sh <input_filename> <output_filename>"
echo " * Input file can be anything that mplayer can play"
echo " * Output filename should be .mp4..."
exit
elif [ -e "${2}" ]; then
echo "Error: output file \"${2}\" exists. Delete or rename it to proceed."
exit
fi
MPLAYER=/usr/bin/mplayer
FFMPEG=/usr/bin/ffmpeg
#MPLAYER="`which mplayer`"
#FFMPEG="`which ffmpeg`"
FIFO="encode.fifo"
# The Droid's video player doesn't like 854x?? videos; I think it likes dimensions in 16px multiples
SCREEN_WIDTH="848"
SCREEN_HEIGHT="480"
# These options work ok, but don't respect anamorphic ratios, and ignores aspect ratios when downscaling
#MPLAYER_OPTS="-vf scale=${SCREEN_WIDTH}:${SCREEN_HEIGHT}:noup=1,harddup -vo yuv4mpeg:file=${FIFO} -nosound -quiet -benchmark -noframedrop"
# dsize restricts the maximum scale to 848x480 while preserving aspect ratio,
# scale applies anamorphic scaling (if necessary)
FFMPEG_COMMON="-vpre ipod640 -b 1700k -bt 1000k -threads 8"
FFMPEG_AOPTS="-acodec libfaac -ab 192k -ac 2"
# Figure out our source video's display resolution
MPLAYER_LOG="`tempfile -d . -p mplayer -s .log`"
"${MPLAYER}" -nosound -vo null -endpos 0 -identify "${1}" > "${MPLAYER_LOG}" 2>&1
VIDEO_ASPECT=`grep -m1 ID_VIDEO_ASPECT ${MPLAYER_LOG} | cut -d '=' -f 2`
VIDEO_HEIGHT=`grep -m1 ID_VIDEO_HEIGHT ${MPLAYER_LOG} | cut -d '=' -f 2`
VIDEO_WIDTH=`grep -m1 ID_VIDEO_WIDTH ${MPLAYER_LOG} | cut -d '=' -f 2`
VIDEO_DWIDTH=`echo "${VIDEO_HEIGHT}*${VIDEO_ASPECT}" | bc -q`
# sometimes mplayer returns 0 for the aspect, so we should check both scaled and unscaled widths
TOO_WIDE=`echo "${VIDEO_DWIDTH}>${SCREEN_WIDTH} || ${VIDEO_WIDTH}>${SCREEN_WIDTH}" | bc -q`
TOO_TALL=`echo "${VIDEO_HEIGHT}>${SCREEN_HEIGHT}" | bc -q`
echo "VIDEO HEIGHT: ${VIDEO_HEIGHT}"
echo "VIDEO WIDTH: ${VIDEO_WIDTH}"
echo "VIDEO ASPECT: ${VIDEO_ASPECT}"
echo "VIDEO DWIDTH: ${VIDEO_DWIDTH}"
echo "TOO WIDE: ${TOO_WIDE}"
echo "TOO TALL: ${TOO_TALL}"
if [ ${TOO_TALL} -gt "0" ] || [ ${TOO_TALL} -gt "0" ]; then
echo "Downscaling to fit inside ${SCREEN_WIDTH}x${SCREEN_HEIGHT} window"
MPLAYER_OPTS="-sws 9 -vf dsize=${SCREEN_WIDTH}:${SCREEN_HEIGHT}:0:16,scale=0:0,harddup -vo yuv4mpeg:file=${FIFO} -nosound -quiet -benchmark -noframedrop"
else
echo "Not downscaling: source fits inside fit inside ${SCREEN_WIDTH}x${SCREEN_HEIGHT} window"
# the only scaling here is expansion for anamorphic video
MPLAYER_OPTS="-sws 9 -vf scale=-8:-8,harddup -vo yuv4mpeg:file=${FIFO} -nosound -quiet -benchmark -noframedrop"
fi
#exit
# Create fifo, removing an old one if necessary
if [ -p "${FIFO}" ]; then
rm "${FIFO}"
fi
mkfifo "${FIFO}"
# 1st pass
"${MPLAYER}" ${MPLAYER_OPTS} "${1}" &
"${FFMPEG}" -i ${FIFO} -pass 1 -an -vcodec libx264 -vpre fastfirstpass ${FFMPEG_COMMON} -f rawvideo -y /dev/null
# Encode audio stream
AUDIO_FILE="`tempfile -d . -p audio -s .mp4`"
"${FFMPEG}" -vn -i "${1}" ${FFMPEG_AOPTS} -y "${AUDIO_FILE}"
# 2nd pass - mux the audio stream as well
"${MPLAYER}" ${MPLAYER_OPTS} "${1}" &
if [ -s "${AUDIO_FILE}" ]; then
"${FFMPEG}" -i ${FIFO} -i "${AUDIO_FILE}" -pass 2 -acodec copy -vcodec libx264 -vpre hq ${FFMPEG_COMMON} "${2}"
else
"${FFMPEG}" -i ${FIFO} -pass 2 -vcodec libx264 -vpre hq ${FFMPEG_COMMON} "${2}"
fi
# Clean up
rm "${FIFO}"
if [ -s "${2}" ]; then
echo "Encode complete: \"${2}\""
if [ -f "${AUDIO_FILE}" ]; then rm "${AUDIO_FILE}"; fi
if [ -f "${MPLAYER_LOG}" ]; then rm "${MPLAYER_LOG}"; fi
if [ -f ffmpeg2pass-0.log ]; then rm ffmpeg2pass-0.log; fi
if [ -f x264_2pass.log ]; then rm x264_2pass.log; fi
if [ -f x264_2pass.log.mbtree ]; then rm x264_2pass.log.mbtree; fi
else
echo "UH-OH something went wrong"
rm "${2}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment