retry sup cropping with crop_sup

This commit is contained in:
2026-03-07 19:35:46 -06:00
parent 5261b67a18
commit 18815854f5

View File

@@ -198,6 +198,46 @@ replace_mkv_sup() {
"${mergeCmd[@]}" "${mergeCmd[@]}"
} }
crop_sup() {
local inSup="$1"
local outSup="$2"
local left="$3"
local top="$4"
local right="$5"
local bottom="$6"
local warnMsg='Window is outside new screen area'
local maxAcceptableWarn=5
local offset=5
# skip cropping if not needed
if [[ "${left}${top}${right}${bottom}" == "0000" ]]; then
cp "${inSup}" "${outSup}" || return 1
return 0
fi
for ((try = 0; try < 30; try++)); do
echo_info "cropping sup with ${left} ${top} ${right} ${bottom}"
"${SUPMOVER}" \
"${inSup}" \
"${outSup}" \
--crop \
"${left}" "${top}" "${right}" "${bottom}" &>"${outSup}.out" || return 1
# supmover does not error for out-of-bounds subtitles
# so adjust crop value until there is most certainly no issue
if [[ "$(grep -c "${warnMsg}" "${cropSup}.out")" -gt ${maxAcceptableWarn} ]]; then
echo_warn "${warnMsg}, retrying... (try ${try})"
test "${left}" -gt ${offset} && left=$((left - offset))
test "${top}" -gt ${offset} && top=$((top - offset))
test "${right}" -gt ${offset} && right=$((right - offset))
test "${bottom}" -gt ${offset} && bottom=$((bottom - offset))
else
return 0
fi
done
# if we got here, all tries were had, so indicate failure
return 1
}
# extract PGS_SUB_STREAMS from INPUT # extract PGS_SUB_STREAMS from INPUT
# and crop using CROP_VALUE # and crop using CROP_VALUE
setup_pgs_mkv() { setup_pgs_mkv() {
@@ -226,19 +266,15 @@ setup_pgs_mkv() {
cropMkv="${tmpdir}/${stream}.mkv" cropMkv="${tmpdir}/${stream}.mkv"
mkvextract "${INPUT}" tracks "${stream}:${ogSup}" || return 1 mkvextract "${INPUT}" tracks "${stream}:${ogSup}" || return 1
# check if PGS was uncropped # check sup resolution
local supRes local supRes
supRes="$(get_sup_resolution "${ogSup}")" || return 1 supRes="$(get_sup_resolution "${ogSup}")" || return 1
# crop PGS if either initially cropping or "fixing"
# previously uncropped (relative to video) PGS subs
if [[ ${CROP_VALUE} != '' || ${supRes} != "${vidRes}" ]]; then
local supWidth supHeight local supWidth supHeight
IFS=x read -r supWidth supHeight <<<"${supRes}" IFS=x read -r supWidth supHeight <<<"${supRes}"
local left top right bottom local left top right bottom
# determine crop values # determine crop values
# if the supfile is smaller than the video stream # if the supfile is smaller than the video stream
# crop using aspect ratio # crop using aspect ratio instead of resolution
if [[ ${vidWidth} -gt ${supWidth} || ${vidHeight} -gt ${supHeight} ]]; then if [[ ${vidWidth} -gt ${supWidth} || ${vidHeight} -gt ${supHeight} ]]; then
echo_warn "PGS sup (stream=${stream}) is somehow smaller than initial video stream" echo_warn "PGS sup (stream=${stream}) is somehow smaller than initial video stream"
echo_warn "cropping based off of aspect ratio instead of resolution" echo_warn "cropping based off of aspect ratio instead of resolution"
@@ -269,36 +305,10 @@ setup_pgs_mkv() {
bottom=${top} bottom=${top}
fi fi
# only crop if actually required if ! crop_sup "${ogSup}" "${cropSup}" "${left}" "${top}" "${right}" "${bottom}"; then
if [[ "${left}${top}${right}${bottom}" != "0000" ]]; then
# crop sup
(
set -x
"${SUPMOVER}" \
"${ogSup}" \
"${cropSup}" \
--crop \
"${left}" "${top}" "${right}" "${bottom}" &>"${cropSup}.out" || return 1
)
local cropRet=$?
# supmover does not error for out-of-bounds subtitles
# so error only when there is most certainly an issue
if [[ "$(grep -c 'Window is outside new screen area' "${cropSup}.out")" -gt 5 ]]; then
echo_fail "check ${cropSup}.out for complete logs"
cropRet=1
fi
if [[ ${cropRet} -ne 0 ]]; then
rm -r "${tmpdir}" || return 1 rm -r "${tmpdir}" || return 1
return 1 return 1
fi fi
else
# create placeholder copy for replacement
cp "${ogSup}" "${cropSup}" || return 1
fi
else
# create placeholder copy for replacement
cp "${ogSup}" "${cropSup}" || return 1
fi
if ! replace_mkv_sup "${INPUT}" "${cropSup}" "${cropMkv}" "${stream}"; then if ! replace_mkv_sup "${INPUT}" "${cropSup}" "${cropMkv}" "${stream}"; then
echo_fail "could not replace mkv sup for ${stream}" echo_fail "could not replace mkv sup for ${stream}"