tc-video-custom: tc-video script generator for custom encoding

master
Pierre Neidhardt 2013-08-04 11:29:35 +02:00
parent 563e38d1c4
commit dddf9e0eb2
8 changed files with 182 additions and 84 deletions

View File

@ -13,3 +13,4 @@ sourceforge.net
particuliers.societegenerale.fr
particuliers.secure.societegenerale.fr
aur.archlinux.org
.imdb.com

View File

@ -0,0 +1,122 @@
#!/bin/sh
if [ -z "$(command -v ffmpeg)" ]; then
echo "ffmpeg required."
exit
fi
_transcode ()
{
## 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
ffmpeg -i "$@" \
-c:v libx264 -preset slow -crf 20 \
-c:a libvorbis -b:a 192k -ac 2 \
-c:s copy \
-map 0 \
$TC_SAMPLE \
"${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.
[ "$STATUS" = "off" ] && set +o shwordsplit
}
_printhelp()
{
cat <<EOF
Usage: ${1##*/} [-f] [FILES]
Transcode FILES using custom FFmpeg command. By default FFmpeg will only encode
a sample for preview purpose. Use the '-f' flag to process the complete file.
Edit this file to fit your needs and learn more about FFmpeg.
EOF
}
TC_SAMPLE="-ss 60 -t 360"
while getopts ":hf" opt; do
case $opt in
h)
_printhelp "$0"
exit 1 ;;
f)
TC_SAMPLE="" ;;
?)
_printhelp "$0"
exit 1 ;;
:)
echo "Missing argument."
_printhelp "$0"
exit 1 ;;
esac
done
shift $(($OPTIND - 1))
## Choose to process all files in one batch
_transcode ###FILELIST
## Or one after another.
# while read -r i; do
# _transcode "$i"
# done<<EOF
# ###FILELINES
# EOF
################################################################################
## Help
##
## Intersting options:
##
## Exchange stream 1 an 2 by first removing them, then reading them in the
## desired order.
##
## -map -0:1 -map -0:2 -map 0:2 -map 0:1
##
## Change audio stream 1 title and remove audio stream 2 title:
##
## -metadata:s:a:0 title="FR: OGG Stereo" -metadata:s:a:1 title=""
##
## Demux audio:
## -vn -sn -c:a copy -map 0:1
##
## To dump audio to an ogg file:
## -vn -c:a libvorbis -b:a 192k
##
## 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
##
## Presets:
## quality*size ~= 1/speed
## ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo
##
## x264 overall quality (-crf) is a logarithmic scale.
## 18 is near perfection.
## 22 is really good compression, while a bit more blurry than the original.
## 20 is a good compromise.
##
## x264 tuning (-tune <preset>).
## Possible values: film, animation, grain, ...
## See x264 --fullhelp.
## No tuning by default.
##
## x264 options (-x264opts me=<value>).
## Possible values: hex, umh...
## me=umh is default.
##
## WARNING: you should consider mixing 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.
##
################################################################################

View File

@ -1,16 +0,0 @@
#!/bin/sh
if [ -z "$(command -v ffmpeg)" ]; then
echo "ffmpeg required."
exit
fi
if [ $# -ne 1 ]; then
echo "Usage: ${0##*/} VIDEO"
fi
## TODO: getopt support for audio quality.
TC_VIDEO_AUDIO_QUAL=192k
echo "================================================================================"
ffmpeg -i "$1" -vn -c:a libvorbis -b:a $TC_VIDEO_AUDIO_QUAL "${1%.*}-$(date '+%F-%H%M%S').ogg"

46
.scripts/tc-video-custom Executable file
View File

@ -0,0 +1,46 @@
#!/bin/sh
_printhelp()
{
cat <<EOF
Usage: ${1##*/} [FILES]
Create a 'tc-video' script in current folder. The script provides a FFmpeg call
command with some useful options and instructive help.
FILES realpath will be included into the script.
This script is meant to be easily editable to fit the user needs.
EOF
}
if [ "$1" = "-h" ]; then
_printhelp "$0"
exit
fi
if [ -z "$(command -v realpath)" ]; then
echo "realpath required."
exit
fi
for i ; do
REAL="$(realpath "$i")"
FILELIST="${FILELIST:+$FILELIST }'$REAL'"
FILELINES="${FILELINES:+$FILELINES
}$REAL"
done
SCRIPT_PATH="${0%/*}"
SCRIPT_NAME="${0##*/}"
OUTPUT_NAME="tc-video"
if [ -f "$SCRIPT_PATH/.$SCRIPT_NAME.in" ]; then
cp -i "$SCRIPT_PATH/.$SCRIPT_NAME.in" "$OUTPUT_NAME"
sed -i 's|###FILELIST|'"$FILELIST"'|' "$OUTPUT_NAME"
sed -i 's|###FILELINES|'"$FILELINES"'|' "$OUTPUT_NAME"
chmod +x "$OUTPUT_NAME"
else
echo ".$SCRIPT_NAME.in not found!"
fi

View File

@ -1,13 +0,0 @@
#!/bin/sh
if [ -z "$(command -v ffmpeg)" ]; then
echo "ffmpeg required."
exit
fi
if [ $# -ne 2 ]; then
echo "Usage: ${0##*/} VIDEO AUDIO_OUTPUT"
exit
fi
ffmpeg -i "$1" -vn -sn -c:a copy -map 0:1 "$2"

View File

@ -1,53 +1,9 @@
#!/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
################################################################################
## TODO: option to replace original file.
## 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."
@ -85,17 +41,23 @@ Examples:
EOF
}
## What to do if file exists:
# -y overwrite
# -n do not overwrite.
TC_OVERWRITE="-n"
TC_SAMPLE=""
TC_USER_OPT=""
TC_REMOVE_TITLE=false
TC_VIDEO_CODEC="libx264"
TC_AUDIO_CODEC="libvorbis"
TC_VIDEO_OPT="-c:v libx264 -preset $TC_VIDEO_PRESET -crf $TC_VIDEO_QUAL $TC_VIDEO_TUNE $TC_VIDEO_OPTS"
TC_AUDIO_OPT="-c:a libvorbis -b:a $TC_VIDEO_AUDIO_QUAL -ac 2"
TC_VIDEO_OPT="-preset slow -crf 20"
## What to do if file exists:
# -y overwrite
# -n do not overwrite.
TC_OVERWRITE="-n"
## 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.
TC_AUDIO_OPT="-b:a 192k -ac 2"
while getopts ":cfho:sS:t" opt; do
case $opt in
@ -140,10 +102,6 @@ fi
STATUS="$(set -o | grep 'shwordsplit' | awk '{print $2}')"
[ "$STATUS" = "off" ] && set -o shwordsplit
## 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.
_tc_transcode()
{
cat<<EOF

View File

@ -54,6 +54,6 @@ fi
cat "$1" | sed -e 's|/usr|/usr/local|g' >> "$UBUILD"
cat "$(realpath "$0").in" >> "$UBUILD"
cat "${0%/*}/.${0##*/}.in" >> "$UBUILD"
chmod +x "$UBUILD"