2013-03-03 00:01:20 +01:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
## User options
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
## To check what fmmpeg supports:
|
|
|
|
# ffmpeg -codecs
|
|
|
|
# ffmpeg -formats
|
|
|
|
|
|
|
|
## Useful guides
|
|
|
|
## x264: http://ffmpeg.org/trac/ffmpeg/wiki/x264EncodingGuide
|
|
|
|
## ID3 details: http://en.wikipedia.org/wiki/ID3
|
|
|
|
|
|
|
|
TC_VIDEO_AUDIO_QUAL=192k
|
|
|
|
|
|
|
|
## quality*size ~= 1/speed
|
|
|
|
# ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo
|
|
|
|
TC_VIDEO_PRESET=slow
|
|
|
|
|
|
|
|
## Overall quality. Logarithmic scale.
|
|
|
|
# 18 is near perfection.
|
|
|
|
# 22 is really good compression, while a bit more blurry than the original.
|
|
|
|
# 20 is a good compromise.
|
|
|
|
TC_VIDEO_QUAL=20
|
|
|
|
|
|
|
|
## x264 tuning (presets).
|
|
|
|
## Possible values: film, animation, grain, ...
|
|
|
|
## See x264 --fullhelp.
|
|
|
|
## No tuning by default.
|
|
|
|
# TC_VIDEO_TUNE="-tune film"
|
|
|
|
TC_VIDEO_TUNE=""
|
|
|
|
|
|
|
|
## x264 options.
|
|
|
|
## Possible values: hex, umh...
|
|
|
|
## me=umh is default.
|
|
|
|
# TC_VIDEO_OPTS="-x264opts me=hex"
|
|
|
|
TC_VIDEO_OPTS=""
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
## End of user options
|
|
|
|
################################################################################
|
|
|
|
|
2013-05-05 10:19:01 +02:00
|
|
|
## TODO: option to replace original file.
|
2013-03-03 00:01:20 +01:00
|
|
|
## TODO: audio quality. Copy if AAC or OGG <320k.
|
|
|
|
## TODO: crop options.
|
|
|
|
## TODO: handle srt encoding.
|
|
|
|
## TODO: separate functions:
|
|
|
|
## tc_video_batch
|
|
|
|
## tc_video_transcode
|
|
|
|
|
|
|
|
if [ -z "$(command -v ffmpeg)" ]; then
|
|
|
|
echo "ffmpeg required."
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
_printhelp()
|
|
|
|
{
|
|
|
|
cat <<EOF
|
2013-03-03 11:19:50 +01:00
|
|
|
Usage: ${1##*/} [OPTIONS] FILES|FOLDERS
|
2013-03-03 00:01:20 +01:00
|
|
|
|
|
|
|
Transcode FILES or files found in FOLDERS to .mkv with x264 and ogg. Output
|
|
|
|
files are the same as the original, with time appended. You can customize
|
|
|
|
encoding with the TC_* variables.
|
|
|
|
|
2013-05-03 23:44:53 +02:00
|
|
|
-f: Overwrite existing file if any.
|
|
|
|
-h: Display this help.
|
2013-05-14 16:38:27 +02:00
|
|
|
-o OPT: Additional options.
|
2013-05-03 23:44:53 +02:00
|
|
|
-s: Sample of 5 minutes.
|
|
|
|
-S MIN: Sample of MIN minutes.
|
2013-05-14 16:38:27 +02:00
|
|
|
-t: Remove all "title" metadata.
|
2013-05-05 10:19:01 +02:00
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
2013-05-14 16:38:27 +02:00
|
|
|
* Exchange stream 1 an 2 by first removing them, then reading them in the
|
2013-05-05 10:19:01 +02:00
|
|
|
desired order.
|
|
|
|
|
|
|
|
${1##*/} -o '-map -0:1 -map -0:2 -map 0:2 -map 0:1' input.mkv
|
|
|
|
|
|
|
|
* Change audio stream 1 title and remove audio stream 2 title:
|
|
|
|
|
|
|
|
${1##*/} -o '-metadata:s:a:0 title="FR: OGG Stereo" -metadata:s:a:1 title=' input.mkv
|
|
|
|
|
2013-03-03 00:01:20 +01:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
## What to do if file exists:
|
|
|
|
# -y overwrite
|
|
|
|
# -n do not overwrite.
|
|
|
|
TC_OVERWRITE="-n"
|
|
|
|
TC_SAMPLE=""
|
2013-05-05 10:19:01 +02:00
|
|
|
TC_USER_OPT=""
|
2013-05-14 16:38:27 +02:00
|
|
|
TC_REMOVE_TITLE=0
|
2013-03-03 00:01:20 +01:00
|
|
|
|
2013-05-14 16:38:27 +02:00
|
|
|
while getopts ":fho:sS:t" opt; do
|
2013-03-03 00:01:20 +01:00
|
|
|
case $opt in
|
|
|
|
f)
|
|
|
|
TC_OVERWRITE="-y"
|
|
|
|
;;
|
|
|
|
h)
|
|
|
|
_printhelp "$0"
|
|
|
|
exit 1
|
|
|
|
;;
|
2013-05-05 10:19:01 +02:00
|
|
|
o)
|
|
|
|
TC_USER_OPT="$OPTARG"
|
|
|
|
;;
|
2013-03-03 00:01:20 +01:00
|
|
|
s)
|
2013-05-03 23:44:53 +02:00
|
|
|
TC_SAMPLE="-ss 60 -t 360"
|
|
|
|
;;
|
|
|
|
S)
|
2013-05-05 10:19:01 +02:00
|
|
|
TC_SAMPLE="-ss 60 -t $((60*$OPTARG))"
|
2013-03-03 00:01:20 +01:00
|
|
|
;;
|
2013-05-14 16:38:27 +02:00
|
|
|
t)
|
|
|
|
TC_REMOVE_TITLE=1
|
|
|
|
;;
|
2013-03-03 00:01:20 +01:00
|
|
|
?)
|
|
|
|
_printhelp "$0"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
:)
|
|
|
|
echo "Missing argument."
|
2013-03-03 11:19:50 +01:00
|
|
|
_printhelp "$0"
|
2013-03-03 00:01:20 +01:00
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
shift $(($OPTIND - 1))
|
|
|
|
|
|
|
|
|
|
|
|
if [ $# -eq 0 ]; then
|
2013-05-05 20:57:20 +02:00
|
|
|
_printhelp "$0"
|
2013-03-03 00:01:20 +01:00
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
## Zsh compatibility. We need it otherwise word splitting of parameter like
|
|
|
|
## TC_SAMPLE will not work.
|
|
|
|
STATUS="$(set -o | grep 'shwordsplit' | awk '{print $2}')"
|
|
|
|
[ "$STATUS" = "off" ] && set -o shwordsplit
|
|
|
|
|
2013-05-03 23:44:53 +02:00
|
|
|
## 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 other formats.
|
2013-03-03 00:01:20 +01:00
|
|
|
_tc_transcode()
|
|
|
|
{
|
2013-05-05 20:57:20 +02:00
|
|
|
cat<<EOF
|
|
|
|
================================================================================
|
2013-05-14 16:38:27 +02:00
|
|
|
User options: ${TC_USER_OPT:-None}
|
|
|
|
Sample: ${TC_SAMPLE:-No}
|
|
|
|
Clear 'Title': $TC_REMOVE_TITLE
|
2013-05-05 20:57:20 +02:00
|
|
|
================================================================================
|
|
|
|
EOF
|
|
|
|
|
2013-05-14 16:38:27 +02:00
|
|
|
STREAM_TITLE=""
|
|
|
|
if [ $TC_REMOVE_TITLE -eq 1 ]; then
|
|
|
|
STREAM_NUM=$(ffmpeg -i "$1" 2>&1 | grep -c 'Stream')
|
|
|
|
for i in $(seq 0 $STREAM_NUM); do
|
|
|
|
STREAM_TITLE="${STREAM_TITLE}-metadata:s:$i title= "
|
|
|
|
done
|
|
|
|
# echo "$STREAM_NUM[$STREAM_TITLE]"
|
|
|
|
fi
|
|
|
|
|
2013-03-03 00:01:20 +01:00
|
|
|
ffmpeg $TC_OVERWRITE $TC_SAMPLE -i "$1" \
|
|
|
|
-c:v libx264 -preset $TC_VIDEO_PRESET -crf $TC_VIDEO_QUAL $TC_VIDEO_TUNE $TC_VIDEO_OPTS \
|
2013-05-03 23:44:53 +02:00
|
|
|
-c:a libvorbis -b:a $TC_VIDEO_AUDIO_QUAL -ac 2 \
|
2013-03-03 00:01:20 +01:00
|
|
|
-c:s copy \
|
2013-05-14 16:38:27 +02:00
|
|
|
-map 0 $STREAM_TITLE \
|
2013-05-05 10:19:01 +02:00
|
|
|
$TC_USER_OPT "${1%.*}-$(date '+%F-%H%M%S').mkv"
|
2013-03-03 00:01:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 <<EOF
|
|
|
|
$(find "$i" \( \
|
|
|
|
-iname '*.mkv' -o \
|
|
|
|
-iname '*.mp4' -o \
|
|
|
|
-iname '*.avi' -o \
|
|
|
|
-iname '*.webm' -o \
|
|
|
|
-iname '*.flv' -o \
|
|
|
|
-iname '*.wmv' -o \
|
|
|
|
-iname '*.mpg' \) )
|
|
|
|
EOF
|
|
|
|
|
|
|
|
else
|
2013-05-05 10:19:01 +02:00
|
|
|
## Argument is a regular file.
|
2013-03-03 00:01:20 +01:00
|
|
|
_tc_transcode "$i"
|
|
|
|
fi
|
|
|
|
|
|
|
|
done
|
|
|
|
|
|
|
|
## Restore Zsh previous options. This will not turn off shwordsplit if it
|
|
|
|
## was on before calling the function.
|
|
|
|
[ "$STATUS" = "off" ] && set +o shwordsplit
|