fix REPO_DIR and encode

This commit is contained in:
2025-08-29 17:24:11 -05:00
parent 0d515af0f0
commit b884307cc1
4 changed files with 62 additions and 22 deletions

View File

@@ -142,6 +142,34 @@ bash_basename() {
printf '%s\n' "${tmp:-/}" printf '%s\n' "${tmp:-/}"
} }
bash_realpath() {
local file=$1
local dir
# If the file is already absolute
[[ $file == /* ]] && {
printf '%s\n' "$file"
return
}
# Otherwise: split into directory + basename
dir="$(bash_dirname "${file}")"
file="$(bash_basename "${file}")"
# If no directory component, use current directory
if [[ $dir == "$file" ]]; then
dir="$PWD"
else
# Save current dir, move into target dir, capture $PWD, then return
local oldpwd="$PWD"
cd "$dir" || return 1
dir="$PWD"
cd "$oldpwd" || return 1
fi
printf '%s/%s\n' "$dir" "$file"
}
line_contains() { line_contains() {
local line="$1" local line="$1"
local substr="$2" local substr="$2"

View File

@@ -11,7 +11,8 @@ get_duration() {
get_crop() { get_crop() {
local file="$1" local file="$1"
local duration="$(get_duration "${file}")" local duration
duration="$(get_duration "${file}")" || return 1
# don't care about decimal points # don't care about decimal points
IFS='.' read -r duration _ <<<"${duration}" IFS='.' read -r duration _ <<<"${duration}"
# get crop value for first half of input # get crop value for first half of input
@@ -48,11 +49,12 @@ get_stream_codec() {
get_file_format() { get_file_format() {
local file="$1" local file="$1"
local probe="$(ffprobe \ local probe
probe="$(ffprobe \
-v error \ -v error \
-show_entries format=format_name \ -show_entries format=format_name \
-of default=noprint_wrappers=1:nokey=1 \ -of default=noprint_wrappers=1:nokey=1 \
"${file}")" "${file}")" || return 1
if line_contains "${probe}" 'matroska'; then if line_contains "${probe}" 'matroska'; then
echo mkv echo mkv
else else
@@ -88,14 +90,16 @@ get_num_audio_channels() {
-show_entries stream=channels \ -show_entries stream=channels \
-of default=noprint_wrappers=1:nokey=1 \ -of default=noprint_wrappers=1:nokey=1 \
"${file}" "${file}"
} }
unmap_streams() { unmap_streams() {
local file="$1" local file="$1"
local unmapFilter='bin_data|jpeg|png' local unmapFilter='bin_data|jpeg|png'
local unmap=() local unmap=()
for stream in $(get_streams "${file}"); do local streamsStr
streamsStr="$(get_streams "${file}")" || return 1
mapfile -t streams <<<"${streamsStr}" || return 1
for stream in "${streams[@]}"; do
if [[ "$(get_stream_codec "${file}" "${stream}")" =~ ${unmapFilter} ]]; then if [[ "$(get_stream_codec "${file}" "${stream}")" =~ ${unmapFilter} ]]; then
unmap+=("-map" "-0:${stream}") unmap+=("-map" "-0:${stream}")
fi fi
@@ -107,9 +111,10 @@ set_audio_bitrate() {
local file="$1" local file="$1"
local bitrate=() local bitrate=()
for stream in $(get_audio_streams "${file}"); do for stream in $(get_audio_streams "${file}"); do
local numChannels="$(get_num_audio_channels "${file}" "a:${stream}")" local numChannels codec
numChannels="$(get_num_audio_channels "${file}" "a:${stream}")" || return 1
local channelBitrate=$((numChannels * 64)) local channelBitrate=$((numChannels * 64))
local codec="$(get_stream_codec "${file}" "a:${stream}")" codec="$(get_stream_codec "${file}" "a:${stream}")" || return 1
if [[ ${codec} == 'opus' ]]; then if [[ ${codec} == 'opus' ]]; then
bitrate+=( bitrate+=(
"-c:a:${stream}" "-c:a:${stream}"
@@ -305,7 +310,8 @@ set_encode_opts() {
# use same container for output # use same container for output
if [[ $SAME_CONTAINER == "true" ]]; then if [[ $SAME_CONTAINER == "true" ]]; then
local fileFormat="$(get_file_format "${INPUT}")" local fileFormat
fileFormat="$(get_file_format "${INPUT}")" || return 1
FILE_EXT='' FILE_EXT=''
if [[ ${fileFormat} == 'MPEG-4' ]]; then if [[ ${fileFormat} == 'MPEG-4' ]]; then
FILE_EXT='mp4' FILE_EXT='mp4'
@@ -351,7 +357,7 @@ gen_encode_script() {
) )
local crop='' local crop=''
if [[ $CROP == "true" ]]; then if [[ $CROP == "true" ]]; then
crop="-vf $(get_crop "${INPUT}")" crop="$(get_crop "${INPUT}")" || return 1
fi fi
local videoEncoder='libsvtav1' local videoEncoder='libsvtav1'
local ffmpegVersion="$(ffmpeg_version)" local ffmpegVersion="$(ffmpeg_version)"
@@ -396,24 +402,30 @@ gen_encode_script() {
'-y' '-map' '0' '-y' '-map' '0'
) )
local unmap=($(unmap_streams "${INPUT}")) local unmapStr
unmapStr="$(unmap_streams "${INPUT}")" || return 1
mapfile -t unmap <<<"${unmapStr}"
if [[ ${unmap[*]} != '' ]]; then if [[ ${unmap[*]} != '' ]]; then
ffmpegParams+=('${unmap[@]}') ffmpegParams+=('${unmap[@]}')
fi fi
local audioBitrate=($(set_audio_bitrate "${INPUT}")) local audioBitrateStr
audioBitrateStr="$(set_audio_bitrate "${INPUT}")" || return 1
mapfile -t audioBitrate <<<"${audioBitrateStr}"
if [[ ${audioBitrate[*]} != '' ]]; then if [[ ${audioBitrate[*]} != '' ]]; then
ffmpegParams+=('${audioBitrate[@]}') ffmpegParams+=('${audioBitrate[@]}')
fi fi
if [[ ${crop} != '' ]]; then if [[ ${crop} != '' ]]; then
ffmpegParams+=('${crop}') ffmpegParams+=('-vf' '${crop}')
fi fi
ffmpegParams+=( ffmpegParams+=(
'-c:s' 'copy' '-c:s' 'copy'
) )
local convertSubs=($(convert_subs "${INPUT}")) local convertSubsStr
convertSubsStr="$(convert_subs "${INPUT}")" || return 1
mapfile -t convertSubs <<<"${convertSubsStr}"
if [[ ${convertSubs[*]} != '' ]]; then if [[ ${convertSubs[*]} != '' ]]; then
ffmpegParams+=('${convertSubs[@]}') ffmpegParams+=('${convertSubs[@]}')
fi fi

13
main.sh
View File

@@ -1,12 +1,10 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# set top dir # set top dir
relativeRepoRoot="${BASH_SOURCE[0]//'main.sh'/}" if [[ -z ${REPO_DIR} ]]; then
if [[ -d ${relativeRepoRoot} ]]; then thisFile="${BASH_SOURCE[0]}"
preloadCmd="cd ${relativeRepoRoot} &&" REPO_DIR="$(dirname "${thisFile}")"
fi fi
REPO_DIR="$(${preloadCmd} echo "$PWD")"
unset relativeRepoRoot preloadCmd
IGN_DIR="${REPO_DIR}/gitignore" IGN_DIR="${REPO_DIR}/gitignore"
TMP_DIR="${IGN_DIR}/tmp" TMP_DIR="${IGN_DIR}/tmp"
@@ -37,9 +35,10 @@ src_scripts() {
rm "${SCRIPT_DIR}"/*.sh rm "${SCRIPT_DIR}"/*.sh
# shellcheck disable=SC2016 # shellcheck disable=SC2016
echo '#!/usr/bin/env bash echo '#!/usr/bin/env bash
cd "$(dirname "$(readlink -f $0)")/.."
export FB_RUNNING_AS_SCRIPT=1 export FB_RUNNING_AS_SCRIPT=1
. main.sh || return 1 thisFile="$(readlink -f "$0")"
export REPO_DIR="$(cd "$(dirname "${thisFile}")/.." && echo "$PWD")"
source "${REPO_DIR}/main.sh" || return 1
scr_name="$(bash_basename $0)" scr_name="$(bash_basename $0)"
cmd="${scr_name//.sh/}" cmd="${scr_name//.sh/}"
if [[ $DEBUG == 1 ]]; then set -x; fi if [[ $DEBUG == 1 ]]; then set -x; fi

View File

@@ -1,7 +1,8 @@
#!/usr/bin/env bash #!/usr/bin/env bash
cd "$(dirname "$(readlink -f $0)")/.."
export FB_RUNNING_AS_SCRIPT=1 export FB_RUNNING_AS_SCRIPT=1
. main.sh || return 1 thisFile="$(readlink -f "$0")"
export REPO_DIR="$(cd "$(dirname "${thisFile}")/.." && echo "$PWD")"
source "${REPO_DIR}/main.sh" || return 1
scr_name="$(bash_basename $0)" scr_name="$(bash_basename $0)"
cmd="${scr_name//.sh/}" cmd="${scr_name//.sh/}"
if [[ $DEBUG == 1 ]]; then set -x; fi if [[ $DEBUG == 1 ]]; then set -x; fi