#!/bin/bash INPUT_DIR="/mnt/jonathanDisk/ivritData" OUTPUT_DIR="/mnt/jonathanDisk/ivritDataOpus" CACHE_DIR="$HOME/ivritCache" WORKERS=24 echo "Initializing FFmpeg CPU flood with $WORKERS workers..." echo "Input: $INPUT_DIR" echo "Output: $OUTPUT_DIR" echo "SSD Cache: $CACHE_DIR" # Ensure all base directories exist mkdir -p "$OUTPUT_DIR" mkdir -p "$CACHE_DIR" # 1. Find all audio files # 2. Pipe into xargs with -P to run $WORKERS in parallel # 3. Pass infile, input_dir, output_dir, and cache_dir cleanly into the subshell 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" # Calculate the relative path to maintain your exact folder hierarchy relpath="${infile#$in_dir/}" # Define target and cache subfolders target_folder="$out_dir/$(dirname "$relpath")" cache_folder="$cache_dir/$(dirname "$relpath")" # Create the specific subfolders if they do not exist mkdir -p "$target_folder" mkdir -p "$cache_folder" # Swap the original extension to .opus filename=$(basename "$infile") cachefile="$cache_folder/${filename%.*}.opus" outfile="$target_folder/${filename%.*}.opus" # Run FFmpeg writing to the SSD Cache first # Using && ensures we only move the file if the encoding succeeded without errors if ffmpeg -y -hide_banner -loglevel error -i "$infile" -ar 16000 -c:a libopus "$cachefile"; then mv "$cachefile" "$outfile" else echo "Error encoding: $infile" fi ' _ {} "$INPUT_DIR" "$OUTPUT_DIR" "$CACHE_DIR" echo "Pipeline complete! All files converted and moved to HDD."