#!/bin/bash INPUT_DIR="/mnt/jonathanDisk/ivritData" OUTPUT_DIR="/mnt/jonathanDisk/ivritDataOpus" CACHE_DIR="$HOME/ivritCache" WORKERS=24 FF_CMD=$(which ffmpeg) PROBE_CMD=$(which ffprobe) echo "======================================================" echo "Starting Distributed Audio Pipeline (v6)" echo "Workers: $WORKERS" echo "FFmpeg path: $FF_CMD" echo "======================================================" mkdir -p "$OUTPUT_DIR" mkdir -p "$CACHE_DIR" # [Phase 1 code remains unchanged here. Assuming we are focusing on Phase 2 scaling] echo ">>> PHASE 2: Distributed Processing of new files..." find "$INPUT_DIR" -type f \( -iname \*.wav -o -iname \*.mp3 -o -iname \*.flac -o -iname \*.m4a -o -iname \*.ogg \) -print0 | xargs -0 -P "$WORKERS" -I {} bash -c ' infile="$1" in_dir="$2" out_dir="$3" cache_dir="$4" ffmpeg_bin="$5" relpath="${infile#$in_dir/}" target_folder="$out_dir/$(dirname "$relpath")" cache_folder="$cache_dir/$(dirname "$relpath")" filename=$(basename "$infile") outfile="$target_folder/${filename%.*}.opus" lock_dir="${outfile}.lock" # 1. IS IT DONE? (Check if output exists) if [ -f "$outfile" ]; then exit 0 fi # Create the specific subfolders if they do not exist mkdir -p "$target_folder" 2>/dev/null mkdir -p "$cache_folder" 2>/dev/null # 2. IS IT PROCESSING? (Atomic Lock) # If mkdir fails, another worker/laptop already claimed this file. Skip it. if ! mkdir "$lock_dir" 2>/dev/null; then exit 0 fi # If we made it here, this specific worker owns the file. cache_source="$cache_folder/$filename" cache_target="$cache_folder/${filename%.*}.opus" # Process the file if cp "$infile" "$cache_source"; then if "$ffmpeg_bin" -y -hide_banner -loglevel error -i "$cache_source" -af "aresample=resample_cutoff=0.98" -ar 16000 -ac 1 -c:a libopus "$cache_target"; then # Move finished file back to HDD mv "$cache_target" "$outfile" else echo "Error encoding: $infile" fi # Clean up SSD Cache rm -f "$cache_source" else echo "Error copying to SSD cache: $infile" fi # 3. RELEASE THE LOCK # Clean up the lock directory so we do not leave orphaned locks if something failed rm -rf "$lock_dir" ' _ {} "$INPUT_DIR" "$OUTPUT_DIR" "$CACHE_DIR" "$FF_CMD" echo ">>> PHASE 2 COMPLETE."