transcode : refactor & place output in the same dir.

This commit is contained in:
Dmitry Voronin 2023-11-27 20:12:35 +03:00
parent 8cfac6ec7c
commit e0614f206c

View file

@ -31,6 +31,7 @@ transcode()
local from="${target##*.}" local from="${target##*.}"
local to="${format}" local to="${format}"
local status="[${count}/${total}] ${target} -> ${target%.*}.${to}" local status="[${count}/${total}] ${target} -> ${target%.*}.${to}"
local output="${target#../}"
# Show status. # Show status.
echo -e "${status}" echo -e "${status}"
@ -47,25 +48,28 @@ transcode()
# Send convert. # Send convert.
case "${from}-${to}" in case "${from}-${to}" in
"gz-xz"|"tgz-txz") "gz-xz"|"tgz-txz")
_transcode_gz-xz "${target}" output="${output%gz}xz"
_transcode_gz-xz "${target}" "${output}"
;; ;;
"xz-gz"|"txz-tgz") "xz-gz"|"txz-tgz")
_transcode_xz-gz "${target}" output="${output%xz}gz"
_transcode_xz-gz "${target}" "${output}"
;; ;;
"-mp3") "-mp3")
_transcode_mp3 "${target}" output="${output%.*}.mp3"
;; _transcode_mp3 "${target}" "${output}"
"-av1")
_transcode_av1 "${target}"
;; ;;
"-flac") "-flac")
_transcode_flac "${target}" output="${output%.*}.flac"
_transcode_flac "${target}" "${output}"
;; ;;
"-mka") "-mka")
_transcode_mka "${target}" output="${output%.*}.mka"
_transcode_mka "${target}" "${output}"
;; ;;
"-mkv") "-mkv")
_transcode_mkv "${target}" output="${output%.*}.mkv"
_transcode_mkv "${target}" "${output}"
;; ;;
*) *)
echo -e "${color_yellow}Conversion ${target##*.}-${to} not supported.${color_default}" echo -e "${color_yellow}Conversion ${target##*.}-${to} not supported.${color_default}"
@ -88,44 +92,56 @@ transcode()
_transcode_gz-xz() _transcode_gz-xz()
{ {
pv "${1}" | gzip -d | xz -9e > "${1%gz}xz" pv "${1}" | gzip -d | xz -9e > "${2}"
} }
_transcode_xz-gz() _transcode_xz-gz()
{ {
pv "${1}" | xz -d | gzip -1 > "${1%xz}gz" pv "${1}" | xz -d | gzip -1 > "${2}"
} }
_transcode_mp3() _transcode_mp3()
{ {
ffmpeg -i "${1}" -c:a libmp3lame -b:a 320k -f mp3 "${1%.*}.mp3" ffmpeg -i "${1}" -c:a libmp3lame -b:a 320k -f mp3 "${2}"
} }
_transcode_flac() _transcode_flac()
{ {
ffmpeg -i "${1}" -c:a flac -f flac "${1%.*}.flac" ffmpeg -i "${1}" -c:a flac -f flac "${2}"
} }
_transcode_mka() _transcode_mka()
{ {
ffmpeg -i "${1}" -c:a libopus -b:a 128k -vn "${1%.*}.mka" ffmpeg -i "${1}" -c:a libopus -b:a 128k -vn "${2}"
} }
_transcode_mkv() _transcode_mkv()
{ {
# Calculate keyframe int. local keyint=$(_ffprobe_keyint "${1}")
local fps=$(ffprobe -v 0 -of csv=p=0 -select_streams v:0 -show_entries stream=r_frame_rate "${1}") local braudio=$(_ffprobe_ba "${1}")
fps="${fps%%/*}"
local keyint=$((fps*5))
# Calculate audio bitrate.
local braudio=$(ffprobe -v error -select_streams a:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 "${1}")
braudio=$((braudio/1024))
[[ ${braudio} -gt 128 ]] && braudio=128 [[ ${braudio} -gt 128 ]] && braudio=128
# Convert. ffmpeg -i "${1}" -c:a libopus -b:a ${braudio}k -c:v libsvtav1 -crf 30 -svtav1-params "fast-decode=1:tune=0" -preset 6 -g ${keyint} -vf "scale=-1:min'(1080,ih)'" "${2}"
ffmpeg -i "${1}" -c:a libopus -b:a ${braudio}k -c:v libsvtav1 -crf 30 -svtav1-params "fast-decode=1:tune=0" -preset 6 -g ${keyint} -vf "scale=-1:min'(1080,ih)'" "${1%.*}.mkv" }
_ffprobe_fps()
{
local fps=$(ffprobe -v 0 -of csv=p=0 -select_streams v:0 -show_entries stream=r_frame_rate "${1}")
fps="${fps%%/*}"
echo "${fps}"
}
_ffprobe_keyint()
{
local fps=$(_ffprobe_fps "${1}")
echo $((fps*5))
}
_ffprobe_ba()
{
local ba=$(ffprobe -v error -select_streams a:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 "${1}")
echo $((ba/1024))
} }
# Export. # Export.
export -f transcode _transcode_gz-xz _transcode_xz-gz _transcode_mp3 _transcode_flac _transcode_mka _transcode_mkv export -f transcode _transcode_gz-xz _transcode_xz-gz _transcode_mp3 _transcode_flac _transcode_mka _transcode_mkv _ffprobe_keyint _ffprobe_ba _ffprobe_fps