#!/bin/bash # Check if the correct number of arguments is provided if [ $# -ne 1 ]; then echo "Usage: $0 " exit 1 fi # Get the top-level directory from the argument TOP_DIR="$1" # Define a temporary file to store the list of files FILE_LIST="file_list.txt" # Remove the temporary file if it exists rm -f "$FILE_LIST" # Find all segment directories and sort them numerically dirs=$(find "$TOP_DIR" -maxdepth 1 -type d -name 'segment_*' | sort -V) # Loop over the directories for dir in $dirs; do segment_name=$(basename "$dir") segment_num=$(echo "$segment_name" | sed 's/segment_//') output_file="$dir/output_$segment_num.mp4" if [ -f "$output_file" ]; then echo "file '$output_file'" >> "$FILE_LIST" else echo "No output_$segment_num.mp4 found in $dir" fi done # Run ffmpeg to concatenate the videos ffmpeg -f concat -safe 0 -i "$FILE_LIST" -c copy output_combined.mp4 # Remove the temporary file rm "$FILE_LIST"