Skip to content

Instantly share code, notes, and snippets.

@mtigas
Created October 13, 2010 19:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mtigas/624732 to your computer and use it in GitHub Desktop.
Save mtigas/624732 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Using `ffmpeg` to convert an arbitrary video file to an ipad-compatible M4V (h264+AAC)
# file. Mac-specific since libfaac has occasional audio glitches and the `afconvert` command
# is of a much higher quality (since it uses the system's Quicktime libs).
# Assumes ffmpeg and x264 (and other libs ffmpeg uses to decode various video formats
# you want) are installed.
#
# Easy mode:
# * Install homebrew: http://github.com/mxcl/homebrew
# * brew install libmp3lame libogg libvorbis x264 faad2 ffmpeg
# 0-127, higher is better/larger
AUDIO_VBR_QUALITY=85
AUDIO_VBR_QUALITY_FLOAT=`python -c "print $AUDIO_VBR_QUALITY/127.0"`
# 0-50ish, lower is better/larger. 22-25 is already high HD quality. Lower values are insane.
VIDEO_CRF_QUALITY=26
# Controls encoder quality options.
# Good values to try are "max", "hq", "medium", "fast"
# The "main" preset is tacked onto this (and the refs=4 option is set as well)
# to ensure playback compatibility with iPad.
X264_VIDEO_PRESET="hq"
# If you want to test a short portion of the video, you can use "-t 30" to only convert
# the first 30 seconds of your source material.
EXTRA_FFMPEG_FLAGS=""
# ==============================
BASENAME=`python -c "import os;print os.path.basename('$1').rsplit('.',1)[0]"`
# pull out audio
echo "Extracting and converting audio..."
echo
ffmpeg -i "$1" -f wav $EXTRA_FFMPEG_FLAGS -y "/tmp/convert-$BASENAME.wav"
afconvert -q 127 -s 3 -f m4af -d aac -u vbrq $AUDIO_VBR_QUALITY "/tmp/convert-$BASENAME.wav" "/tmp/convert-$BASENAME.m4a"
echo
echo
echo "Converting video..."
echo
ffmpeg -i "$1" -i "/tmp/convert-$BASENAME.m4a" -map 0:0 -map 1:0 $EXTRA_FFMPEG_FLAGS -f mp4 -threads 0 -vcodec libx264 -vpre $X264_VIDEO_PRESET -vpre main -level 31 -refs 4 -crf $VIDEO_CRF_QUALITY -qmin 1 -qmax 32 -qdiff 6 -acodec copy -y "$BASENAME.m4v"
rm "/tmp/convert-$BASENAME.wav"
rm "/tmp/convert-$BASENAME.m4a"
# ==============================
# Footnotes:
#
# CRF (constant rate factor) is recommended over CQP (constant quantization parameter) for "constant quality"
# usecases.
# http://rob.opendot.cl/index.php/useful-stuff/ffmpeg-x264-encoding-guide/
# http://sites.google.com/site/linuxencoding/x264-ffmpeg-mapping
#
# For even more obnoxious video quality, you can install this x264 encoder preset:
# http://gist.github.com/552224
#
# `afconvert` used over ffmpeg+faac for audio as per my own experiements and supporting arguments at:
# http://wiki.hydrogenaudio.org/index.php?title=AAC_implementations
#
# If you want to go all-out with audio quality, install Wine and download a copy of NeroAacEnc <http://j.mp/cZqWVu>
# and swap out the afconvert line with
# wine /path/to/neroAacEnc.exe -q AUDIO_VBR_QUALITY -if "/tmp/convert-$BASENAME.wav" -of "/tmp/convert-$BASENAME.m4a"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment