tc-video-generic: croping image preview!

master
Pierre Neidhardt 2013-10-24 21:52:34 +02:00
parent 89d056f5b8
commit c38fab4a8e
3 changed files with 35 additions and 14 deletions

View File

@ -63,13 +63,13 @@ _transcode ()
## You can choose here to process all files at the same time. Useful if you
## need to remux streams or to concatenate.
# ffmpeg -i ###FILELIST \
ffmpeg -i "$@" \
ffmpeg -nostdin -i "$@" \
-c:v libx264 -preset slow -crf 20 \
-c:a libvorbis -b:a 192k -ac 2 \
-c:s copy \
-map 0 \
$SAMPLE \
"${1%.*}-$(date '+%F-%H%M%S').mkv" </dev/null
"${1%.*}-$(date '+%F-%H%M%S').mkv"
## Restore Zsh previous options. This will not turn off shwordsplit if it
## was on before calling the function.

View File

@ -446,9 +446,9 @@ STATUS="$(set -o | grep 'shwordsplit' | awk '{print $2}')"
## With the -map_metadata parameter we clear all metadata.
## 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.
## be called from within a while<<EOF loop without disabling stdin.
echo ":: Processing..."
ffmpeg $LOGLEVEL $OVERWRITE -i "$1" -vn -sn $OGG_PARAM \
ffmpeg -nostdin $LOGLEVEL $OVERWRITE -i "$1" -vn -sn $OGG_PARAM \
-map_metadata -1 \
-metadata title="$OUTPUT_TITLE" \
-metadata artist="$OUTPUT_ARTIST" \
@ -457,7 +457,7 @@ ffmpeg $LOGLEVEL $OVERWRITE -i "$1" -vn -sn $OGG_PARAM \
-metadata album="$OUTPUT_ALBUM" \
-metadata album_artist="$OUTPUT_ARTIST" \
-metadata genre="$OUTPUT_GENRE" \
"$OUTPUT_FOLDER/$OUTPUT_FILE.$OUTPUT_EXT" </dev/null
"$OUTPUT_FOLDER/$OUTPUT_FILE.$OUTPUT_EXT"
echo ":: Process finished!"
## If we are overwriting inplace.

View File

@ -1,6 +1,6 @@
#!/bin/sh
## TODO: handle srt encoding.
## TODO: handle srt encoding?
_printhelp()
{
@ -20,6 +20,8 @@ Options:
-h: Display this help.
-o OPT: Additional options.
-p: Preview changes, do not encode.
-P: Generate two thumbnails, one cropped, the other not. Enable
auto-crop, do not encode.
-s: Sample of 5 minutes.
-S MIN: Sample of MIN minutes.
-t: Remove all "title" metadata.
@ -56,9 +58,10 @@ OPT_OVERWRITE=false
OPT_REMOVE_TITLE=false
OPT_CROP=false
OPT_CROPPREVIEW=false
OPT_PREVIEW=false
while getopts ":b:cCfho:psS:t" opt; do
while getopts ":b:cCfho:pPsS:t" opt; do
case $opt in
b)
AUDIO_DEFAULT_RATE=$OPTARG ;;
@ -77,6 +80,10 @@ while getopts ":b:cCfho:psS:t" opt; do
TC_VIDEO_OPT="$OPTARG" ;;
p)
OPT_PREVIEW=true ;;
P)
OPT_CROP=true
OPT_PREVIEW=true
OPT_CROPPREVIEW=true ;;
s)
SAMPLE="-ss 60 -t 360" ;;
S)
@ -109,7 +116,7 @@ fi
_duration()
{
## In seconds.
ffmpeg -i "$1" 2>&1 </dev/null | awk '/Duration/ {split($2, time, /:|\./); print time[1]*60*60 + time[2]*60 + time[3]}'
ffmpeg -nostdin -i "$1" 2>&1 | awk '/Duration/ {split($2, time, /:|\./); print time[1]*60*60 + time[2]*60 + time[3]}'
}
_highfreq()
@ -123,10 +130,20 @@ _cropvalue()
## 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 -ss $i -t 10 -i "$1" -vf "cropdetect=24:2:0" -f null - 2>&1 </dev/null
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
@ -140,7 +157,7 @@ _audiobitrate()
if [ ! -n "$(command -v mediainfo)" ]; then
mediainfo "$1" | awk 'BEGIN {id=0} /^Audio/ { getline; while(getline && ! index($0,"Bit rate ")); if($4) res=$4; else res=0; print id, res; id++}'
else
ffmpeg -i "$1" 2>&1 </dev/null | awk 'BEGIN {id=0} /^ *Stream.*Audio/ {match($0, / [^ ]+ kb\/s/); res=substr($0, RSTART+1, RLENGTH-6); print id, res; id++}'
ffmpeg -nostdin -i "$1" 2>&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 ""}'
}
@ -152,7 +169,7 @@ _tc_transcode()
STREAM_TITLE=""
if $OPT_REMOVE_TITLE; then
STREAM_NUM=$(ffmpeg -i "$1" 2>&1 </dev/null| grep -c 'Stream')
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
@ -161,6 +178,10 @@ _tc_transcode()
if $OPT_CROP; then
echo "Computing crop values... "
VIDEO_FILTER="-vf $(_cropvalue "$1") $VIDEO_FILTER"
if $OPT_CROPPREVIEW; then
echo "Generating preview... "
_croppreview "$1"
fi
fi
## Only if AUDIO_PARAM not already set.
@ -194,12 +215,12 @@ EOF
STATUS="$(set -o | grep 'shwordsplit' | awk '{print $2}')"
[ "$STATUS" = "off" ] && set -o shwordsplit
ffmpeg $OVERWRITE -i "$1" \
ffmpeg -nostdin $OVERWRITE -i "$1" \
$VIDEO_PARAM $VIDEO_FILTER \
$AUDIO_PARAM \
-c:s copy \
-map 0 $STREAM_TITLE \
$SAMPLE $TC_VIDEO_OPT "$OUTPUT" </dev/null
$SAMPLE $TC_VIDEO_OPT "$OUTPUT"
## Restore Zsh previous options. This will not turn off shwordsplit if it
## was on before calling the function.
@ -218,7 +239,7 @@ for i in "$@"; do
if [ -d "$i" ]; then
## 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.
## not be called from within a while<<EOF loop without disabling stdin.
while IFS= read -r i; do
_tc_transcode "$j"
done<<EOF