diff options
-rwxr-xr-x | localvideowebencode | 128 |
1 files changed, 79 insertions, 49 deletions
diff --git a/localvideowebencode b/localvideowebencode index 65e4d90..74bc152 100755 --- a/localvideowebencode +++ b/localvideowebencode @@ -31,7 +31,11 @@ Usage: $PRG [OPTION...] [--] [ARG=VALUE...] INPUTFILE... [ARG=VALUE...] Encode video file in multiple web-optimized formats, and provide sample html favoring open formats with optional non-JavaScript Flash fallback. - -p, --profile Video format: + -p, --profile Video format - size with optional rate suffix, + delimited by @ (except for widescreen labels): + e.g. 848x480@25 480p25 wvga@25 + (default: none) + -s, --size: Video frame size: [modulus 16] 320x240 qvga 640x480 vga 480p 848x480 wvga @@ -41,12 +45,12 @@ html favoring open formats with optional non-JavaScript Flash fallback. 240p 424x240 wqvga 480x360 hvga nhd 360p 640x360 800x600 svga - -s, --size Output size (ffmpeg): WIDTHxHEIGHT vga qcif etc. (default: use input size) -a, --aspect Display Aspect Ratio in melt format e.g. @16/9 (default: no aspect hinting) - -r, --rate Video framerate - (default: 25) + -r, --rate Video framerate - integer or fraction: + e.g. 15 1001/24000 25 1001/30000 + (default: use input framerate) -b, --bitrate Video bitrate in bytes, with optional ISO suffix (default: none) --h264profile MPEG-4 AVC target profile: baseline main high @@ -103,7 +107,6 @@ exit1() { } # defaults -rate=25 h264profile=baseline # parse cmdline options @@ -115,7 +118,7 @@ while true ; do -p|--profile) profile="$2"; shift 2;; -s|--size) size="$2"; shift 2;; -a|--aspect) aspect="$2"; shift 2;; - -r|--rate) rate="$2"; shift 2;; + -r|--rate) framerate="$2"; shift 2;; -b|--bitrate) bitrate="$2"; shift 2;; --h264profile) h264profile="$2"; shift 2;; --h264preset) h264preset="$2"; shift 2;; @@ -169,55 +172,74 @@ stem=${stem:-$(basename "$infile_first" | perl -pe 's/\.[^.]*//')} title=${title:-$stem} case "$profile" in - 320x240|qvga) - _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}" - _melt_video="${size:-width=320 height=240}${_melt_video:+ $_melt_video}" - ;; - 480x360|hvga) - _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}" - _melt_video="${size:-width=480 height=360}${_melt_video:+ $_melt_video}" - ;; - 640x480|vga) - _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}" - _melt_video="${size:-width=640 height=480}${_melt_video:+ $_melt_video}" - ;; - 800x600|svga) - _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}" - _melt_video="${size:-width=800 height=600}${_melt_video:+ $_melt_video}" - ;; - 1024x768|xga) - _melt_in="-profile=quarter_pal${_melt_in:+ $_melt_in}" - _melt_video="${size:-width=1024 height=768}${_melt_video:+ $_melt_video}" - webmpreset="${webmpreset:-720p}" - ;; - 240p|424x240|wqvga) - _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}" - _melt_video="${size:-width=424 height=240}${_melt_video:+ $_melt_video}" - ;; - 360p|640x360|nhd) - _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}" - _melt_video="${size:-width=640 height=360}${_melt_video:+ $_melt_video}" + *@*) + while read s r foo; do + size="${size:-$s}" + framerate="${framerate:-$r}" + done << EOF +$(echo "$profile" | perl -F@ -anE 'say join " ", @F') +EOF ;; - 480p|848x480|wvga) - _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}" - _melt_video="${size:-width=848 height=480}${_melt_video:+ $_melt_video}" + *p*) + while read s r foo; do + size="${size:-${s}p}" + framerate="${framerate:-$r}" + done << EOF +$(echo "$profile" | perl -Fp -anE 'say join " ", @F') +EOF ;; - 576p|1024x576|wsvga) - _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}" - _melt_video="${size:-width=1024 height=576}${_melt_video:+ $_melt_video}" + *) + size="$profile" ;; - 720p|1280x720|wxga|hd) - _melt_in="-profile=atsc_720p_25${_melt_in:+ $_melt_in}" - webmpreset="${webmpreset:-720p}" +esac + +case "$size" in + qvga) size=320x240;; + hvga) size=480x360;; + vga) size=640x480;; + svga) size=800x600;; + xga) size=1024x768;; + 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 d n foo; do + framerate_den="${framerate_den:-$d}" + framerate_num="${framerate_num:-$n}" + done << EOF +$(echo "$framerate" | perl -Fx -anE 'say join " ", @F') +EOF ;; - '') - _melt_video="frame_rate_den=1${_melt_video:+ $_melt_video}" - [ "25" != "$rate" ] || _melt_video="{_melt_video:+$_melt_video }frame_rate_num=$rate" + ?*) + framerate_den=1 + framerate_num="$framerate" ;; - *) exit1 "Unknown profile \"$profile\" - please specify size and aspect directly.";; esac -[ "25" = "$rate" ] || _melt_video="{_melt_video:+$_melt_video }frame_rate_num=$rate" +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 "$h264profile" in baseline|main) @@ -229,6 +251,14 @@ case "$h264profile" in *) exit1 "Unknown MPEG-4 AVC profile \"$h264profile\".";; esac +if [ -n "${width:-$width_in}" ] && [ -n "${height:-$height_in}" ]; then + _pixels="$((${width:-$width_in}*${height:-$height_in}))" +fi + +if [ -n "$pixels" ] && [ $pixels -ge $((1024*768)) ]; then + webmpreset="${webmpreset:-720p}" +fi + # default per-codec-channel bitrates bitrate_vorbis=64 bitrate_aac=96 @@ -264,7 +294,7 @@ esac # TODO: When verified beneficial, add option real_time=-2 melt="melt -progress" _melt_in="${_melt_in:+$_melt_in }$sample" -_melt_video="progressive=1 $_melt_video${bitrate:+ vb=${bitrate}}${size:+ s=$size}${aspect:+ aspect=$aspect}" +_melt_video="progressive=1${framerate:+ frame_rate_den="$framerate_den" frame_rate_num="$framerate_num"}${bitrate:+ vb=$bitrate}${size:+ s=${width:+$width}x${height:+$height}}${aspect:+ aspect=$aspect}" _melt_ogg="$_melt_video f=ogg vcodec=libtheora${bitrate:- qscale=5}" _melt_h264="$_melt_video $_melt_h264${bitrate:- qscale=5}" _melt_webm="$_melt_video vpre=libvpx-${webmpreset:-360p}" |