127 lines
3.1 KiB
Bash
127 lines
3.1 KiB
Bash
# Convert between different formats.
|
|
# By default tries to convert all files.
|
|
# Usage: transcode <FORMAT> [FILES]
|
|
function transcode() {
|
|
local IFS=$'\n'
|
|
local targets=("${@:2}")
|
|
local format="${1}"
|
|
local total=${#targets[@]}
|
|
local count=0
|
|
local failed=0
|
|
local skipped=0
|
|
|
|
# Report no format.
|
|
if [[ "${format}" = "" ]]; then
|
|
echo -e "${color_bred}No format specified.${color_default}"
|
|
help transcode
|
|
return 2
|
|
fi
|
|
|
|
# All files by default.
|
|
if [[ "${targets}" = "" ]]; then
|
|
targets=($(ls --classify | grep -v /$))
|
|
total=${#targets[@]}
|
|
fi
|
|
|
|
# Process.
|
|
for target in "${targets[@]}"; do
|
|
# Increment counter.
|
|
((count++))
|
|
|
|
# Define context names and status.
|
|
local from="${target##*.}"
|
|
local to="${format}"
|
|
local output="${target##*/}"
|
|
output="${output%.*}.${to}"
|
|
local status="[${count}/${total}] ${target} -> ${output}"
|
|
|
|
# Show status.
|
|
echo -e "${status}"
|
|
|
|
# Skip if file exists.
|
|
[[ -f "${output}" ]] && { ((skipped++)); continue; }
|
|
|
|
# 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}"
|
|
;;
|
|
*)
|
|
echo -e "${color_yellow}Conversion ${target##*.}-${to} not supported.${color_default}"
|
|
false
|
|
;;
|
|
esac
|
|
|
|
# Show error.
|
|
if [[ ${?} != 0 ]]; then
|
|
echo -e "${color_bred}${status}: Failed.${color_default}"
|
|
((failed++))
|
|
fi
|
|
done
|
|
|
|
if [[ ${skipped} != 0 ]]; then
|
|
echo -e "${color_byellow}Skipped: ${skipped}.${color_default}"
|
|
return 1
|
|
fi
|
|
|
|
if [[ ${failed} != 0 ]]; then
|
|
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
|
false
|
|
fi
|
|
}
|
|
|
|
function _transcode_gz-xz() {
|
|
[[ -f "${2}" ]] && return 1
|
|
pv "${1}" | gzip -d | xz -9e > "${2}"
|
|
}
|
|
|
|
function _transcode_xz-gz() {
|
|
[[ -f "${2}" ]] && return 1
|
|
pv "${1}" | xz -d | gzip -1 > "${2}"
|
|
}
|
|
|
|
function _transcode_mp3() {
|
|
ffmpeg -n -i "${1}" -c:a libmp3lame -b:a 320k -f mp3 "${2}"
|
|
}
|
|
|
|
function _transcode_flac() {
|
|
ffmpeg -n -i "${1}" -c:a flac -f flac "${2}"
|
|
}
|
|
|
|
function _transcode_mka() {
|
|
local braudio=$(_ffprobe_ba "${1}")
|
|
[[ ${braudio} -gt 128 ]] && braudio=128
|
|
|
|
ffmpeg -n -i "${1}" -ac 2 -c:a libopus -b:a ${braudio}k -vn "${2}"
|
|
}
|
|
|
|
function _transcode_mkv() {
|
|
local keyint=$(_ffprobe_keyint "${1}")
|
|
local braudio=$(_ffprobe_ba "${1}")
|
|
[[ ${braudio} -gt 128 ]] && braudio=128
|
|
|
|
# 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}"
|
|
}
|