diff --git a/lib/common.sh b/lib/common.sh index b74dff3..73aeed3 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -142,6 +142,34 @@ bash_basename() { 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() { local line="$1" local substr="$2" diff --git a/lib/encode.sh b/lib/encode.sh index 23758d8..47f076f 100644 --- a/lib/encode.sh +++ b/lib/encode.sh @@ -11,7 +11,8 @@ get_duration() { get_crop() { local file="$1" - local duration="$(get_duration "${file}")" + local duration + duration="$(get_duration "${file}")" || return 1 # don't care about decimal points IFS='.' read -r duration _ <<<"${duration}" # get crop value for first half of input @@ -48,11 +49,12 @@ get_stream_codec() { get_file_format() { local file="$1" - local probe="$(ffprobe \ + local probe + probe="$(ffprobe \ -v error \ -show_entries format=format_name \ -of default=noprint_wrappers=1:nokey=1 \ - "${file}")" + "${file}")" || return 1 if line_contains "${probe}" 'matroska'; then echo mkv else @@ -88,14 +90,16 @@ get_num_audio_channels() { -show_entries stream=channels \ -of default=noprint_wrappers=1:nokey=1 \ "${file}" - } unmap_streams() { local file="$1" local unmapFilter='bin_data|jpeg|png' 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 unmap+=("-map" "-0:${stream}") fi @@ -107,9 +111,10 @@ set_audio_bitrate() { local file="$1" local bitrate=() 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 codec="$(get_stream_codec "${file}" "a:${stream}")" + codec="$(get_stream_codec "${file}" "a:${stream}")" || return 1 if [[ ${codec} == 'opus' ]]; then bitrate+=( "-c:a:${stream}" @@ -305,7 +310,8 @@ set_encode_opts() { # use same container for output if [[ $SAME_CONTAINER == "true" ]]; then - local fileFormat="$(get_file_format "${INPUT}")" + local fileFormat + fileFormat="$(get_file_format "${INPUT}")" || return 1 FILE_EXT='' if [[ ${fileFormat} == 'MPEG-4' ]]; then FILE_EXT='mp4' @@ -351,7 +357,7 @@ gen_encode_script() { ) local crop='' if [[ $CROP == "true" ]]; then - crop="-vf $(get_crop "${INPUT}")" + crop="$(get_crop "${INPUT}")" || return 1 fi local videoEncoder='libsvtav1' local ffmpegVersion="$(ffmpeg_version)" @@ -396,24 +402,30 @@ gen_encode_script() { '-y' '-map' '0' ) - local unmap=($(unmap_streams "${INPUT}")) + local unmapStr + unmapStr="$(unmap_streams "${INPUT}")" || return 1 + mapfile -t unmap <<<"${unmapStr}" if [[ ${unmap[*]} != '' ]]; then ffmpegParams+=('${unmap[@]}') fi - local audioBitrate=($(set_audio_bitrate "${INPUT}")) + local audioBitrateStr + audioBitrateStr="$(set_audio_bitrate "${INPUT}")" || return 1 + mapfile -t audioBitrate <<<"${audioBitrateStr}" if [[ ${audioBitrate[*]} != '' ]]; then ffmpegParams+=('${audioBitrate[@]}') fi if [[ ${crop} != '' ]]; then - ffmpegParams+=('${crop}') + ffmpegParams+=('-vf' '${crop}') fi ffmpegParams+=( '-c:s' 'copy' ) - local convertSubs=($(convert_subs "${INPUT}")) + local convertSubsStr + convertSubsStr="$(convert_subs "${INPUT}")" || return 1 + mapfile -t convertSubs <<<"${convertSubsStr}" if [[ ${convertSubs[*]} != '' ]]; then ffmpegParams+=('${convertSubs[@]}') fi diff --git a/main.sh b/main.sh index 5a867a7..ab716a3 100755 --- a/main.sh +++ b/main.sh @@ -1,12 +1,10 @@ #!/usr/bin/env bash # set top dir -relativeRepoRoot="${BASH_SOURCE[0]//'main.sh'/}" -if [[ -d ${relativeRepoRoot} ]]; then - preloadCmd="cd ${relativeRepoRoot} &&" +if [[ -z ${REPO_DIR} ]]; then + thisFile="${BASH_SOURCE[0]}" + REPO_DIR="$(dirname "${thisFile}")" fi -REPO_DIR="$(${preloadCmd} echo "$PWD")" -unset relativeRepoRoot preloadCmd IGN_DIR="${REPO_DIR}/gitignore" TMP_DIR="${IGN_DIR}/tmp" @@ -37,9 +35,10 @@ src_scripts() { rm "${SCRIPT_DIR}"/*.sh # shellcheck disable=SC2016 echo '#!/usr/bin/env bash -cd "$(dirname "$(readlink -f $0)")/.." 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)" cmd="${scr_name//.sh/}" if [[ $DEBUG == 1 ]]; then set -x; fi diff --git a/scripts/entry.sh b/scripts/entry.sh index 283cc93..4039c36 100755 --- a/scripts/entry.sh +++ b/scripts/entry.sh @@ -1,7 +1,8 @@ #!/usr/bin/env bash -cd "$(dirname "$(readlink -f $0)")/.." 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)" cmd="${scr_name//.sh/}" if [[ $DEBUG == 1 ]]; then set -x; fi