#!/bin/sh ## TODO: audio quality. Copy if AAC or OGG <320k. ## TODO: crop is not correct. ## TODO: handle srt encoding. if [ -z "$(command -v ffmpeg)" ]; then echo "ffmpeg required." exit fi _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 $OVERWRITE -i "$1" \ $VIDEO_CODEC $VIDEO_FILTER \ $AUDIO_CODEC \ -c:s copy \ -map 0 $STREAM_TITLE \ $SAMPLE $USER_OPT "$OUTPUT" if $OPT_OVERWRITE; then rm -f "$1" mv -f "$OUTPUT" "${1%.*}.mkv" fi echo } for i in "$@"; do ## Argument is a folder. We search for all video files in there. if [ -d "$i" ]; then ## WARNING: ffmpeg should not be called from within a while<