add script task templates and refactor convert-to-mp3 to use template pattern
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,20 +1,11 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
# Accepts path as $1 or $INPUT_FILE_PATH
|
||||
TARGET="${1:-${INPUT_FILE_PATH:-}}"
|
||||
# ── Task: Convert To MP3 ──
|
||||
|
||||
if [[ -z "$TARGET" ]]; then
|
||||
echo "Error: No file or directory path provided" >&2
|
||||
exit 1
|
||||
fi
|
||||
EXTENSIONS="flac|wav|ogg|wma|aac|m4a|opus|aiff|aif|ape|wv|alac|dsf|dff"
|
||||
|
||||
if [[ ! -e "$TARGET" ]]; then
|
||||
echo "Error: Path does not exist: $TARGET" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
convert_file() {
|
||||
process_file() {
|
||||
local input="$1"
|
||||
local ext="${input##*.}"
|
||||
local dir
|
||||
@@ -34,37 +25,48 @@ convert_file() {
|
||||
fi
|
||||
|
||||
echo "Converting: $input → $output"
|
||||
ffmpeg -i "$input" -codec:a libmp3lame -b:a 320k -map_metadata 0 -id3v2_version 3 -y "$output" 2>/dev/null
|
||||
|
||||
if [[ $? -eq 0 ]]; then
|
||||
echo " ✓ Done"
|
||||
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
|
||||
echo " Failed" >&2
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
AUDIO_EXTS="flac|wav|ogg|wma|aac|m4a|opus|aiff|aif|ape|wv|alac|dsf|dff"
|
||||
# ── 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 convert_file "$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 convert_file "$file"; then
|
||||
if process_file "$file"; then
|
||||
converted=$((converted + 1))
|
||||
else
|
||||
failed=$((failed + 1))
|
||||
fi
|
||||
done < <(find "$TARGET" -type f -regextype posix-extended -iregex ".*\.($AUDIO_EXTS)" -print0 | sort -z)
|
||||
done < <(find "$TARGET" -type f -regextype posix-extended -iregex ".*\.($EXTENSIONS)" -print0 | sort -z)
|
||||
|
||||
if [[ $converted -eq 0 && $failed -eq 0 ]]; then
|
||||
echo "No audio files found in: $TARGET"
|
||||
echo "No matching files found in: $TARGET"
|
||||
exit 0
|
||||
fi
|
||||
else
|
||||
@@ -73,4 +75,4 @@ else
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "Summary: $converted converted, $failed failed"
|
||||
echo "Summary: $converted processed, $failed failed"
|
||||
|
||||
Reference in New Issue
Block a user