How to Add Chapters to a Video using FFMPEG - Part One - The Basics

Sun Aug 01 2021Uncategorized

This is the first post in the series involving the creation of chapters using FFMPEG.

  1. First of all, let's use FFMPEG itself create a 6 minute long test video:
ffmpeg -f lavfi -i color=c=red@0.2:duration=360:s=1280x720:r=10 6min720p.mp4
  1. Next, create a file called metadata.txt and add the following contents to it:
title=Adding Chapters using FFMPEG
artist=Terry Butler

# Chapter 1 starts at 00:00
# Chapter 1 ends at 02:30 (minus 1 millisecond)
title=Chapter 1

# Chapter 2 starts at 02:30
# Chapter 2 ends at 04:45 (minus 1 millisecond)
title=Chapter 2

# Chapter 3 starts at 04:45
# Chapter 3 / end of file ends at 06:00 (minus 1 millisecond)
title=Chapter 3

It's worth noting that the START and END times are based on milliseconds in this case so you must multiply seconds * 1000.

Also the END of a chapter must be one millisecond less than the START of the next. This is to ensure no overlapping boundaries.

  1. Finally we use FFMPEG to read the video in along with the metadata we created in the previous step (metadata.txt) to create a brand new outfile file complete with new chapters:
ffmpeg -i ./6min720p.mp4 -i metadata.txt -map_metadata 1 -codec copy ./6min720p-output.mp4
  1. Opening the file in a media player that supports chapters should show the following:

mpc be chapters added with ffmpeg

See here for further reading:

GatsbyNetlifyReactTypeScriptTailwind CSS
© Copyright 2008-2021 Terry Butler