#!/bin/bash

# Directory containing the movies
DIRECTORY="$1"

# Check if directory is provided
if [ -z "$DIRECTORY" ]; then
    echo "Usage: $0 <directory>"
    exit 1
fi

# Check if the provided argument is a directory
if [ ! -d "$DIRECTORY" ]; then
    echo "Error: $DIRECTORY is not a valid directory."
    exit 1
fi

# Loop through all video files in the directory
for file in "$DIRECTORY"/*; do
    # Check if the file is a video file
    if [[ -f "$file" && "$file" =~ \.(mp4|mkv|avi|mov|flv|wmv)$ ]]; then
        # Check for Dolby audio track
        if ffmpeg -i "$file" 2>&1 | grep -q "DOLBY"; then
            # Define output file name
            output_file="${file%.*}_converted.aac"

            # Convert Dolby audio to AAC
            ffmpeg -i "$file" -c:v copy -c:a aac -b:a 192k "$output_file"

            echo "Converted: $file to $output_file"
        else
            echo "No Dolby audio found in: $file"
        fi
    fi
done

echo "Conversion process completed."
exit 0