This repository has been archived on 2024-03-04. You can view files and clone it, but cannot push or open issues or pull requests.
linux/.config/bash/module/Transcode.sh

97 lines
2.5 KiB
Bash
Raw Normal View History

# Convert between different formats.
2023-12-07 04:02:47 +03:00
# By default tries to convert all files.
# Usage: transcode <FORMAT> [FILES]
2023-12-07 01:44:42 +03:00
function transcode() {
2023-12-05 21:50:45 +03:00
local IFS=$'\n'
2023-12-17 20:44:54 +03:00
local format=${1}
local targets=(${@:2})
[[ "${targets}" = "" ]] && targets=($(_ls_file))
2023-12-05 21:50:45 +03:00
# Report no format.
2023-12-17 20:44:54 +03:00
if [[ "${format}" = "" ]] || [[ "${format}" =~ "." ]]; then
_error "No format specified."
2023-12-07 04:02:47 +03:00
help transcode
2023-12-05 21:50:45 +03:00
return 2
fi
2023-12-17 20:44:54 +03:00
process() {
2023-12-05 21:50:45 +03:00
# Define context names and status.
local from="${target##*.}"
local to="${format}"
local output="${target##*/}"
output="${output%.*}.${to}"
# Skip if file exists.
2024-01-03 18:50:59 +03:00
[[ -f "${output}" ]] && { _iterate_skip "Already exists."; return 0; }
2023-12-05 21:50:45 +03:00
# Support multiple inputs.
[[ "${to}" = "mp3" ]] && from=""
[[ "${to}" = "flac" ]] && from=""
[[ "${to}" = "mka" ]] && from=""
[[ "${to}" = "mkv" ]] && from=""
# Send convert.
case "${from}-${to}" in
"gz-xz"|"tgz-txz")
_transcode_gz-xz "${target}" "${output}"
;;
"xz-gz"|"txz-tgz")
_transcode_xz-gz "${target}" "${output}"
;;
"-mp3")
_transcode_mp3 "${target}" "${output}"
;;
"-flac")
_transcode_flac "${target}" "${output}"
;;
"-mka")
_transcode_mka "${target}" "${output}"
;;
"-mkv")
_transcode_mkv "${target}" "${output}"
;;
*)
2023-12-17 20:44:54 +03:00
_error "Conversion ${target##*.}-${to} not supported."
return 1
2023-12-05 21:50:45 +03:00
;;
esac
2023-12-17 20:44:54 +03:00
}
2023-12-05 21:50:45 +03:00
2023-12-17 20:44:54 +03:00
_iterate_targets process ${targets[@]}
}
2023-12-07 01:44:42 +03:00
function _transcode_gz-xz() {
2023-12-05 21:50:45 +03:00
[[ -f "${2}" ]] && return 1
pv "${1}" | gzip -d | xz -9e > "${2}"
}
2023-12-07 01:44:42 +03:00
function _transcode_xz-gz() {
2023-12-05 21:50:45 +03:00
[[ -f "${2}" ]] && return 1
pv "${1}" | xz -d | gzip -1 > "${2}"
}
2023-12-07 01:44:42 +03:00
function _transcode_mp3() {
2023-12-05 21:50:45 +03:00
ffmpeg -n -i "${1}" -c:a libmp3lame -b:a 320k -f mp3 "${2}"
}
2023-12-07 01:44:42 +03:00
function _transcode_flac() {
2023-12-05 21:50:45 +03:00
ffmpeg -n -i "${1}" -c:a flac -f flac "${2}"
}
2023-12-07 01:44:42 +03:00
function _transcode_mka() {
2023-12-05 21:50:45 +03:00
local braudio=$(_ffprobe_ba "${1}")
[[ ${braudio} -gt 128 ]] && braudio=128
2023-12-05 21:50:45 +03:00
ffmpeg -n -i "${1}" -ac 2 -c:a libopus -b:a ${braudio}k -vn "${2}"
}
2023-12-07 01:44:42 +03:00
function _transcode_mkv() {
2023-12-05 21:50:45 +03:00
local keyint=$(_ffprobe_keyint "${1}")
local braudio=$(_ffprobe_ba "${1}")
[[ ${braudio} -gt 128 ]] && braudio=128
[[ ${braudio} -lt 32 ]] && braudio=32
2023-12-05 21:50:45 +03:00
# ffmpeg -n -i "${1}" -c:a libopus -b:a ${braudio}k -c:v libsvtav1 -crf 30 -svtav1-params "fast-decode=1:tune=0" -preset 8 -pix_fmt yuv420p10le -g ${keyint} -vf "scale=-2:min'(1080,ih)'" "${2}"
ffmpeg -n -i "${1}" -map 0 -map -v -map V -map -t -c:s srt -ac 2 -c:a libopus -b:a ${braudio}k -c:v libsvtav1 -crf 30 -svtav1-params "tune=0" -pix_fmt yuv420p10le -g ${keyint} -vf "scale=-2:min'(1080,ih)'" "${2}"
}