Files
2025-04-12 09:56:11 +02:00

64 lines
1.5 KiB
Bash
Executable File

#!/bin/bash
# Check for ffmpeg dependency
if ! command -v ffmpeg &> /dev/null; then
echo "ffmpeg could not be found. Please install ffmpeg to use this script."
exit 1
fi
# Number of concurrent ffmpeg processes
N=${1:-8}
# Semaphore file for limiting concurrent processes
semaphore=/tmp/ffmpeg.lock
# Opening the semaphore file descriptor on 9
exec 9>"$semaphore"
# Function to run an ffmpeg command with semaphore locking
run_ffmpeg() {
# Wait for a semaphore slot to become available
flock -x 9
# Execute the ffmpeg command
ffmpeg -y -i "$1" -af "loudnorm=I=-16:LRA=11:measured_I=-20:measured_LRA=16,volume=0.8" "$2"
if [ $? -ne 0 ]; then
echo "An error occurred with ffmpeg processing $1"
# Release the semaphore slot on error as well
flock -u 9
return 1
fi
# Release the semaphore slot
flock -u 9
}
# Create the semaphore file if it does not exist
touch "$semaphore"
if [ ! -f "$semaphore" ]; then
echo "Failed to create semaphore file."
exit 1
fi
# Processing each .opus file
find . -maxdepth 1 -type f -name '*.m4a' | while read -r file; do
wav_file="${file/%.m4a/.wav}"
if [ ! -f "$wav_file" ]; then
echo "Processing $file..."
run_ffmpeg "$file" "$wav_file" &
# Ensure N parallel ffmpeg processes
while [ $(jobs -p | wc -l) -ge "$N" ]; do
wait -n
done
fi
done
# Wait for all background jobs to finish
wait
# Close the semaphore file descriptor and remove the file
exec 9>&-
rm -f "$semaphore"