79 lines
1.7 KiB
Bash
Executable File
79 lines
1.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
# ── Task: Convert To MP3 ──
|
|
|
|
EXTENSIONS="flac|wav|ogg|wma|aac|m4a|opus|aiff|aif|ape|wv|alac|dsf|dff"
|
|
|
|
process_file() {
|
|
local input="$1"
|
|
local ext="${input##*.}"
|
|
local dir
|
|
dir="$(dirname "$input")"
|
|
local base
|
|
base="$(basename "$input" ".$ext")"
|
|
local output="$dir/$base.mp3"
|
|
|
|
if [[ "${ext,,}" == "mp3" ]]; then
|
|
echo "Skipping (already MP3): $input"
|
|
return 0
|
|
fi
|
|
|
|
if [[ -f "$output" ]]; then
|
|
echo "Skipping (output exists): $output"
|
|
return 0
|
|
fi
|
|
|
|
echo "Converting: $input → $output"
|
|
if ffmpeg -i "$input" -codec:a libmp3lame -b:a 320k -map_metadata 0 -id3v2_version 3 -y "$output" 2>/dev/null; then
|
|
echo " Done"
|
|
else
|
|
echo " Failed" >&2
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# ── Template: File Processor ──
|
|
|
|
TARGET="${1:-${INPUT_FILE_PATH:-}}"
|
|
|
|
if [[ -z "$TARGET" ]]; then
|
|
echo "Error: No file or directory path provided" >&2
|
|
exit 1
|
|
fi
|
|
|
|
if [[ ! -e "$TARGET" ]]; then
|
|
echo "Error: Path does not exist: $TARGET" >&2
|
|
exit 1
|
|
fi
|
|
|
|
converted=0
|
|
failed=0
|
|
|
|
if [[ -f "$TARGET" ]]; then
|
|
if process_file "$TARGET"; then
|
|
converted=$((converted + 1))
|
|
else
|
|
failed=$((failed + 1))
|
|
fi
|
|
elif [[ -d "$TARGET" ]]; then
|
|
while IFS= read -r -d '' file; do
|
|
if process_file "$file"; then
|
|
converted=$((converted + 1))
|
|
else
|
|
failed=$((failed + 1))
|
|
fi
|
|
done < <(find "$TARGET" -type f -regextype posix-extended -iregex ".*\.($EXTENSIONS)" -print0 | sort -z)
|
|
|
|
if [[ $converted -eq 0 && $failed -eq 0 ]]; then
|
|
echo "No matching files found in: $TARGET"
|
|
exit 0
|
|
fi
|
|
else
|
|
echo "Error: Not a file or directory: $TARGET" >&2
|
|
exit 1
|
|
fi
|
|
|
|
echo ""
|
|
echo "Summary: $converted processed, $failed failed"
|