#!/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" # Set up filenames for the SSD cache and HDD destination filename=$(basename "$infile") cache_source="$cache_folder/$filename" cache_target="$cache_folder/${filename%.*}.opus" outfile="$target_folder/${filename%.*}.opus" # STEP 1: Copy the source file from the HDD to the SSD # We use an `if` statement so we only encode if the copy succeeds # (prevents zero-byte errors if your SSD gets full) if cp "$infile" "$cache_source"; then # STEP 2: Run FFmpeg reading from the SSD and writing to the SSD if ffmpeg -y -hide_banner -loglevel error -i "$cache_source" -ar 16000 -c:a libopus "$cache_target"; then # STEP 3: Move the finished Opus file back to the HDD mv "$cache_target" "$outfile" else echo "Error encoding: $infile" fi # STEP 4: Delete the source copy from the SSD to free up space rm -f "$cache_source" else echo "Error copying to SSD cache: $infile" fi ' _ {} "$INPUT_DIR" "$OUTPUT_DIR" "$CACHE_DIR" echo "Pipeline complete! All files converted and moved to HDD."