Created
June 10, 2025 03:33
-
-
Save AlyxPink/ad3b4abd40836d1fad51086b36520444 to your computer and use it in GitHub Desktop.
Script created with LLM to convert tons of audio files to another format, recursively, using a network attached disk on Windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| set -euo pipefail | |
| # MUST CONFIGURE THIS PATH | |
| NAS_MOUNT="/mnt/z/Audio" # Windows/WSL: /mnt/z/... | |
| # OR for Linux: "/mnt/nas_audio" | |
| # Configuration Options | |
| DRY_RUN=true | |
| OVERWRITE=false | |
| THREADS=$(nproc) # Uses all cores | |
| OUTPUT_EXT="m4a" # Universally compatible AAC format | |
| # Validate mount point | |
| if [[ ! -d "$NAS_MOUNT" ]]; then | |
| echo "ERROR: NAS mount point not found: $NAS_MOUNT" | |
| echo "1. Confirm NAS is mounted" | |
| echo "2. Update NAS_MOUNT path in script" | |
| exit 1 | |
| fi | |
| # Check dependencies | |
| command -v ffmpeg >/dev/null || { echo "Install ffmpeg: sudo apt install ffmpeg"; exit 1; } | |
| command -v parallel >/dev/null || { echo "Install parallel: sudo apt install parallel"; exit 1; } | |
| # Find all opus files | |
| echo "Scanning OPUS files in $NAS_MOUNT..." | |
| mapfile -d '' opus_files < <(find "$NAS_MOUNT" -type f \( -iname "*.opus" -o -iname "*.ogg" \) -print0 2>/dev/null) | |
| if [[ ${#opus_files[@]} -eq 0 ]]; then | |
| echo "No OPUS files found" | |
| exit 0 | |
| fi | |
| # Dry-run mode | |
| dry_run() { | |
| echo "--- DRY RUN ($THREADS THREADS) ---" | |
| echo "Found ${#opus_files[@]} OPUS files" | |
| for file in "${opus_files[@]}"; do | |
| output="${file%.*}.$OUTPUT_EXT" | |
| if [[ -f "$output" && "$OVERWRITE" != "true" ]]; then | |
| echo "[SKIP] Exists: $output" | |
| else | |
| echo "[CONVERT] $file ➔ $output" | |
| fi | |
| done | |
| read -rp "Execute conversion? (y/n): " choice | |
| [[ "$choice" == "y" ]] || exit 0 | |
| } | |
| # Conversion function using native AAC encoder | |
| convert_file() { | |
| local input="$1" | |
| local output="${input%.*}.$OUTPUT_EXT" | |
| [[ -f "$output" && "$OVERWRITE" != "true" ]] && return | |
| # Using native AAC encoder with highest quality VBR | |
| ffmpeg -v error -nostdin -i "$input" -c:a aac -q:a 1 -vn -y "$output" | |
| echo "✓ $output" | |
| } | |
| export -f convert_file | |
| export OUTPUT_EXT OVERWRITE | |
| # Execution flow | |
| [[ "$DRY_RUN" == "true" ]] && dry_run | |
| echo "Starting conversion with $THREADS threads..." | |
| printf "%s\0" "${opus_files[@]}" | parallel -0 -j "$THREADS" convert_file | |
| # Post-conversion cleanup (manual) | |
| echo -e "\nSuccess! To remove originals safely:" | |
| echo "find \"$NAS_MOUNT\" -type f \( -name \"*.opus\" -o -name \"*.ogg\" \) -exec echo rm {} \;" | |
| echo "# REMOVE 'echo' AFTER VERIFYING FILE LIST" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment