#!/bin/sh ## TODO: handle srt encoding? _usage () { cat <max) {max=t[$NF]; val=$NF}} END{print val}' } ## Usage: _cropvalue FILE STEP ## Return the crop values as ffmpeg output them. _cropvalue() { local step=$2 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 } ## Return the audio encoding parameters. For instance ## -c:2 libvorbis -b:2 384k -c:5 copy ## If input codec is $AUDIO_CODEC, we copy. If some stream bitrates are missing ## or 0, we encode it to a default value. If default value is 0, then we copy ## stream. _audiobitrate() { local bitrate for i in $(seq 0 $(($format_nb_streams-1)) ); do ## Skip non audio tracks. [ "$(eval echo \$streams_stream_${i}_codec_type)" != "audio" ] && continue bitrate=$(eval echo \$streams_stream_${i}_bit_rate) if [ -n "$bitrate" ] && [ "$bitrate" -gt 0 ] 2>/dev/null; then ## If non-empty and a positive number. bitrate=$(( $bitrate / 1000)) else bitrate="$AUDIO_DEFAULT_RATE" fi if [ "$bitrate" -le 0 ] || \ [ "$AUDIO_CODEC" = "$(eval echo \$streams_stream_${i}_codec_name)" ] || \ [ "$AUDIO_CODEC" = "lib$(eval echo \$streams_stream_${i}_codec_name)" ]; then printf -- "-c:%s copy " $i else [ $bitrate -gt 500 ] && bitrate=500 printf -- "-c:%s %s -b:%s %sk " $i "$AUDIO_CODEC" $i "$bitrate" fi done } _transcode() { echo "==> [$1]" OUTPUT="${1%.*}.$EXT" [ -e "$OUTPUT" ] && OUTPUT="${1%.*}-$(date '+%F-%H%M%S').$EXT" ## Metadata (i.e. tags + technical data). _buffer="$(ffprobe -v quiet -print_format flat=s=_ -show_streams -show_format "$1")" if [ $? -ne 0 ]; then _error "File [$1] is unsupported by FFmpeg." return fi eval "$_buffer" unset _buffer STREAM_TITLE="" if $OPT_REMOVE_TITLE; then for i in $(seq 0 $(($format_nb_streams-1)) ); do STREAM_TITLE="${STREAM_TITLE}-metadata:s:$i title= " done fi if $OPT_CROP; then echo "Computing crop values... " ## 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. This is much faster than encoding in one pass with low ## framerate. STEP=${format_duration:-$streams_stream_0_duration} STEP="${STEP%%.*}" STEP=$(($STEP/6)) VIDEO_FILTER="-vf $(_cropvalue "$1" "$STEP")" if $OPT_CROPPREVIEW; then echo "Generating preview... " 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 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<