From 362f44c319d1e6abd779cd41196b13a7c6c3ad06 Mon Sep 17 00:00:00 2001 From: Pierre Neidhardt Date: Tue, 22 Oct 2013 18:44:50 +0200 Subject: [PATCH] tc-video-generic: refactoring and croping support. --- .scripts/tc-video-generic | 138 ++++++++++++++++++++++++-------------- 1 file changed, 89 insertions(+), 49 deletions(-) diff --git a/.scripts/tc-video-generic b/.scripts/tc-video-generic index 4f2b06c8..064ad475 100755 --- a/.scripts/tc-video-generic +++ b/.scripts/tc-video-generic @@ -1,7 +1,7 @@ #!/bin/sh ## TODO: audio quality. Copy if AAC or OGG <320k. -## TODO: crop options. +## TODO: crop is not correct. ## TODO: handle srt encoding. if [ -z "$(command -v ffmpeg)" ]; then @@ -14,11 +14,12 @@ _printhelp() cat <&1 | awk '/Duration/ {split($2, time, /:|\./); print time[1]*60*60 + time[2]*60 + time[3]}' +} + +_highfreq() +{ + awk 'BEGIN{max=0} /crop=/ {t[$NF]++; if (t[$NF]>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 + echo -ss $i -t 10 + ffmpeg -ss $i -t 10 -i "$1" -vf "cropdetect=24:16:0" -f null - 2>&1 + done | _highfreq +} + _tc_transcode() { cat<&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 + VIDEO_FILTER="-vf $(_cropvalue "$1")" + fi + OUTPUT="${1%.*}-$(date '+%F-%H%M%S').mkv" - ffmpeg $TC_OVERWRITE -i "$1" \ - -c:v $TC_VIDEO_CODEC $TC_VIDEO_OPT \ - -c:a $TC_AUDIO_CODEC $TC_AUDIO_OPT \ + ffmpeg $OVERWRITE -i "$1" \ + $VIDEO_CODEC $VIDEO_FILTER \ + $AUDIO_CODEC \ -c:s copy \ -map 0 $STREAM_TITLE \ - $TC_SAMPLE $TC_USER_OPT "$OUTPUT" + $SAMPLE $USER_OPT "$OUTPUT" if $OPT_OVERWRITE; then rm -f "$1" mv -f "$OUTPUT" "${1%.*}.mkv" fi + echo } for i in "$@"; do @@ -140,19 +172,27 @@ for i in "$@"; do ## Argument is a folder. We search for all video files in there. if [ -d "$i" ]; then - ## TODO: provide max-depth as option. - while read -r j; do - _tc_transcode $j - done <