Skip to content

Instantly share code, notes, and snippets.

@zmillman
Created February 26, 2013 23:51

Revisions

  1. zmillman created this gist Feb 26, 2013.
    95 changes: 95 additions & 0 deletions deploy-ffmpeg.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    # FFmpeg is a tool for converting, streaming, and playing multimedia content.
    #
    # The installation recipe pulls heavily from this guide: https://ffmpeg.org/trac/ffmpeg/wiki/UbuntuCompilationGuide
    #
    # If you have trouble with the server hanging on "Building Debian package...", try increasing the
    # instance size. I've had bad luck with m1.small and good luck with m1.large
    #
    # qtfaststart is a script for repositioning the Quicktime atom for mp4 streaming.
    # see https://github.com/danielgtaylor/qtfaststart for more info

    namespace :rubber do

    namespace :ffmpeg do

    rubber.allow_optional_tasks(self)

    # Attach installation hooks to rubber's setup process
    after 'rubber:enable_multiverse', 'rubber:ffmpeg:enable_medibuntu'
    before 'rubber:install_packages', 'rubber:ffmpeg:install_medibuntu'
    after 'rubber:install_packages', 'rubber:ffmpeg:install'
    after 'rubber:install_packages', 'rubber:ffmpeg:install_qtfaststart'
    after 'rubber:install_packages', 'rubber:ffmpeg:install_normalize_audio'

    ffmpeg_dependencies = %w(libfaac-dev libgpac-dev libjack-jackd2-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev texi2html yasm zlib1g-dev)

    task :enable_medibuntu, :roles => :ffmpeg do
    sudo 'wget http://www.medibuntu.org/sources.list.d/$(lsb_release -cs).list --output-document=/etc/apt/sources.list.d/medibuntu.list'
    end

    task :install_medibuntu, :roles => :ffmpeg do
    sudo 'apt-get --yes -q --allow-unauthenticated install medibuntu-keyring'
    end

    # Installs ffmpeg with mp4 codecs
    task :install, :roles => :ffmpeg do
    # Checks if the ffmpeg command already exists
    ffmpeg_installed = capture('command -v ffmpeg >/dev/null 2>&1 || { echo >&2 "Not installed"; }')
    if ffmpeg_installed.empty?
    Rubber.logger.info 'ffmpeg is already installed'
    # installed, nothing else to do
    else

    # Install codec depencies
    sudo "apt-get install build-essential checkinstall --force-yes --yes --quiet #{ffmpeg_dependencies.join(' ')}"

    # Remove any old temporary folders
    sudo 'rm ~/ffmpeg ~/x264 -rf'

    # Install the x264 library
    run 'cd ~; git clone git://git.videolan.org/x264'
    run 'cd ~/x264; ./configure --enable-static --enable-shared'
    run 'cd ~/x264; make'
    run 'cd ~/x264; sudo checkinstall --pkgname=x264 --pkgversion="3:$(./version.sh | awk -F\'[" ]\' \'/POINT/{print $4"+git"$5}\')" --backup=no --deldoc=yes --fstrans=no --default'

    # Install and compile ffmpeg from source
    run 'cd ~; git clone --depth 1 git://source.ffmpeg.org/ffmpeg'
    run 'cd ~/ffmpeg; ./configure --enable-gpl --enable-libfaac --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libx264 --enable-nonfree --enable-version3'
    run 'cd ~/ffmpeg; make' # this takes so long that ubuntu occasionally kills it -- run it manually if that happens
    run 'cd ~/ffmpeg; sudo checkinstall --pkgname=ffmpeg --pkgversion="5:$(date +%Y%m%d%H%M)-git" --backup=no --deldoc=yes --fstrans=no --default'

    # Clean up temporary folders
    sudo 'rm ~/ffmpeg ~/x264 -rf'

    # Fix library loading
    sudo 'ldconfig `locate libx264.so.125`'
    end
    end

    # Installs the normalize-audio package
    task :install_normalize_audio, :roles => :ffmpeg do
    sudo 'apt-get install --yes -q normalize-audio'
    end

    # Installs qtfaststart.py helper script for making mp4s streamable
    task :install_qtfaststart, :roles => :ffmpeg do
    # Checks if the qtfaststart command already exists
    qtfaststart_installed = capture('command -v qtfaststart >/dev/null 2>&1 || { echo >&2 "Not installed"; }')
    if qtfaststart_installed.empty?
    Rubber.logger.info 'qtfaststart is already installed'
    # installed, nothing else to do
    else
    sudo 'rm ~/qtfaststart -rf'
    run 'cd ~; git clone git://github.com/danielgtaylor/qtfaststart.git'
    run 'cd ~/qtfaststart; sudo python setup.py install'
    sudo 'rm ~/qtfaststart -rf'
    end
    end

    task :remove, :roles => :ffmpeg do
    # libavcodec-extra-52 etc. might be left over from a previous installation
    old_libs = %w(libavcodec-extra-52 libx264-dev libvpx-dev)
    sudo "apt-get autoremove --force-yes --yes -q ffmpeg x264 #{ffmpeg_dependencies.join(' ')} #{old_libs.join(' ')}"
    end
    end
    end