Skip to content

Instantly share code, notes, and snippets.

@varunchandak
Created April 20, 2024 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save varunchandak/1980746316021400823ff43a80d3b270 to your computer and use it in GitHub Desktop.
Save varunchandak/1980746316021400823ff43a80d3b270 to your computer and use it in GitHub Desktop.
Join Multiple Videos for YouTube (with chapters)

Video Concatenation Guide with FFmpeg

This guide provides instructions on how to concatenate multiple video files using FFmpeg, add a black screen between each video, and address common timestamp issues during the concatenation process.

Requirements

  • FFmpeg installed on your system.
  • Videos to be concatenated should be in the same codec, resolution, and frame rate.

Steps

Step 1: Create a Black Screen Video

Ensure the black screen video matches the resolution and frame rate of your other videos.

ffmpeg -f lavfi -i color=c=black:s=1920x1080:r=30:d=1 black.mp4

Step 2: Create a Concatenation File

Create a text file (filelist.txt) listing all the video files to be concatenated, including the black screen video between each main video.

file 'VID_20240203_103925.mp4'
file 'black.mp4'
file 'VID_20240203_105117.mp4'
file 'black.mp4'
...
file 'VID_20240204_142836.mp4'

Step 3: Concatenate Videos Without Re-encoding

Use the following FFmpeg command to concatenate the videos without re-encoding:

ffmpeg -f concat -safe 0 -i filelist.txt -c copy output.mp4

Addressing Non-monotonic DTS Errors

If you encounter "Non-monotonic DTS" errors, use the following commands to fix the timestamps:

  1. Convert each video to MPEG-TS format:
    ffmpeg -i input.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts
  2. Concatenate TS files:
    ffmpeg -i "concat:intermediate1.ts|intermediate2.ts|...|intermediateN.ts" -c copy -bsf:a aac_adtstoasc output.mp4

Step 4: Generate Timestamps for YouTube Chapters

To generate timestamps for YouTube chapters:

Calculate the start time for each video manually or using the script attached, accounting for the duration of each video and the 1-second black screens. Include these timestamps in your YouTube video description, formatted as follows:

00:00:00 Introduction
00:00:10 Chapter 1
00:05:15 Chapter 2
...

Conclusion

This method ensures that videos are concatenated with a uniform format, maintaining quality and ensuring proper playback without timestamp errors.

#!/bin/bash
########################################
# This bash script reads each video file
# calculates the cumulative duration,
# and generates the timestamps for
# YouTube chapters:
########################################
# Initialize total_duration in seconds
total_duration=0
# Print the initial chapter
echo "00:00:00 Introduction"
# Loop through each video file except the last
for file in *.mp4; do
# Get the duration of the current video in seconds
duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file")
duration=${duration%.*} # Convert to integer (drop decimal part)
# Add duration of the current video to the total duration
total_duration=$((total_duration + duration))
# Print the next chapter start time
printf "%02d:%02d:%02d Next Chapter\n" $((total_duration / 3600)) $(((total_duration / 60) % 60)) $((total_duration % 60))
# Add 1 second for the black screen
total_duration=$((total_duration + 1))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment