Compare commits

...

No commits in common. "v0.11.3" and "master" have entirely different histories.

2 changed files with 161 additions and 49 deletions

42
README.md Normal file
View file

@ -0,0 +1,42 @@
# burnSubs
You found my burnSubs tool. The goal for this tool is to ease the
conversion of arbitrary video files with soft-subtitles in the SSA/ASS
Substation Alpha format into simple stereo hard-subtitled video files.
## Features
* softsub to hardsub conversion
* embedded font files
* selecting specific audio streams
* selecting specific video streams
* automatic selection of language preferences
* default surround sound to stereo down-mixing
* opt out CLI flag available
* anti-clobbering default behavior
* auto-cleanup on error
## Prerequisite Tools
Firstly, the tool will yell at you if the tools it needs don't exist.
Feel free to just run the tool, and it will let you know what you're
missing.
* [`ffmpeg`](https://ffmpeg.org/) - the one and only
* `ffprobe` - usually comes with ffmpeg
* [`jq`](https://stedolan.github.io/jq/) - file/pipe based JSON processor
## How does it work?
`burnSubs` takes an input video file and tries to figure out what
audio streams and subtitle streams exist within the file. It stores
metadata in `/tmp` while it runs. When running it will pull the
streams within the input file, and try to select Japanese language
audio streams, and a non-signs subtitle stream (i.e. a full language
subtitle stream) to add to the output video.
Before transcoding it will then extract the subtitle file to pass into
`ffmpeg`'s subtitle burn in filter, and will try to down-mix any
surround sound input streams to stereo. Downmixing, track selection,
clobbering behavior, and verbosity can all be controlled to a limited
extent by CLI flags.
## Can you make it do *XYZ*.
Give me an enhancement request in github and I'll take a look.

168
burnSubs
View file

@ -5,7 +5,7 @@ set -o errexit
################################################################################
# burnSubs
# version 0.11.3
# version 0.13.3
#################
# Wishlist:
# queue encodes
@ -13,11 +13,9 @@ set -o errexit
# finish help flag
# audio recode flag
#
# Changes
# automatically select JPN audio if more than one audio channel found.
################################################################################
DEFAULTS_OPTS_CRF=20
DEFAULTS_OPTS_CRF=18
function machineSetup() {
# Default setup
@ -57,7 +55,7 @@ function machineSetup() {
#export FFMPEG="/opt/ffmpeg-nvenc/bin/ffmpeg"
export LD_LIBRARY_PATH="/opt/ffmpeg-nvenc/lib"
export FF_EXT="${FF_EXT} -pix_fmt yuv420p"
elif [[ "$(hostname)" == "grad-heo-lappy" ]]; then
elif [[ "$(hostname)" == "random-vaapi-intel-laptop" ]]; then
echo " > Hostname: $(hostname)"
export OPTS_ENC="vaapi"
else
@ -67,20 +65,6 @@ function machineSetup() {
fi
fi
# Configure audio filtergraph if needed.
if [[ "${OPTS_TRANS_AUDIO}" == true ]]; then
FILT_AUDIO="-c:a aac"
if [[ "${OPTS_LPF_AUDIO}" == true ]]; then
FILT_AUDIO="-filter:a highpass=f=7 ${FILT_AUDIO}"
fi
if [[ "$(hostname)" == "Ram-the-Red" ]]; then
FILT_AUDIO="${FILT_AUDIO} -strict -2"
fi
else
FILT_AUDIO="-c:a copy"
fi
# Configure the encoder based upon the specific encoder chain required
## NVIDIA GPU Encode/Decode
if [[ "$OPTS_ENC" == "nvidia" ]]; then
@ -153,9 +137,9 @@ function setupBins() {
setAndValidateBin "ffmpeg" "FFMPEG"
setAndValidateBin "ffprobe" "FFPROBE"
setAndValidateBin "jq" "JQ"
setAndValidateBin "python3" "PYTHON3"
setAndValidateBin "awk" "AWK"
setAndValidateBin "date" "DATE"
#setAndValidateBin "python3" "PYTHON3"
#setAndValidateBin "awk" "AWK"
#setAndValidateBin "date" "DATE"
}
# Used by the above function to evaluate overrides if they are set, and
@ -189,6 +173,7 @@ function doCleanup() {
echo "=> Cleaning up."
rm -r "$TMP"
else
set +x
echo "tmp dir: $TMP"
fi
@ -296,10 +281,45 @@ function parseStreams() {
"$JQ" 'reduce .streams[] as $item ([]; if ($item.codec_name == "ass" or $item.codec_name == "dvd_subtitle") then [.[],$item] else . end) | reduce .[] as $item ([]; [.[],{t:($item.tags.title),i:($item.index),lang:$item.tags.language, disposition:$item.disposition, codec:($item.codec_name)}]) | reduce .[] as $item ([]; [.[],{t:($item.t // ($item.lang + "-" + ($item.i | tostring))),i:$item.i,lang:$item.lang,disposition:$item.disposition,codec:$item.codec}])' "${STREAMS_ALL}" > "${STREAMS_SUB}"
export SUB_COUNT=$("$JQ" 'length' "${STREAMS_SUB}")
# shellcheck disable=SC2016
"$JQ" 'reduce .streams[] as $item ([]; if ($item.codec_type == "audio") then [.[],$item] else . end) | reduce .[] as $item ([]; [.[],{t:($item.tags.title),i:($item.index),lang:$item.tags.language, disposition:$item.disposition}]) | reduce .[] as $item ([]; [.[],{t:($item.t // ($item.lang + "-" + ($item.i | tostring))),i:$item.i,lang:$item.lang,disposition:$item.disposition}])' "${STREAMS_ALL}" > "${STREAMS_AUDIO}"
"$JQ" 'reduce .streams[] as $item ([]; if ($item.codec_type == "audio") then [.[],$item] else . end) | reduce .[] as $item ([]; [.[],{t:($item.tags.title),i:($item.index),lang:$item.tags.language, disposition:$item.disposition, chan:{channels:$item.channels, channel_layout:$item.channel_layout}}]) | reduce .[] as $item ([]; [.[],{t:($item.t // ($item.lang + "-" + ($item.i | tostring))),i:$item.i,lang:$item.lang,disposition:$item.disposition, chan:$item.chan}])' "${STREAMS_ALL}" > "${STREAMS_AUDIO}"
export AUDIO_COUNT=$("$JQ" 'length' "${STREAMS_AUDIO}")
}
function setupAudioTranscode() {
if [[ $AUDIO_COUNT -ne 1 ]]; then
CHANNEL_COUNT=$($JQ '.[] | select(.i == '$AUDIO_INDEX') | .chan.channels' "$STREAMS_AUDIO")
CHANNEL_LAYOUT=$($JQ '.[] | select(.i == '$AUDIO_INDEX') | .chan.channel_layout' "$STREAMS_AUDIO")
else
CHANNEL_COUNT=$($JQ '.[] | .chan.channels' "$STREAMS_AUDIO")
CHANNEL_LAYOUT=$($JQ '.[] | .chan.channel_layout' "$STREAMS_AUDIO")
fi
if [[ "${CHANNEL_COUNT}" == "6" && "${CHANNEL_LAYOUT}" == '"5.1"' ]]; then
# check if we're 5.1 and if so flag transcode.
export OPTS_TRANS_AUDIO=true
elif [[ "${CHANNEL_COUNT}" != "2" && "${CHANNEL_LAYOUT}" != '"5.1"' ]]; then
echo "ERROR: Trying to enocde non 5.1 and non-stereo audio stream."
exit 1
fi
# Configure audio filtergraph if needed.
if [[ "${OPTS_TRANS_AUDIO}" == true ]]; then
FILT_AUDIO="-c:a aac"
if [[ "${OPTS_LPF_AUDIO}" == true ]]; then
FILT_AUDIO="-filter:a highpass=f=7 ${FILT_AUDIO}"
fi
if [[ "${OPTS_SURROUND_PRESERVE}" == false ]]; then
# From https://superuser.com/questions/852400/properly-downmix-5-1-to-stereo-using-ffmpeg
# Nightmode Formula
FILT_AUDIO="-filter:a pan=stereo|FL=FC+0.30*FL+0.30*BL|FR=FC+0.30*FR+0.30*BR ${FILT_AUDIO}"
fi
else
FILT_AUDIO="-c:a copy"
fi
}
function listSubtitles() {
@ -365,9 +385,9 @@ function selectSubs() {
# try to avoid a signs and lyrics track
LANG_SINGLE_SELECT=$($JQ '[.[].lang] | index("'${OPTS_SELSUB_LANG}'")' "$STREAMS_SUB")
echo "Multiple english tracks found."
export SUBTITLE_INDEX=$($JQ 'reduce .[] as $trk ([]; if ($trk.lang == "'${OPTS_SELSUB_LANG}'" and ( ($trk.t | test("lyrics";"i") or ($trk.t | test("signs";"i")) ) | not ) ) then [.[],{t:$trk.t,i:$trk.i}] else . end) | .[].i' "$STREAMS_SUB")
export SUBTITLE_INDEX=$($JQ 'reduce .[] as $trk ([]; if ($trk.lang == "'${OPTS_SELSUB_LANG}'" and (( ($trk.t | test("lyrics";"i")) or ($trk.t | test("signs";"i")) or ($trk.t | test("dub";"i")) )|not) ) then [.[],{t:$trk.t,i:$trk.i}] else . end) | .[].i' "$STREAMS_SUB")
# And display rejected subtitles too.
SUBTITLE_REJECT_LIST=($($JQ 'reduce .[] as $trk ([]; if ($trk.lang == "'${OPTS_SELSUB_LANG}'" and ( ($trk.t | test("lyrics";"i") or ($trk.t | test("signs";"i")) ) ) ) then [.[],{t:$trk.t,i:$trk.i}] else . end) | .[].t' "$STREAMS_SUB"))
SUBTITLE_REJECT_LIST=($($JQ 'reduce .[] as $trk ([]; if ($trk.lang == "'${OPTS_SELSUB_LANG}'" and (( ($trk.t | test("lyrics";"i")) or ($trk.t | test("signs";"i")) or ($trk.t | test("dub";"i")) )) ) then [.[],{t:$trk.t,i:$trk.i}] else . end) | .[].t' "$STREAMS_SUB"))
for REJECT_SUB in ${SUBTITLE_REJECT_LIST[@]}; do
echo " > rejecting ${REJECT_SUB}"
done
@ -428,17 +448,21 @@ function extractSubs() {
function doTranscode() {
echo "=> Starting transcode:"
# shellcheck disable=SC2086
echo "$FFMPEG" ${FF_HW} -i "${INPUT_VIDEO}" -sn ${LIM_TIME} \
echo "$FFMPEG" ${FF_VERBOSITY} ${FF_HW} -i "${INPUT_VIDEO}" \
-sn ${LIM_TIME} \
-filter:v "${FILT_PFX}ass=${SUBTITLE_FILE}${FILT_SFX}" \
${FILT_AUDIO} -c:v "${FF_ENC}" ${FF_STD} ${FF_EXT} ${FF_AUDIO} \
${FILT_AUDIO} \
-c:v "${FF_ENC}" ${FF_STD} ${FF_EXT} ${FF_AUDIO} \
"${OUTPUT_VIDEO}"
if [[ "$OPTS_DRYRUN" == true ]]; then
return
fi
# shellcheck disable=SC2086
"$FFMPEG" ${FF_HW} -i "${INPUT_VIDEO}" -sn ${LIM_TIME} \
"$FFMPEG" ${FF_VERBOSITY} ${FF_HW} -i "${INPUT_VIDEO}" \
-sn ${LIM_TIME} \
-filter:v "${FILT_PFX}ass=${SUBTITLE_FILE}${FILT_SFX}" \
${FILT_AUDIO} -c:v "${FF_ENC}" ${FF_STD} ${FF_EXT} ${FF_AUDIO} \
${FILT_AUDIO} \
-c:v "${FF_ENC}" ${FF_STD} ${FF_EXT} ${FF_AUDIO} \
"${OUTPUT_VIDEO}"
export FINAL_STATUS=$?
}
@ -496,6 +520,8 @@ OPTS_DEBUG=false
OPTS_LPF_AUDIO=false
OPTS_TRANS_AUDIO=false
OPTS_derived_NO_OUTPUT=false
OPTS_VERBOSITY=1
OPTS_SURROUND_PRESERVE=false
unset OPT_CRF
# this is the --icon flag passed to notify-send at the end of the transcode
NOTIFY_ICON="face-tired"
@ -515,7 +541,8 @@ FINAL_STATUS=1
######
# Reformat and organize the input strings
OPT_STRING=$(getopt -o 'hkls:a:dt:' --long 'help,psoft,soft,dry,crf:,audio,audiofix,alocale:,slocale:' -- "$@")
OPT_STRING=$(getopt -o 'hkls:a:dt:vq' \
--long 'help,psoft,soft,dry,crf:,audio,audiofix,alocale:,slocale:,verbose,quiet,keep-surround' -- "$@")
# reassign them as positional arguments
eval set -- "$OPT_STRING"
@ -591,6 +618,13 @@ while true; do
shift
continue
;;
"--keep-surround")
OPTS_SURROUND_PRESERVE=true
OPTS_TRANS_AUDIO=true
echo ">> !! preserving 5.1/7.1 surround sound if available."
shift
continue
;;
"--soft")
OPTS_FORCESOFT=true
echo ">> !! forcing software decoding/encoding."
@ -616,6 +650,22 @@ while true; do
shift
continue
;;
"--verbose"|"-v")
if [[ ${OPTS_VERBOSITY} -ne 0 ]]; then
OPTS_VERBOSITY=2
echo ">> !! print ffmpeg header."
else
echo ">> !! Ignoring verbosity flag. Quiet takes precidence."
fi
shift
continue
;;
"--quiet"|"-q")
OPTS_VERBOSITY=0
echo ">> !! print ffmpeg header."
shift
continue
;;
"--")
shift # all arguments parsed
break
@ -627,28 +677,31 @@ while true; do
#echo "TODO: HELP!" # Display HELP
cat << _EOT
----------------------------------------------------------------------------
-k auto-klobber when ffmpeg asks
-k auto-klobber when ffmpeg asks
-t <t> ffmpeg encoding time limit
--crf <#> override CRF setting
default: $DEFAULTS_OPTS_CRF
--soft force software decode and encode
--psoft use software encoding (allow hardware decode when available)
--audiofix transcode audio
--lpf transcode audio, and low-pass filter as well
-t <t> ffmpeg encoding time limit
--crf <#> override CRF setting
default: $DEFAULTS_OPTS_CRF
--soft force software decode and encode
--psoft use software encoding (allow hardware decode when available)
--audiofix transcode audio
--keep-surround try to preserve surround sound rather than downmixing to stereo.
--lpf transcode audio, and low-pass filter as well
-l list subtitles and audio tracks (no encoding)
-s <#> select specific subtitle track number
#TODO: verify legal subtitle track number convention
-a <#> select specific audio track number
#TODO: verify legal audio track number convention
--slocale 'eng' select specific subtitle track number
#TODO: verify legal subtitle track number convention
--alocale 'jpn' select specific audio track number
#TODO: verify legal audio track number convention
-l list subtitles and audio tracks (no encoding)
-s <#> select specific subtitle track number
#TODO: verify legal subtitle track number convention
-a <#> select specific audio track number
#TODO: verify legal audio track number convention
--slocale 'eng' select specific subtitle track number
#TODO: verify legal subtitle track number convention
--alocale 'jpn' select specific audio track number
#TODO: verify legal audio track number convention
-d debug (no cleanup)
--dry dry run (no encoding)
-d debug (no cleanup)
--dry dry run (no encoding)
-q, --quiet make ffmpeg shutup
-v, --verbose show all FFMPEG details (except that ruddy header)
_EOT
exit
;;
@ -674,6 +727,19 @@ else
exit
fi
# Actual verbosity parsing
if [[ ${OPTS_VERBOSITY} -le 0 ]]; then
FF_VERBOSITY="-hide_banner -loglevel error"
elif [[ ${OPTS_VERBOSITY} -eq 1 ]]; then
FF_VERBOSITY="-hide_banner -loglevel error -stats"
else # ie [[ ${OPTS_VERBOSITY} -ge 2 ]]; then
FF_VERBOSITY="-hide_banner"
fi
if [[ "$OPTS_DEBUG" == "true" ]]; then
set -x
fi
###############
# Configure the encoder based upon the hostname
machineSetup
@ -702,6 +768,10 @@ fi
selectSubs
# extract the selected subtitle file
extractSubs $SUBTITLE_INDEX
# Configure the audio straem
setupAudioTranscode
# Set the bitrate if that function wasn't disabled
runExtraProc "h264_vaapi"