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/convert.sh

97 lines
2 KiB
Bash
Raw Normal View History

2023-11-19 03:22:20 +03:00
# Convert between different formats.
2023-11-21 23:32:08 +03:00
# Usage: convert <FORMAT> [FILES]
2023-11-21 14:15:11 +03:00
convert()
{
local IFS=$'\n'
2023-11-21 23:32:08 +03:00
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]"
2023-11-20 18:39:04 +03:00
return 2
2023-11-19 03:22:20 +03:00
fi
2023-11-21 23:32:08 +03:00
# All files by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls --classify | grep -v /$))
total=${#targets[@]}
2023-11-19 03:22:20 +03:00
fi
# Process.
2023-11-21 23:32:08 +03:00
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.
2023-11-22 11:07:29 +03:00
echo -e "${status}"
2023-11-21 23:32:08 +03:00
# 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}"
false
;;
esac
# Increment failed on error.
if [[ $? != 0 ]]; then
_convert_error "${target}"
((failed++))
fi
done
# Report result.
if [[ ${count} -gt 1 ]] || [[ "${*}" = "" ]]; then
if [[ ${failed} = 0 ]]; then
echo -e "${color_green}All successful.${color_default}"
else
echo -e "${color_bred}Items failed to convert: ${failed}.${color_default}"
fi
2023-11-19 03:22:20 +03:00
fi
}
2023-11-21 23:32:08 +03:00
_convert_error()
{
echo -e "${color_bred}${1}: failed.${color_default}"
}
2023-11-21 14:15:11 +03:00
_convert_gz-xz()
{
2023-11-21 23:32:08 +03:00
pv "${1}" | gzip -d | xz -9e > "${1%.gz}.xz"
2023-11-19 03:22:20 +03:00
}
2023-11-21 14:15:11 +03:00
_convert_xz-gz()
{
pv "${1}" | xz -d | gzip -1 > "${1%.xz}.gz"
2023-11-21 23:32:08 +03:00
}
_convert_mp3()
{
ffmpeg -i "${1}" -c:a libmp3lame -b:a 320k -f mp3 "${1%.*}.mp3"
2023-11-19 03:22:20 +03:00
}
# Export.
export -f convert _convert_gz-xz _convert_xz-gz