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`|
## Convert.
`convert <FROM.gz> <TO.xz>`|Convert between different formats.
`convert <FORMAT> [FILES]`|Convert between different formats.
## Copy/paste.
@ -569,7 +569,7 @@ 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.
## Permissions.

View file

@ -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
}

View file

@ -1,53 +1,94 @@
# Convert between different formats.
# Usage: convert <FROM.gz> <TO.xz>
# Usage: convert <FORMAT> [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 <FORMAT> [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.
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 "${color_bblue}${status}${color_default}"
# Support multiple inputs.
[[ "${to}" = "mp3" ]] && from=""
# Send convert.
case "${from}-${to}" in
"gz-xz"|"tgz-txz")
_convert_gz-xz "${input}" "${output}"
_convert_gz-xz "${target}"
;;
"xz-gz"|"txz-tgz")
_convert_xz-gz "${input}" "${output}"
_convert_xz-gz "${target}"
;;
"-mp3")
_convert_mp3 "${target}"
;;
*)
echo -e "${color_yellow}Conversion ${from}-${to} not supported.${color_default}"
return 1
false
;;
esac
if [[ $? = 0 ]]; then
echo -e "${color_green}Done.${color_default}"
else
echo -e "${color_bred}There were errors.${color_default}"
# 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.

View file

@ -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
}

View file

@ -1,7 +1,7 @@
_UNPACK_SUPPORTED=".tar$|.tgz$|.txz$|.tar.gz$|.tar.xz$|.zip$|.iso$|.rar$"
# Pack files into desired format.
# Usage: pack <TARGET.ext> <FILES>
# Usage: pack <TARGET.ext> [FILES]
pack()
{
local output="${1}"
@ -11,21 +11,20 @@ pack()
# report no output.
if [[ "${output}" = "" ]]; then
echo "Usage: pack <TARGET.ext> <FILES>"
echo "Usage: pack <TARGET.ext> [FILES]"
return 2
fi
# report no format.
if [[ "${format}" = "" ]]; then
echo "Could not determine output format."
echo "Usage: pack <TARGET.ext> <FILES>"
echo "Usage: pack <TARGET.ext> [FILES]"
return 2
fi
# report no targets.
# All targets by default.
if [[ "${targets}" = "" ]]; then
echo "Usage: pack <TARGET.ext> <FILES>"
return 2
targets=($(ls))
fi
# process.
@ -58,7 +57,7 @@ pack()
}
# attempt to unpack everything.
# usage: unpack <FILES>
# 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
}