generalize convert-to-mp3 into convert-audio with selectable target format
- rename task to convert-audio, support mp3/flac/wav/ogg/aac/opus targets - add options input type with selectable pill buttons in UI - extend seed script parser to handle YAML list properties (options) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
---
|
||||
name: Convert Audio
|
||||
description: Convert audio files between formats, preserving metadata.
|
||||
version: 1
|
||||
mode: script
|
||||
language: bash
|
||||
triggers:
|
||||
- type: file
|
||||
extensions:
|
||||
- mp3
|
||||
- flac
|
||||
- wav
|
||||
- ogg
|
||||
- wma
|
||||
- aac
|
||||
- m4a
|
||||
- opus
|
||||
- aiff
|
||||
- aif
|
||||
- ape
|
||||
- wv
|
||||
- alac
|
||||
- dsf
|
||||
- dff
|
||||
- type: directory
|
||||
inputs:
|
||||
file_path:
|
||||
type: string
|
||||
description: Path to an audio file or directory to convert.
|
||||
target_format:
|
||||
type: string
|
||||
description: Target format
|
||||
default: mp3
|
||||
options:
|
||||
- mp3
|
||||
- flac
|
||||
- wav
|
||||
- ogg
|
||||
- aac
|
||||
- opus
|
||||
delete_source:
|
||||
type: boolean
|
||||
description: Delete source files after successful conversion.
|
||||
default: false
|
||||
args: [file_path]
|
||||
---
|
||||
|
||||
# Convert Audio
|
||||
|
||||
Convert audio files between formats using ffmpeg, preserving metadata.
|
||||
Supports single file conversion and batch directory conversion.
|
||||
Executable
+99
@@ -0,0 +1,99 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
# ── Task: Convert Audio ──
|
||||
|
||||
EXTENSIONS="mp3|flac|wav|ogg|wma|aac|m4a|opus|aiff|aif|ape|wv|alac|dsf|dff"
|
||||
TARGET_FORMAT="${INPUT_TARGET_FORMAT:-mp3}"
|
||||
DELETE_SOURCE="${INPUT_DELETE_SOURCE:-false}"
|
||||
|
||||
ffmpeg_args_for_format() {
|
||||
case "$1" in
|
||||
mp3) echo "-codec:a libmp3lame -b:a 320k -id3v2_version 3" ;;
|
||||
flac) echo "-codec:a flac" ;;
|
||||
wav) echo "-codec:a pcm_s16le" ;;
|
||||
ogg) echo "-codec:a libvorbis -q:a 6" ;;
|
||||
aac) echo "-codec:a aac -b:a 256k" ;;
|
||||
opus) echo "-codec:a libopus -b:a 192k" ;;
|
||||
*) echo "Unsupported format: $1" >&2; return 1 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
FFMPEG_ARGS=$(ffmpeg_args_for_format "$TARGET_FORMAT") || exit 1
|
||||
|
||||
process_file() {
|
||||
local input="$1"
|
||||
local ext="${input##*.}"
|
||||
local dir
|
||||
dir="$(dirname "$input")"
|
||||
local base
|
||||
base="$(basename "$input" ".$ext")"
|
||||
local output="$dir/$base.$TARGET_FORMAT"
|
||||
|
||||
if [[ "${ext,,}" == "$TARGET_FORMAT" ]]; then
|
||||
echo "Skipping (already $TARGET_FORMAT): $input"
|
||||
return 0
|
||||
fi
|
||||
|
||||
if [[ -f "$output" ]]; then
|
||||
echo "Skipping (output exists): $output"
|
||||
return 0
|
||||
fi
|
||||
|
||||
echo "Converting: $input → $output"
|
||||
# shellcheck disable=SC2086
|
||||
if ffmpeg -nostdin -i "$input" $FFMPEG_ARGS -map_metadata 0 -y "$output" 2>/dev/null; then
|
||||
echo " Done"
|
||||
if [[ "$DELETE_SOURCE" == "true" ]]; then
|
||||
rm "$input"
|
||||
echo " Deleted source"
|
||||
fi
|
||||
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"
|
||||
Reference in New Issue
Block a user