convert : add mp3 support.

This commit is contained in:
Dmitry Voronin 2023-11-21 23:32:08 +03:00
parent 47d52cd635
commit 5b0a864784
5 changed files with 100 additions and 60 deletions

View file

@ -402,7 +402,7 @@ Please note that colors depend on Terminal Emulator and may vary based on its se
|yellow (bold)|`\033[1;33m`| |yellow (bold)|`\033[1;33m`|
## Convert. ## Convert.
`convert <FROM.gz> <TO.xz>`|Convert between different formats. `convert <FORMAT> [FILES]`|Convert between different formats.
## Copy/paste. ## Copy/paste.
@ -569,7 +569,7 @@ Command|Description
Command|Description Command|Description
---|--- ---|---
`pack <TARGET.ext> <FILES>`|Create desired file format from other files. `pack <TARGET.ext> [FILES]`|Create desired file format from other files.
`unpack [FILES]`|Attempt to extract file content. `unpack [FILES]`|Attempt to extract file content.
## Permissions. ## Permissions.

View file

@ -15,7 +15,7 @@ archive()
# set dafult value to target all supported archives. # set dafult value to target all supported archives.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
targets=(*) targets=($(ls))
total=${#targets[@]} total=${#targets[@]}
fi fi
@ -26,7 +26,7 @@ archive()
# status info. # status info.
local status="[${count}/${total}] ${target}" local status="[${count}/${total}] ${target}"
echo "${status}" echo -e "${color_bblue}${status}${color_default}"
# create archive. # create archive.
tar -c "${target}" | pv -s $(du -sb "${target}" | awk '{print $1}') | xz -9e > "${target%/*}".txz 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 mv "${target%/*}".txz "${target%/*}"_${date}-$(pv "${target%/*}".txz | sha1sum | cut -d\ -f1).txz
# report success. # report success.
echo -e "${color_default}${status}: Done.${color_default}" echo -e "${color_green}${status}: Done.${color_default}"
done done
} }
@ -61,7 +61,7 @@ archive_fast()
# status info. # status info.
local status="[${count}/${total}] ${target}" local status="[${count}/${total}] ${target}"
echo "${status}" echo -e "${color_bblue}${status}${color_default}"
# create archive. # create archive.
tar -c "${target}" | pv -s $(du -sb "${target}" | awk '{print $1}') | gzip -1 > "${target%/*}".tgz 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 mv "${target%/*}".tgz "${target%/*}"_${date}-$(pv "${target%/*}".tgz | sha1sum | cut -d\ -f1).tgz
# report success. # report success.
echo -e "${color_default}${status}: Done.${color_default}" echo -e "${color_green}${status}: Done.${color_default}"
done done
} }
@ -109,7 +109,7 @@ archive_check()
# compare hashes, show error on mismatch. # compare hashes, show error on mismatch.
if [[ "${actual}" = "${saved}" ]]; then if [[ "${actual}" = "${saved}" ]]; then
echo -e "${color_default}${status}: Validation OK.${color_default}" echo -e "${color_bblue}${status}: Validation OK.${color_default}"
else else
echo -e "${color_bred}${status}: Validation failed.${color_default}" echo -e "${color_bred}${status}: Validation failed.${color_default}"
((failed++)) ((failed++))
@ -155,7 +155,7 @@ unarchive()
# extract if hash matched or show error if not. # extract if hash matched or show error if not.
if [[ "${saved}" = "${actual}" ]]; then if [[ "${saved}" = "${actual}" ]]; then
echo "${status}: Validation OK." echo -e "${color_bblue}${status}: Validation OK.${color_default}"
# figure out the compression tool. # figure out the compression tool.
local compressor local compressor
@ -177,7 +177,7 @@ unarchive()
fi fi
# report extraction complete. # report extraction complete.
echo -e "${color_default}${status}: Done.${color_default}" echo -e "${color_green}${status}: Done.${color_default}"
done done
} }
@ -230,7 +230,7 @@ archive_name()
fi fi
# rename. # 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 done
} }

View file

@ -1,53 +1,94 @@
# Convert between different formats. # Convert between different formats.
# Usage: convert <FROM.gz> <TO.xz> # Usage: convert <FORMAT> [FILES]
convert() convert()
{ {
local input="${1}" local targets=("${@:2}")
local output="${2}" local format="${1}"
local from="${input##*.}" local total=${#targets[@]}
local to="${output##*.}" local count=0
local failed=0
# Report no output. # Report no format.
if [[ "${to}" = "" ]]; then if [[ "${format}" = "" ]]; then
echo -e "${color_bred}no output file/format.${color_default}" echo -e "${color_bred}No format specified.${color_default}"
echo "Usage: convert <FORMAT> [FILES]"
return 2 return 2
fi fi
# Report no input. # All files by default.
if [[ "${from}" = "" ]]; then if [[ "${targets}" = "" ]]; then
echo -e "${color_bred}no input file/format.${color_default}" targets=($(ls --classify | grep -v /$))
return 2 total=${#targets[@]}
fi fi
# Process. # Process.
case "${from}-${to}" in for target in "${targets[@]}"; do
"gz-xz"|"tgz-txz") # Increment counter.
_convert_gz-xz "${input}" "${output}" ((count++))
;;
"xz-gz"|"txz-tgz")
_convert_xz-gz "${input}" "${output}"
;;
*)
echo -e "${color_yellow}Conversion ${from}-${to} not supported.${color_default}"
return 1
;;
esac
if [[ $? = 0 ]]; then # Define context names and status.
echo -e "${color_green}Done.${color_default}" local from="${target##*.}"
else local to="${format}"
echo -e "${color_bred}There were errors.${color_default}" 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 fi
} }
_convert_error()
{
echo -e "${color_bred}${1}: failed.${color_default}"
}
_convert_gz-xz() _convert_gz-xz()
{ {
pv "${1}" | gzip -d | xz -9e > "${2}" pv "${1}" | gzip -d | xz -9e > "${1%.gz}.xz"
} }
_convert_xz-gz() _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. # Export.

View file

@ -52,7 +52,7 @@ name()
mv -- "${target}" "${new_name}" &> /dev/null mv -- "${target}" "${new_name}" &> /dev/null
# show change. # show change.
echo -e "${color_default}${status}${color_default}" echo -e "${color_bblue}${status}${color_default}"
done done
} }
@ -102,7 +102,7 @@ name_hash()
mv -- "${target}" "${new_name}" &> /dev/null mv -- "${target}" "${new_name}" &> /dev/null
# show change. # show change.
echo -e "${color_default}${status}${color_default}" echo -e "${color_bblue}${status}${color_default}"
else else
# Increment count. # Increment count.
((count++)) ((count++))
@ -144,7 +144,7 @@ name_hash_check()
# compare hashes. # compare hashes.
if [[ "${stored}" = "${actual}" ]]; then if [[ "${stored}" = "${actual}" ]]; then
echo -e "${color_default}${status}: Validation OK.${color_default}" echo -e "${color_bblue}${status}: Validation OK.${color_default}"
else else
echo -e "${color_bred}${status}: Validation failed.${color_default}" echo -e "${color_bred}${status}: Validation failed.${color_default}"
((failed++)) ((failed++))
@ -214,7 +214,7 @@ name_series()
mv -- "${target}" "${new_name}" &> /dev/null mv -- "${target}" "${new_name}" &> /dev/null
# Report status. # Report status.
echo -e "${color_default}${status}${color_default}" echo -e "${color_bblue}${status}${color_default}"
else else
# Increment count. # Increment count.
((count++)) ((count++))
@ -272,7 +272,7 @@ name_manga()
mv -- "${target}" "${new_name}" &> /dev/null mv -- "${target}" "${new_name}" &> /dev/null
# Show status. # Show status.
echo -e "${color_default}${status}${color_default}" echo -e "${color_bblue}${status}${color_default}"
else else
# Increment count. # Increment count.
((count++)) ((count++))
@ -327,7 +327,7 @@ name_ext()
mv -- "${target}" "${new_name}" &> /dev/null mv -- "${target}" "${new_name}" &> /dev/null
# show change. # show change.
echo -e "${color_default}${status}${color_default}" echo -e "${color_bblue}${status}${color_default}"
else else
# Increment count. # Increment count.
((count++)) ((count++))
@ -375,7 +375,7 @@ name_prefix()
mv -- "${target}" "${new_name}" &> /dev/null mv -- "${target}" "${new_name}" &> /dev/null
# Show change. # Show change.
echo -e "${color_default}${status}${color_default}" echo -e "${color_bblue}${status}${color_default}"
done done
} }

View file

@ -1,7 +1,7 @@
_UNPACK_SUPPORTED=".tar$|.tgz$|.txz$|.tar.gz$|.tar.xz$|.zip$|.iso$|.rar$" _UNPACK_SUPPORTED=".tar$|.tgz$|.txz$|.tar.gz$|.tar.xz$|.zip$|.iso$|.rar$"
# Pack files into desired format. # Pack files into desired format.
# Usage: pack <TARGET.ext> <FILES> # Usage: pack <TARGET.ext> [FILES]
pack() pack()
{ {
local output="${1}" local output="${1}"
@ -11,21 +11,20 @@ pack()
# report no output. # report no output.
if [[ "${output}" = "" ]]; then if [[ "${output}" = "" ]]; then
echo "Usage: pack <TARGET.ext> <FILES>" echo "Usage: pack <TARGET.ext> [FILES]"
return 2 return 2
fi fi
# report no format. # report no format.
if [[ "${format}" = "" ]]; then if [[ "${format}" = "" ]]; then
echo "Could not determine output format." echo "Could not determine output format."
echo "Usage: pack <TARGET.ext> <FILES>" echo "Usage: pack <TARGET.ext> [FILES]"
return 2 return 2
fi fi
# report no targets. # All targets by default.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
echo "Usage: pack <TARGET.ext> <FILES>" targets=($(ls))
return 2
fi fi
# process. # process.
@ -58,7 +57,7 @@ pack()
} }
# attempt to unpack everything. # attempt to unpack everything.
# usage: unpack <FILES> # usage: unpack [FILES]
unpack() unpack()
{ {
local targets=("${@}") local targets=("${@}")
@ -66,7 +65,7 @@ unpack()
local failed=0 local failed=0
local total=${#targets[@]} local total=${#targets[@]}
# show error if no target specified. # All targets by default.
if [[ "${targets}" = "" ]]; then if [[ "${targets}" = "" ]]; then
targets=($(ls | grep -E ${_UNPACK_SUPPORTED})) targets=($(ls | grep -E ${_UNPACK_SUPPORTED}))
total=${#targets[@]} total=${#targets[@]}
@ -81,7 +80,7 @@ unpack()
local status="[${count}/${total}] ${target}" local status="[${count}/${total}] ${target}"
# show status. # show status.
echo -e "${status}" echo -e "${color_bblue}${status}${color_default}"
# unpack file type. # unpack file type.
local type="${target##*.}" local type="${target##*.}"
@ -133,10 +132,10 @@ unpack()
# print report. # print report.
if [[ ${count} -gt 1 ]] || [[ "${*}" = "" ]]; then if [[ ${count} -gt 1 ]] || [[ "${*}" = "" ]]; then
if [[ ${failed} -gt 0 ]]; then if [[ ${failed} = 0 ]]; then
echo -e "${color_bred}Items failed to unpack: ${failed}.${color_default}"
else
echo -e "${color_green}All successful.${color_default}" echo -e "${color_green}All successful.${color_default}"
else
echo -e "${color_bred}Items failed to unpack: ${failed}.${color_default}"
fi fi
fi fi
} }