tc-video-generic: dynamic audio bitrate almost done

master
Pierre Neidhardt 2013-10-24 16:48:45 +02:00
parent 5836eb3586
commit bbfcc7633d
1 changed files with 15 additions and 45 deletions

View File

@ -133,47 +133,23 @@ _audiobitrate()
## use mediainfo if available, ffmpeg otherwise.
## In the end, if some stream bitrates are missing, we have two
## possibilities: either we copy the stream, or we encore it to a default
## value.
## possibilities: either we copy the stream (default), or we encode it to a
## default value.
## Case1: copy streams
# if [ ! -n "$(command -v mediainfo)" ]; then
# mediainfo "$1" | awk -v codec="$AUDIO_CODEC" 'BEGIN {id=0} /^Audio/ { getline; while(getline && ! index($0,"Bit rate ")); if($4) printf "-c:a:" id " " codec " -b:a:" id " " $4 "k " ; else printf "-c:a:" id " copy "; id++} END {print ""}'
# else
# ffmpeg -i "$1" 2>&1 </dev/null | awk -v codec="$AUDIO_CODEC" 'BEGIN {id=0} /^ *Stream.*Audio/ {match($0, / [^ ]+ kb\/s/); res=substr($0, RSTART+1, RLENGTH-6); if(res) printf "-c:a:" id " " codec " -b:a:" id " " res "k " ; else printf "-c:a:" id " copy "; id++} END {print ""}'
# fi
## TODO: merge two cases, use default value when not 0, copy otherwise.
## Case2: encode to default
if [ ! -n "$(command -v mediainfo)" ]; then
mediainfo "$1" | awk -v codec="$AUDIO_CODEC" -v bitrate="$AUDIO_DEFAULT_RATE" 'BEGIN {id=0} /^Audio/ { getline; while(getline && ! index($0,"Bit rate ")); if($4) printf "-c:a:" id " " codec " -b:a:" id " " $4 "k " ; else printf "-c:a:" id " copy "; id++} END {print ""}'
## AWK note: in "(bitrate+0 > 500)", the +0 is required to make sure bitrate is numeric.
if [ -n "$(command -v mediainfo)" ]; then
mediainfo "$1" | awk -v codec="$AUDIO_CODEC" -v bitrate="$AUDIO_DEFAULT_RATE" 'BEGIN {id=0} /^Audio/ { getline; while(getline && ! index($0,"Bit rate ")); if($4) printf "-c:a:" id " " codec " -b:a:" id " " $4 "k " ; else if (bitrate > 0) printf "-c:a:" id " " codec " -b:a:" id " " bitrate "k "; else printf "-c:a:" id " copy "; id++} END {print ""}'
else
ffmpeg -i "$1" 2>&1 </dev/null | awk -v codec="$AUDIO_CODEC" -v bitrate="$AUDIO_DEFAULT_RATE" 'BEGIN {id=0} /^ *Stream.*Audio/ {match($0, / [^ ]+ kb\/s/); res=substr($0, RSTART+1, RLENGTH-6); if(res) printf "-c:a:" id " " codec " -b:a:" id " " res "k " ; else if (bitrate > 0) printf "-c:a:" id " " codec " -b:a:" id " " bitrate "k "; else printf "-c:a:" id " copy "; id++} END {print ""}'
ffmpeg -i "$1" 2>&1 </dev/null | awk -v codec="$AUDIO_CODEC" -v bitrate="$AUDIO_DEFAULT_RATE" 'BEGIN {id=0} /^ *Stream.*Audio/ {match($0, / [^ ]+ kb\/s/); res=substr($0, RSTART+1, RLENGTH-6); if(res || bitrate) {if(res) bitrate=res; if(bitrate+0 > 500) bitrate=500; printf "-c:a:" id " " codec " -b:a:" id " " bitrate "k "} else printf "-c:a:" id " copy "; id++} END {print ""}'
fi
## TODO: audio quality. Copy if AAC or OGG <320k.
# if [ -n "$(command -v mediainfo)" ]; then
# mediainfo "$1" | awk '/^Audio/ {while(getline && ! index($0,"Bit rate ")); if($4) print $4 ;else print 0}'
# else
# ffmpeg -i "$1" 2>&1 </dev/null | awk '/^ *Stream.*Audio/{match($0, / [^ ]+ kb\/s/); res=substr($0, RSTART+1, RLENGTH-6); if (res) print res; else print 0}'
# fi
# AUDIO_BITRATE="-b:a 192k"
## TODO: use 2 awk calls.
}
_tc_transcode()
{
echo "==> [$1]"
OUTPUT="${1%.*}-$(date '+%F-%H%M%S').mkv"
STREAM_TITLE=""
if $OPT_REMOVE_TITLE; then
@ -189,15 +165,14 @@ _tc_transcode()
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.
# AUDIO_FILTER="-ac 2"
## If AUDIO_FILTER is already set, it means that we copy.
## Only set AUDIO_PARAM if not already set.
if [ -z "$AUDIO_PARAM" ]; then
AUDIO_PARAM="$(_audiobitrate "$1") -ac 2"
## 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.
# AUDIO_FILTER="-ac 2"
AUDIO_PARAM="$(_audiobitrate "$1") -ac 2"
fi
cat<<EOF
@ -215,9 +190,6 @@ EOF
$OPT_PREVIEW && exit
OUTPUT="${1%.*}-$(date '+%F-%H%M%S').mkv"
## Zsh compatibility. We need it otherwise word splitting of parameter like
## SAMPLE will not work.
STATUS="$(set -o | grep 'shwordsplit' | awk '{print $2}')"
@ -230,7 +202,6 @@ EOF
-map 0 $STREAM_TITLE \
$SAMPLE $TC_VIDEO_OPT "$OUTPUT" </dev/null
## Restore Zsh previous options. This will not turn off shwordsplit if it
## was on before calling the function.
[ "$STATUS" = "off" ] && set +o shwordsplit
@ -249,7 +220,6 @@ for i in "$@"; do
## WARNING: ffmpeg continues to read stdin once it has started, so it should
## not be called from within a while<<EOF loop without redirecting stdin.
while IFS= read -r i; do
_tc_transcode "$j"
done<<EOF