transcode : add support for flac, mka and mkv.

This commit is contained in:
Dmitry Voronin 2023-11-27 15:24:04 +03:00
parent b184ca5cc1
commit 1f79575eb6
3 changed files with 134 additions and 94 deletions

View file

@ -408,9 +408,6 @@ Please note that colors depend on Terminal Emulator and may vary based on its se
|yellow|`\033[0;33m`| |yellow|`\033[0;33m`|
|yellow (bold)|`\033[1;33m`| |yellow (bold)|`\033[1;33m`|
## Convert.
`convert <FORMAT> [FILES]`|Convert between different formats.
## Copy/paste. ## Copy/paste.
Command|Description Command|Description
@ -660,6 +657,9 @@ Command|Description
`tb_rpmfusion [NAME]`|Install RPMFusion package to box by name. Default is `main`. `tb_rpmfusion [NAME]`|Install RPMFusion package to box by name. Default is `main`.
`tbl`|List all boxes. `tbl`|List all boxes.
## Transcode.
`transcode <FORMAT> [FILES]`|Convert between different formats.
## Try. ## Try.
Command|Description Command|Description

View file

@ -1,91 +0,0 @@
# Convert between different formats.
# Usage: convert <FORMAT> [FILES]
convert()
{
local IFS=$'\n'
local targets=("${@:2}")
local format="${1}"
local total=${#targets[@]}
local count=0
local failed=0
# Report no format.
if [[ "${format}" = "" ]]; then
echo -e "${color_bred}No format specified.${color_default}"
echo "Usage: convert <FORMAT> [FILES]"
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 status="[${count}/${total}] ${target} -> ${target%.*}.${to}"
# Show status.
echo -e "${status}"
# Skip same format.
[[ "${from}" = "${to}" ]] && continue
# Support multiple inputs.
[[ "${to}" = "mp3" ]] && from=""
# Send convert.
case "${from}-${to}" in
"gz-xz"|"tgz-txz")
_convert_gz-xz "${target}"
;;
"xz-gz"|"txz-tgz")
_convert_xz-gz "${target}"
;;
"-mp3")
_convert_mp3 "${target}"
;;
*)
echo -e "${color_yellow}Conversion ${from}-${to} not supported.${color_default}"
((failed++))
false
;;
esac
# Show error.
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${target}: Failed.${color_default}"
((failed++))
fi
done
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
}
_convert_gz-xz()
{
pv "${1}" | gzip -d | xz -9e > "${1%gz}xz"
}
_convert_xz-gz()
{
pv "${1}" | xz -d | gzip -1 > "${1%xz}gz"
}
_convert_mp3()
{
ffmpeg -i "${1}" -c:a libmp3lame -b:a 320k -f mp3 "${1%.*}.mp3"
}
# Export.
export -f convert _convert_gz-xz _convert_xz-gz _convert_mp3

View file

@ -0,0 +1,131 @@
# Convert between different formats.
# Usage: transcode <FORMAT> [FILES]
transcode()
{
local IFS=$'\n'
local targets=("${@:2}")
local format="${1}"
local total=${#targets[@]}
local count=0
local failed=0
# Report no format.
if [[ "${format}" = "" ]]; then
echo -e "${color_bred}No format specified.${color_default}"
echo "Usage: transcode <FORMAT> [FILES]"
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 status="[${count}/${total}] ${target} -> ${target%.*}.${to}"
# Show status.
echo -e "${status}"
# Skip same format.
[[ "${from}" = "${to}" ]] && 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}"
;;
"xz-gz"|"txz-tgz")
_transcode_xz-gz "${target}"
;;
"-mp3")
_transcode_mp3 "${target}"
;;
"-av1")
_transcode_av1 "${target}"
;;
"-flac")
_transcode_flac "${target}"
;;
"-mka")
_transcode_mka "${target}"
;;
"-mkv")
_transcode_mkv "${target}"
;;
*)
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 [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
}
_transcode_gz-xz()
{
pv "${1}" | gzip -d | xz -9e > "${1%gz}xz"
}
_transcode_xz-gz()
{
pv "${1}" | xz -d | gzip -1 > "${1%xz}gz"
}
_transcode_mp3()
{
ffmpeg -i "${1}" -c:a libmp3lame -b:a 320k -f mp3 "${1%.*}.mp3"
}
_transcode_flac()
{
ffmpeg -i "${1}" -c:a flac -f flac "${1%.*}.flac"
}
_transcode_mka()
{
ffmpeg -i "${1}" -c:a libopus -b:a 128k -vn "${1%.*}.mka"
}
_transcode_mkv()
{
# Calculate keyframe int.
local fps=$(ffprobe -v 0 -of csv=p=0 -select_streams v:0 -show_entries stream=r_frame_rate "${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
# Convert.
ffmpeg -i "${1}" -c:a libopus -b:a ${braudio}k -c:v libsvtav1 -crf 35 -svtav1-params 'fast-decode=1:tune=0' -preset 8 -g ${keyint} -vf 'scale=-1:min(1080,ih)' "${1%.*}.mkv"
}
# Export.
export -f transcode _transcode_gz-xz _transcode_xz-gz _transcode_mp3 _transcode_flac _transcode_mka _transcode_mkv