From 5b0a8647844b85ea93e2cb1cbba3e419d1f7dba7 Mon Sep 17 00:00:00 2001 From: home Date: Tue, 21 Nov 2023 23:32:08 +0300 Subject: [PATCH] convert : add mp3 support. --- .README.md | 4 +- .config/bash/module/archive.sh | 18 +++--- .config/bash/module/convert.sh | 101 +++++++++++++++++++++++---------- .config/bash/module/name.sh | 14 ++--- .config/bash/module/pack.sh | 23 ++++---- 5 files changed, 100 insertions(+), 60 deletions(-) diff --git a/.README.md b/.README.md index 72061ed..9f0bf92 100644 --- a/.README.md +++ b/.README.md @@ -402,7 +402,7 @@ Please note that colors depend on Terminal Emulator and may vary based on its se |yellow (bold)|`\033[1;33m`| ## Convert. -`convert `|Convert between different formats. +`convert [FILES]`|Convert between different formats. ## Copy/paste. @@ -569,7 +569,7 @@ Command|Description Command|Description ---|--- -`pack `|Create desired file format from other files. +`pack [FILES]`|Create desired file format from other files. `unpack [FILES]`|Attempt to extract file content. ## Permissions. diff --git a/.config/bash/module/archive.sh b/.config/bash/module/archive.sh index e71e235..038c237 100644 --- a/.config/bash/module/archive.sh +++ b/.config/bash/module/archive.sh @@ -15,7 +15,7 @@ archive() # set dafult value to target all supported archives. if [[ "${targets}" = "" ]]; then - targets=(*) + targets=($(ls)) total=${#targets[@]} fi @@ -26,7 +26,7 @@ archive() # status info. local status="[${count}/${total}] ${target}" - echo "${status}" + echo -e "${color_bblue}${status}${color_default}" # create archive. tar -c "${target}" | pv -s $(du -sb "${target}" | awk '{print $1}') | xz -9e > "${target%/*}".txz @@ -35,7 +35,7 @@ archive() mv "${target%/*}".txz "${target%/*}"_${date}-$(pv "${target%/*}".txz | sha1sum | cut -d\ -f1).txz # report success. - echo -e "${color_default}${status}: Done.${color_default}" + echo -e "${color_green}${status}: Done.${color_default}" done } @@ -61,7 +61,7 @@ archive_fast() # status info. local status="[${count}/${total}] ${target}" - echo "${status}" + echo -e "${color_bblue}${status}${color_default}" # create archive. tar -c "${target}" | pv -s $(du -sb "${target}" | awk '{print $1}') | gzip -1 > "${target%/*}".tgz @@ -70,7 +70,7 @@ archive_fast() mv "${target%/*}".tgz "${target%/*}"_${date}-$(pv "${target%/*}".tgz | sha1sum | cut -d\ -f1).tgz # report success. - echo -e "${color_default}${status}: Done.${color_default}" + echo -e "${color_green}${status}: Done.${color_default}" done } @@ -109,7 +109,7 @@ archive_check() # compare hashes, show error on mismatch. if [[ "${actual}" = "${saved}" ]]; then - echo -e "${color_default}${status}: Validation OK.${color_default}" + echo -e "${color_bblue}${status}: Validation OK.${color_default}" else echo -e "${color_bred}${status}: Validation failed.${color_default}" ((failed++)) @@ -155,7 +155,7 @@ unarchive() # extract if hash matched or show error if not. if [[ "${saved}" = "${actual}" ]]; then - echo "${status}: Validation OK." + echo -e "${color_bblue}${status}: Validation OK.${color_default}" # figure out the compression tool. local compressor @@ -177,7 +177,7 @@ unarchive() fi # report extraction complete. - echo -e "${color_default}${status}: Done.${color_default}" + echo -e "${color_green}${status}: Done.${color_default}" done } @@ -230,7 +230,7 @@ archive_name() fi # rename. - mv -- "${target}" "${new_name}" && echo -e "${color_default}${status}${color_default}" || echo -e "${color_bred}${status}: Error.${color_default}" + mv -- "${target}" "${new_name}" && echo -e "${color_bblue}${status}${color_default}" || echo -e "${color_bred}${status}: Error.${color_default}" done } diff --git a/.config/bash/module/convert.sh b/.config/bash/module/convert.sh index e0da686..17ea629 100644 --- a/.config/bash/module/convert.sh +++ b/.config/bash/module/convert.sh @@ -1,53 +1,94 @@ # Convert between different formats. -# Usage: convert +# Usage: convert [FILES] convert() { - local input="${1}" - local output="${2}" - local from="${input##*.}" - local to="${output##*.}" + local targets=("${@:2}") + local format="${1}" + local total=${#targets[@]} + local count=0 + local failed=0 - # Report no output. - if [[ "${to}" = "" ]]; then - echo -e "${color_bred}no output file/format.${color_default}" + # Report no format. + if [[ "${format}" = "" ]]; then + echo -e "${color_bred}No format specified.${color_default}" + echo "Usage: convert [FILES]" return 2 fi - # Report no input. - if [[ "${from}" = "" ]]; then - echo -e "${color_bred}no input file/format.${color_default}" - return 2 + # All files by default. + if [[ "${targets}" = "" ]]; then + targets=($(ls --classify | grep -v /$)) + total=${#targets[@]} fi # Process. - case "${from}-${to}" in - "gz-xz"|"tgz-txz") - _convert_gz-xz "${input}" "${output}" - ;; - "xz-gz"|"txz-tgz") - _convert_xz-gz "${input}" "${output}" - ;; - *) - echo -e "${color_yellow}Conversion ${from}-${to} not supported.${color_default}" - return 1 - ;; - esac + for target in "${targets[@]}"; do + # Increment counter. + ((count++)) - if [[ $? = 0 ]]; then - echo -e "${color_green}Done.${color_default}" - else - echo -e "${color_bred}There were errors.${color_default}" + # Define context names and status. + local from="${target##*.}" + local to="${format}" + local status="[${count}/${total}] ${target} -> ${target%.*}.${to}" + + # Show status. + echo -e "${color_bblue}${status}${color_default}" + + # 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 fi } +_convert_error() +{ + echo -e "${color_bred}${1}: failed.${color_default}" +} + _convert_gz-xz() { - pv "${1}" | gzip -d | xz -9e > "${2}" + pv "${1}" | gzip -d | xz -9e > "${1%.gz}.xz" } _convert_xz-gz() { - pv "${1}" | xz -d | gzip -1 > "${2}" + 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. diff --git a/.config/bash/module/name.sh b/.config/bash/module/name.sh index 8769048..f784e22 100644 --- a/.config/bash/module/name.sh +++ b/.config/bash/module/name.sh @@ -52,7 +52,7 @@ name() mv -- "${target}" "${new_name}" &> /dev/null # show change. - echo -e "${color_default}${status}${color_default}" + echo -e "${color_bblue}${status}${color_default}" done } @@ -102,7 +102,7 @@ name_hash() mv -- "${target}" "${new_name}" &> /dev/null # show change. - echo -e "${color_default}${status}${color_default}" + echo -e "${color_bblue}${status}${color_default}" else # Increment count. ((count++)) @@ -144,7 +144,7 @@ name_hash_check() # compare hashes. if [[ "${stored}" = "${actual}" ]]; then - echo -e "${color_default}${status}: Validation OK.${color_default}" + echo -e "${color_bblue}${status}: Validation OK.${color_default}" else echo -e "${color_bred}${status}: Validation failed.${color_default}" ((failed++)) @@ -214,7 +214,7 @@ name_series() mv -- "${target}" "${new_name}" &> /dev/null # Report status. - echo -e "${color_default}${status}${color_default}" + echo -e "${color_bblue}${status}${color_default}" else # Increment count. ((count++)) @@ -272,7 +272,7 @@ name_manga() mv -- "${target}" "${new_name}" &> /dev/null # Show status. - echo -e "${color_default}${status}${color_default}" + echo -e "${color_bblue}${status}${color_default}" else # Increment count. ((count++)) @@ -327,7 +327,7 @@ name_ext() mv -- "${target}" "${new_name}" &> /dev/null # show change. - echo -e "${color_default}${status}${color_default}" + echo -e "${color_bblue}${status}${color_default}" else # Increment count. ((count++)) @@ -375,7 +375,7 @@ name_prefix() mv -- "${target}" "${new_name}" &> /dev/null # Show change. - echo -e "${color_default}${status}${color_default}" + echo -e "${color_bblue}${status}${color_default}" done } diff --git a/.config/bash/module/pack.sh b/.config/bash/module/pack.sh index e25e5be..19e4861 100644 --- a/.config/bash/module/pack.sh +++ b/.config/bash/module/pack.sh @@ -1,7 +1,7 @@ _UNPACK_SUPPORTED=".tar$|.tgz$|.txz$|.tar.gz$|.tar.xz$|.zip$|.iso$|.rar$" # Pack files into desired format. -# Usage: pack +# Usage: pack [FILES] pack() { local output="${1}" @@ -11,21 +11,20 @@ pack() # report no output. if [[ "${output}" = "" ]]; then - echo "Usage: pack " + echo "Usage: pack [FILES]" return 2 fi # report no format. if [[ "${format}" = "" ]]; then echo "Could not determine output format." - echo "Usage: pack " + echo "Usage: pack [FILES]" return 2 fi - # report no targets. + # All targets by default. if [[ "${targets}" = "" ]]; then - echo "Usage: pack " - return 2 + targets=($(ls)) fi # process. @@ -58,7 +57,7 @@ pack() } # attempt to unpack everything. -# usage: unpack +# usage: unpack [FILES] unpack() { local targets=("${@}") @@ -66,7 +65,7 @@ unpack() local failed=0 local total=${#targets[@]} - # show error if no target specified. + # All targets by default. if [[ "${targets}" = "" ]]; then targets=($(ls | grep -E ${_UNPACK_SUPPORTED})) total=${#targets[@]} @@ -81,7 +80,7 @@ unpack() local status="[${count}/${total}] ${target}" # show status. - echo -e "${status}" + echo -e "${color_bblue}${status}${color_default}" # unpack file type. local type="${target##*.}" @@ -133,10 +132,10 @@ unpack() # print report. if [[ ${count} -gt 1 ]] || [[ "${*}" = "" ]]; then - if [[ ${failed} -gt 0 ]]; then - echo -e "${color_bred}Items failed to unpack: ${failed}.${color_default}" - else + if [[ ${failed} = 0 ]]; then echo -e "${color_green}All successful.${color_default}" + else + echo -e "${color_bred}Items failed to unpack: ${failed}.${color_default}" fi fi }