#!/bin/bash # Check if input directory is provided if [ -z "$1" ]; then echo "Usage: $0 /path/to/directory" exit 1 fi # Define input directory and output folder input_dir="$1" output_dir="$input_dir/output" # Create the output folder if it doesn't exist mkdir -p "$output_dir" # Log file for errors and detailed output log_file="$input_dir/conversion_log.txt" error_log="$input_dir/conversion_errors.log" missing_file_log="$input_dir/missing_files.log" # Clear old logs > "$log_file" > "$error_log" > "$missing_file_log" # Create a list of expected filenames (based on video files) expected_files=() # Loop over each subdirectory in the input directory for subdir in "$input_dir"/*/; do # Skip if it's not a directory if [ ! -d "$subdir" ]; then continue fi echo "Processing directory: $subdir" | tee -a "$log_file" # Change to the subdirectory cd "$subdir" || continue # Loop over all video files in the subdirectory and convert them to mp3 for video_file in *.mp4 *.mkv *.avi *.webm *.flv *.mov; do # Skip if no video files are found if [[ ! -e "$video_file" ]]; then continue fi # Extract filename without extension and add to expected list filename=$(basename "$video_file") filename_noext="${filename%.*}" expected_files+=("$filename_noext.mp3") # Set output mp3 path output_mp3="$output_dir/$filename_noext.mp3" # Convert video to mp3 echo "Converting: $video_file to $output_mp3" | tee -a "$log_file" # Run ffmpeg and capture output and error ffmpeg -y -i "$video_file" "$output_mp3" >> "$log_file" 2>> "$error_log" # Check if conversion was successful if [ $? -eq 0 ]; then echo "Successfully converted: $video_file to $output_mp3" | tee -a "$log_file" else echo "Error converting: $video_file" | tee -a "$error_log" fi done done # Create a list of actual output filenames actual_files=() for mp3_file in "$output_dir"/*.mp3; do if [ -e "$mp3_file" ]; then actual_files+=("$(basename "$mp3_file")") fi done # Compare the expected and actual files missing_files=() for expected in "${expected_files[@]}"; do if [[ ! " ${actual_files[@]} " =~ " $expected " ]]; then missing_files+=("$expected") fi done # Output the results of the conversion and missing files check echo "Conversion completed. Check $log_file for detailed output and $error_log for errors." if [ ${#missing_files[@]} -eq 0 ]; then echo "All files were converted successfully!" else echo "Missing files (not converted):" | tee -a "$missing_file_log" for missing in "${missing_files[@]}"; do echo "$missing" | tee -a "$missing_file_log" done fi # Now, run the command to check missing files by comparing video and MP3 filenames find "$input_dir" -type f \( -iname "*.mp4" -o -iname "*.mkv" -o -iname "*.avi" -o -iname "*.webm" -o -iname "*.flv" -o -iname "*.mov" \) -exec basename {} \; | sed 's/\(.*\)\..*/\1/' | sort > video_list.txt find "$output_dir" -type f -iname "*.mp3" -exec basename {} \; | sed 's/\(.*\)\..*/\1/' | sort > mp3_list.txt diff video_list.txt mp3_list.txt | tee -a "$missing_file_log"