#!/bin/sh ## TODO: handle srt encoding? _printhelp() { cat <max) {max=t[$NF]; val=$NF}} END{print val}' } _cropvalue() { ## For 5 different timeslices of 1 second at every 1/6th of the video, we ## sample the crop values. We keep the values with highest frequency. STEP=$(($(_duration "$1")/6)) for i in $(seq $STEP $STEP $((5*$STEP))); do ffmpeg -nostdin -ss $i -i "$1" -t 10 -vf "cropdetect=24:2:0" -f null - 2>&1 done | _highfreq } _croppreview() { STEP=$(($(_duration "$1")/6)) for i in $(seq $STEP $STEP $((5*$STEP))); do ffmpeg -nostdin -v warning -y -ss $i -i "$1" \ -f image2 -vframes 1 $VIDEO_FILTER "${1%.*}-preview-$i-cropped.png" \ -f image2 -vframes 1 "${1%.*}-preview-$i-uncropped.png" done } _audiobitrate() { ## As of 2013-10-23, ffmpeg sucks at retrieving audio bitrate. Therefore we ## use mediainfo if available, ffmpeg otherwise. ## In the end, if some stream bitrates are missing, we have two ## possibilities: either we copy the stream (default), or we encode it to a ## default value. ## AWK note: in "(bitrate+0 > 500)", the +0 is required to make sure bitrate is numeric. ## WARNING: mediainfo puts a space every 3 digits, hence the 'for' loop. if command -v mediainfo >/dev/null; then mediainfo "$1" | awk 'BEGIN {id=0} /^Audio/ { getline; while(getline && ! index($0,"Bit rate ")); for(i=4; i&1 | awk 'BEGIN {id=0} /^ *Stream.*Audio/ {match($0, / [^ ]+ kb\/s/); res=substr($0, RSTART+1, RLENGTH-6); print id, res; id++}' fi | awk -v codec="$AUDIO_CODEC" -v bitrate="$AUDIO_DEFAULT_RATE" \ '{if($2 || bitrate) {if($2) bitrate=$2; if(bitrate+0 > 500) bitrate=500; printf "-c:a:" $1 " " codec " -b:a:" $1 " " bitrate "k "} else printf "-c:a:" $1 " copy "} END {print ""}' } _transcode() { echo "==> [$1]" OUTPUT="${1%.*}-$(date '+%F-%H%M%S').mkv" STREAM_TITLE="" if $OPT_REMOVE_TITLE; then STREAM_NUM=$(ffmpeg -nostdin -i "$1" 2>&1 | grep -c 'Stream') for i in $(seq 0 $STREAM_NUM); do STREAM_TITLE="${STREAM_TITLE}-metadata:s:$i title= " done fi if $OPT_CROP; then echo "Computing crop values... " VIDEO_FILTER="-vf $(_cropvalue "$1")" if $OPT_CROPPREVIEW; then echo "Generating preview... " _croppreview "$1" fi fi ## WARNING: we mix down audio to 2 channels with '-ac 2'. This greatly ## reduce file size and avoid any confusion for playback, which is often the ## case when converting DTS to any other format because DTS has embedded ## channel description which is not available in these formats. ! $OPT_COPY && AUDIO_PARAM="$(_audiobitrate "$1") -ac 2" cat<