# Convert between different formats. # Usage: convert [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 [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