Transcode : Use _iterate_target.

This commit is contained in:
Dmitry Voronin 2023-12-17 20:44:54 +03:00
parent 418f146141
commit 43b1d7bc8e
3 changed files with 20 additions and 43 deletions

View file

@ -32,7 +32,7 @@ function checksum_check() {
local hashfile=".${target#./}.sha1"
# Skip if hash doesn't exist.
[[ -f "${hashfile}" ]] || { ((skipped++)); _warn "No hash found."; return 0; }
[[ -f "${hashfile}" ]] || { _skip "No hash found."; return 0; }
# Calculate hash.
local stored=$(cat "${hashfile}" | cut -d\ -f1)

View file

@ -3,43 +3,26 @@
# Usage: transcode <FORMAT> [FILES]
function transcode() {
local IFS=$'\n'
local targets=("${@:2}")
local format="${1}"
local total=${#targets[@]}
local count=0
local failed=0
local skipped=0
local format=${1}
local targets=(${@:2})
[[ "${targets}" = "" ]] && targets=($(_ls_file))
# Report no format.
if [[ "${format}" = "" ]]; then
echo -e "${color_bred}No format specified.${color_default}"
if [[ "${format}" = "" ]] || [[ "${format}" =~ "." ]]; then
_error "No format specified."
help transcode
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++))
process() {
# Define context names and status.
local from="${target##*.}"
local to="${format}"
local output="${target##*/}"
output="${output%.*}.${to}"
local status="[${count}/${total}] ${target} -> ${output}"
# Show status.
echo -e "${color_bwhite}${status}${color_default}"
# Skip if file exists.
[[ -f "${output}" ]] && { ((skipped++)); continue; }
[[ -f "${output}" ]] && { _skip "Already exists."; return 0; }
# Support multiple inputs.
[[ "${to}" = "mp3" ]] && from=""
@ -68,27 +51,13 @@ function transcode() {
_transcode_mkv "${target}" "${output}"
;;
*)
echo -e "${color_yellow}Conversion ${target##*.}-${to} not supported.${color_default}"
false
_error "Conversion ${target##*.}-${to} not supported."
return 1
;;
esac
}
# Show error.
if [[ ${?} != 0 ]]; then
echo -e "${color_bred}${status}: Failed.${color_default}"
((failed++))
fi
done
if [[ ${skipped} != 0 ]]; then
echo -e "${color_byellow}Skipped: ${skipped}.${color_default}"
return 1
fi
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
_iterate_targets process ${targets[@]}
}
function _transcode_gz-xz() {

View file

@ -77,3 +77,11 @@ function _error() {
function _warn() {
echo -e "${color_byellow}${*}${color_default}"
}
# Skip current iteration.
# Usage: _skip [MESSAGE]
function _skip() {
((skipped++))
[[ "${*}" = "" ]] || echo -e "${color_byellow}${*}${color_default}"
}