#!/bin/bash # Exit immediately if a command exits with a non-zero status. set -e # Function to display informational messages in blue echo_info() { echo -e "\033[1;34m[INFO]\033[0m $1" } # Function to display error messages in red echo_error() { echo -e "\033[1;31m[ERROR]\033[0m $1" } # Function to display usage instructions usage() { echo "Usage: $0 " echo "Example: $0 606-short_segments" exit 1 } # Check if exactly one argument is provided if [ "$#" -ne 1 ]; then echo_error "Incorrect number of arguments." usage fi # Assign the first argument to SEGMENTS_DIR SEGMENTS_DIR="$1" # Resolve the absolute path of SEGMENTS_DIR SEGMENTS_DIR=$(realpath "$SEGMENTS_DIR") # Check if the provided argument is a directory if [ ! -d "$SEGMENTS_DIR" ]; then echo_error "The provided path '$SEGMENTS_DIR' is not a directory or does not exist." exit 1 fi echo_info "Segments directory set to: $SEGMENTS_DIR" # Check if ffmpeg is installed if ! command -v ffmpeg &> /dev/null; then echo_error "ffmpeg could not be found. Please install ffmpeg and ensure it's in your PATH." exit 1 fi # Create a temporary file to store the list of videos to concatenate concat_list=$(mktemp) # Ensure the temporary file is deleted on script exit trap "rm -f $concat_list" EXIT # Iterate over each segment directory within SEGMENTS_DIR in sorted order # Assuming segments are named as segment_1, segment_2, etc. for segment_dir in "$SEGMENTS_DIR"/segment_*; do if [ -d "$segment_dir" ]; then segment_name=$(basename "$segment_dir") echo_info "Processing segment: $segment_name" # Define the path to output-matted.mp4 output_matted="$segment_dir/output-matted.mp4" # Check if output-matted.mp4 exists if [ ! -f "$output_matted" ]; then echo_error " '$output_matted' does not exist. Skipping this segment." continue fi # Append the file path to the concat list in the required format echo "file '$output_matted'" >> "$concat_list" echo_info " Added '$output_matted' to the concatenation list." else echo_error "'$segment_dir' is not a directory. Skipping." fi done # Check if the concat list has at least one file if [ ! -s "$concat_list" ]; then echo_error "No 'output-matted.mp4' files found in the provided segments directory." exit 1 fi echo_info "Starting concatenation of videos..." # Define the output file path output_full="full_output_matted.mp4" # Execute ffmpeg to concatenate videos using the concat demuxer ffmpeg -f concat -safe 0 -i "$concat_list" -c copy "$output_full" \ && echo_info "Successfully created '$output_full' in $(pwd)" echo_info "Concatenation process completed."