#!/bin/bash

# Functie om audio te extraheren
extract_audio() {
    INPUT="$1"
    OUTPUT="${INPUT%.*}.mp3"
    ffmpeg -i "$INPUT" -q:a 0 "$OUTPUT"
}

# Begin de loop om de map in de gaten te houden
inotifywait -m -r -e create --format '%w%f' /watched | while read FILE
do
    # Wacht tot het bestand volledig is geschreven
    sleep 1
    
    # Controleer of het bestand een video- of audio-bestand is
    if [[ "$FILE" == *.mp4 || "$FILE" == *.MP4 || "$FILE" == *.avi || "$FILE" == *.mov || "$FILE" == *.flv || "$FILE" == *.wmv || "$FILE" == *.mp3 || "$FILE" == *.wav ]]; then
        if [[ "$FILE" != "/watched/watch.sh" ]]; then  # Vermijd het verwerken van het script zelf
            echo "Nieuw bestand gedetecteerd: $FILE"
            extract_audio "$FILE"
        fi
    fi
done