#!/bin/sh # Copyright © 2010-2014 Jonas Smedegaard # Description: Recode a video into web-optimized format(s) # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # Depends: libav-tools melt mediainfo # # TODO: # * Offer to skip rendering again if an output file exist already. # * Support --width and --height, resolving the other part from input # or forced aspect ratio. # * Drop $melt_recent flag when melt 0.9.2 is stable. # * Check and fail if all needed tools are not available. # * Test if beneficial to apply real_time=-2. # * Normalize each infile separately when xml fed as infile keeps sync. # Maybe as workaround re-feed audio separately from xml, as done at # . # * Resolve flash player to use. set -e PRG=$(basename "$0") showhelp() { cat < EOF } exit1() { response="${1:+Error: }${1:-Internal error!}" echo >&2 "$response" exit 1 } # defaults formats=webm,vp9,mp4 samplestart=0 samplelength=150 compression=normal # VP8 is rumored to compress ~20% worse than H.264 factor_vp8=120/100 # VP9 compresses natural video ~27% worse than H.264 (i.e. excluding animation) # factor_vp9=127/100 # parse cmdline options TEMP="`getopt -s sh -o hp:s:a:r:b:t: -l help,profile:,size:,aspect:,rate:,video:,refbpp:,formats:,audio:,audioprefilter:,loudness,loudness-data:,filter:,stem:,title:,sample,sample-start:,sample-length:,compression: -n "$PRG" -- "$@"`" || exit1 "Internal getopt error." eval set -- "$TEMP" while true ; do case "$1" in -h|--help) showhelp; exit;; -p|--profile) profile="$2"; shift 2;; -s|--size) size="$2"; shift 2;; -a|--aspect) aspect="$2"; shift 2;; -r|--rate) framerate="$2"; shift 2;; --video) video="$2"; shift 2;; --refbpp) refbpp="$2"; shift 2;; --formats) formats="$2"; shift 2;; --audio) audio="$2"; shift 2;; --audioprefilter) audioprefilters="${audioprefilters:+$audioprefilters }-filter $2"; shift 2;; --loudness) loudness=yes; shift;; --loudness-data) loudness=yes; loudness_data="$2"; shift 2;; --filter) filters="${filters:+$filters }-filter $2"; shift 2;; --stem) stem="$2"; shift 2;; -t|--title) title="$2"; shift 2;; --sample) sample=yes; shift;; --sample-start) sample=yes; samplestart="$2"; shift 2;; --sample-length) sample=yes; samplelength="$2"; shift 2;; --compression) compression="$2"; shift 2;; --) shift; break;; *) exit1 "Internal error resolving options.";; esac done # Resolve if melt is version 0.9.2 or newer melt_recent=$(melt -query filter=loudness | grep -qi R128 && echo yes) # sanitize infiles infiles=$* infile_first=$(perl -e 'print shift @ARGV' $infiles) [ -e "$infile_first" ] || exit1 "Cannot read first input file \"$infile_first\"!" # resolve stem and title (if not explicitly set) stem=${stem:-$(basename "$infile_first" | perl -pe 's/\.[^.]*//')} title=${title:-$stem} # resolve quality/speed hints multipass=yes speedpreset_h264=medium crf_vp8=10 crf_vp9=10 crf_h264=23 qscale_theora=5 cpu_vp8=3 cpu_vp9=5 case "$compression" in normal) :;; dirty) multipass= speedpreset_h264=veryfast crf_vp8=0 crf_vp9=0 qscale_theora=3 cpu_vp8=5 ;; hq) speedpreset_h264=veryslow crf_vp8=10 crf_vp9=10 qscale_theora=6 cpu_vp8=0 cpu_vp9=1 ;; *) exit1 "Unknown compression optimization \"$video\".";; esac # parse/resolve size and framerate case "$profile" in '') :;; *@*) while read s r foo; do profilesize="${size:-$s}" framerate="${framerate:-$r}" done << EOF $(echo "$profile" | perl -F@ -anE 'say join " ", @F') EOF ;; *p*) while read s r foo; do profilesize="${size:-${s}p}" framerate="${framerate:-$r}" done << EOF $(echo "$profile" | perl -Fp -anE 'say join " ", @F') EOF ;; *) profilesize="$profile" ;; esac size=${size:-$profilesize} case "$size" in qvga) size=320x240;; hvga) size=480x360;; vga) size=640x480;; svga) size=800x600;; xga) size=1024x768;; 240p|wqvga) size=424x240;; 360p|nhd) size=640x360;; 480p|wvga) size=848x480;; 576p|wsvga) size=1024x576;; 720p|wxga|hd) size=1280x720;; esac if [ -n "$size" ]; then while read w h foo; do width="${width:-$w}" height="${height:-$h}" done << EOF $(echo "$size" | perl -Fx -anE 'say join " ", @F') EOF if [ -z "$width" ] || [ -z "$height" ]; then exit1 "Failed to parse size \"$size\"." fi fi case "$framerate" in */*) while read n d foo; do framerate_num="${framerate_num:-$n}" framerate_den="${framerate_den:-$d}" done << EOF $(echo "$framerate" | perl -F/ -anE 'say join " ", @F') EOF ;; ?*) framerate_num="$framerate" framerate_den=1 ;; esac # resolve input size and framerate (needed for computing bitrate) while read w h r foo; do width_in="${width_in:-$w}" height_in="${height_in:-$h}" framerate_in="${framerate_in:-$r}" done << EOF $(mediainfo --Inform="Video;%Width% %Height% %FrameRate%" "$infile_first") EOF case "$video" in talkinghead) refbpp="${refbpp:-0.1}" x264tune=film ;; action) refbpp="${refbpp:-0.15}" x264tune=film ;; '') refbpp="${refbpp:-0.12}" ;; *) exit1 "Unknown video style \"$video\".";; esac for format in $(echo "$formats" | sed -e 's/,/ /g'); do case $format in theora|ogg) ogg=yes;; h264|mp4) mp4=yes;; vp8|webm) webm=yes;; vp9) vp9=yes;; *) exit1 "Unknown format \"$format\".";; esac done _width="${width:-$width_in}" _height="${height:-$height_in}" if [ -n "$_width" ] && [ -n "$_height" ]; then _pixels="$(($_width*$_height))" fi _frames="${framerate:-$framerate_in}" webmpreset=360p if [ -n "$_pixels" ] && [ $_pixels -ge $((1024*768)) ]; then webmpreset=720p if [ -n "$_frames" ] && [ $_frames -gt 40 ]; then webmpreset=720p50_60 fi fi # compute average bitrate from reference data and "power of .75" rule bitrate_vp8="$bitrate" bitrate_vp9="$bitrate" if [ -n "$_pixels" ] && [ -n "$_frames" ]; then bitrate=$(perl -E '$refsize=640*360;' \ -E "say int( +(($_pixels/\$refsize)**0.75*\$refsize*$_frames*$refbpp) )") bitrate_vp8=$(perl -E "say int( +($bitrate*$factor_vp8) )") #" bitrate_vp9=$(perl -E "say int( +($bitrate*$factor_vp9) )") #" fi # default per-codec-channel bitrates bitrate_vorbis=64 bitrate_opus=64 bitrate_aac=96 channels=-1 maxchannels=2 case "$audio" in music) opusapp=audio ;; hqspeech) channels=1 bitrate_vorbis=48 bitrate_opus=32 bitrate_aac=64 opusapp=voip ;; speech) channels=1 bitrate_vorbis=48 bitrate_opus=32 bitrate_aac=64 compress=yes [ -z "$melt_recent" ] || _melt_loudness="$loudness_data" limit=yes opusapp=voip ;; silence) channels=0 ;; '') : ;; *) exit1 "Unknown audio style \"$audio\".";; esac [ $channels -ge 0 ] || channels=$(avprobe -v warning -show_streams "$infile_first" | perl -ne 's/channels=// and print $_' || echo -1) [ $channels -le $maxchannels ] || channels=$maxchannels [ $channels -gt 0 ] || channels= melt="melt -progress" _melt_sample="$infile_first ${sample:+in=${samplestart:-0} out=$((${samplestart:-0} + samplelength))}" _melt_video="progressive=1${framerate:+ frame_rate_num="$framerate_num" frame_rate_den="$framerate_den"}${size:+ s=${width:+$width}x${height:+$height}}${aspect:+ aspect=$aspect}" _melt_ogg="$_melt_video f=ogg vcodec=libtheora${bitrate:+ vb=$bitrate} qscale=$qscale_theora" _melt_webm="$_melt_video f=webm vcodec=libvpx vpreset=libvpx-$webmpreset${bitrate_vp8:+ vb=$bitrate_vp8 minrate=$((bitrate_vp8/20)) maxrate=$((bitrate_vp8*12))} crf=$crf_vp8 cpu-used=$cpu_vp8" # CRF ignored with libvpx 1.3 _melt_vp9="$_melt_video f=webm vcodec=libvpx-vp9 vpreset=libvpx-$webmpreset${bitrate_vp9:+ vb=$bitrate_vp9 minrate=$((bitrate_vp9/20)) maxrate=$((bitrate_vp9*12))} crf=$crf_vp9 cpu-used=$cpu_vp9" _melt_h264="$_melt_video f=mp4 vcodec=libx264 vpreset=$speedpreset_h264 vprofile=baseline${x264tune:+ tune=$x264tune} threads=0 movflags=+faststart crf=$crf_h264" _melt_audio="${channels:+ac=$channels}" # limit (i.e. avoid peaks "clipping") _melt_postfilters_audio="${limit:+-filter ladspa.1077}" _melt_vorbis="$_melt_audio acodec=libvorbis${channels:+ ab=$(($channels*$bitrate_vorbis))k}" _melt_opus="$_melt_audio acodec=libopus${channels:+ ab=$(($channels*$bitrate_opus))k}${opusapp:+ application=$opusapp}" _melt_aac="$_melt_audio acodec=aac${channels:+ ab=$(($channels*$bitrate_aac))k}" _melt_img="$_melt_video f=image2" avconv="avconv -threads auto -y -v warning" _avconv_vp8="-c:v libvpx -pre:v libvpx-$webmpreset${bitrate_vp8:+ -b:v $bitrate_vp8 -minrate $((bitrate_vp8/20)) -maxrate $((bitrate_vp8*12))} -crf $crf_vp8 -cpu-used $cpu_vp8" _avconv_vp9="-c:v libvpx-vp9 -pre:v libvpx-$webmpreset${bitrate_vp9:+ -b:v $bitrate_vp9 -minrate $((bitrate_vp9/20)) -maxrate $((bitrate_vp9*12))} -crf $crf_vp9 -cpu-used $cpu_vp9" # resolve EBU R128 audio normalizing if [ -n "$loudness" ] && [ -z "$_melt_loudness" ]; then echo "Analyzing loudness data..." $melt ${_melt_sample:-$infiles} $audioprefilters -filter loudness -consumer xml:$stem.xml $_melt_audio video_off=1 all=1 _melt_loudness="$(perl -ne 'm!([^<]+)! and print $1' $stem.xml)" echo "Loudness data: $_melt_loudness" fi if [ -n "$multipass" ] && [ -n "$webm$vp9" ]; then echo "Analyzing complexity for WebM..." $melt ${_melt_sample:-$infiles} $filters \ -consumer avformat:pipe:1 $_melt_video f=yuv4mpegpipe pix_fmt=yuv420p an=1 audio_off=1 \ | $avconv -i pipe:0 \ ${webm:+-f rawvideo $_avconv_vp8 -an -pass 1 -passlogfile ${stem}_vp8 /dev/null} \ ${vp9:+-f rawvideo $_avconv_vp9 -an -pass 1 -passlogfile ${stem}_vp9 /dev/null} [ -z "$webm" ] || mv -f ${stem}_vp8-*.log ${stem}_vp8_2pass.log [ -z "$vp9" ] || mv -f ${stem}_vp9-*.log ${stem}_vp9_2pass.log fi echo "Encoding video..." $melt ${_melt_sample:-$infiles} \ ${channels:+$audioprefilters${_melt_loudness:+ -filter loudness results="$_melt_loudness"}} \ $filters${channels:+ $_melt_postfilters_audio} \ ${ogg:+-consumer avformat:$stem.ogv $_melt_ogg $_melt_vorbis} \ ${webm:+-consumer avformat:$stem.webm $_melt_webm${multipass:+ pass=2 passlogfile=${stem}_vp8} $_melt_vorbis} \ ${vp9:+-consumer avformat:${stem}_vp9.webm${multipass:+ pass=2 passlogfile=${stem}_vp9} $_melt_vp9 $_melt_opus} \ ${mp4:+-consumer avformat:$stem.mp4 $_melt_h264 $_melt_aac} if [ -n "$mp4" ] && [ -z "$melt_recent" ]; then mv "$stem.mp4" "$stem.mp4"~ qt-faststart "$stem.mp4"~ "$stem.mp4" [ -f "$stem.mp4" ] && rm "$stem.mp4"~ || mv -f "$stem.mp4"~ "$stem.mp4" fi # cleanup audio normalize hinting rm -f $stem.xml # JPEG preview $melt $infile_first in=0 out=0 \ -group $filters \ -consumer avformat:$stem.jpg $_melt_img __width="${_width:+ width=\"$_width\"}" __height="${_height:+ height=\"$_height\"}" # Flash object needs extra space for controllers __heightplus=${_height:+ height=\"$(($_height+4))\"} _source_ogg="" _source_webm="" _source_vp9="" _source_mp4="" [ -z "$flashplayer" ] || flash=yes [ -n "$mp4" ] || [ -z "$flash" ] || error1 "Cannot enable flash when mp4 format is disabled." _object_flash="" _param_name="" _param_flashvars="" __oggfile=${ogg:+open format Ogg} __webmfile=${webm:+open format WebM (VP8)} __vp9file=${vp9:+open format WebM (VP9/Opus)} __mp4file=${mp4:+closed format MPEG-4} cat >"$stem.html" < ${mp4:+$_source_mp4 }${vp9:+$_source_vp9 }${webm:+$_source_webm }${ogg:+$_source_ogg }${flash:+$_object_flash $_param_name $_param_flashvars }$title ${flash:+ }

Download Video:

    ${vp9:+
  • $__vp9file }${webm:+
  • $__webmfile }${ogg:+
  • $__oggfile }${mp4:+
  • $__mp4file }

EOF