日本語

Adding Chapters to a Video with FFmpeg

​​FFmpeg​command line,mpv,video

In FFmpeg, you can add chapters without re-encoding the video. Write the start and end times and the names of the chapters in a text file, then load that file together with the video. For long videos such as recorded lectures, streams, or tutorials, a table of contents like "Opening" and "Main Part" lets you jump to the scene you want in a player that supports chapters.

In my test environment, I added three chapters to a 30-second video and checked the result with ffprobe and mpv. I also found that when a video already has chapters, -map_chapters is needed to replace them, and that forgetting to write the time unit makes the chapters come out wrong.

Note

The test environment was Ubuntu 24.04.5, FFmpeg 6.1.1 (Ubuntu package 6.1.1-3ubuntu5), and mpv 0.37.0. The source video was a 30-second, 640×360, 30fps H.264 video with mono AAC audio of a 440 Hz sine wave, in an MP4 file. The elapsed time is drawn in the upper left corner. I used only mpv to check the chapters. I did not test how other players or video streaming services display them, or formats other than MP4 and MKV, such as WebM or MOV.

Write chapters in a text file

FFmpeg has a dedicated format for metadata (information such as the title) and chapters. According to the description of the metadata file format in the official FFmpeg documentation, you write ;FFMETADATA1 on the first line and create a [CHAPTER] section for each chapter. Each section contains the time base (TIMEBASE), the start (START), the end (END), and the name (title).

To make 0–8 seconds "Opening", 8–21.5 seconds "Main Part", and 21.5–30 seconds "Summary", write the following. Here I saved it as chapters.txt.

ini
;FFMETADATA1
title=チャプターの検証

[CHAPTER]
TIMEBASE=1/1000
START=0
END=8000
title=オープニング

[CHAPTER]
TIMEBASE=1/1000
START=8000
END=21500
title=本編

[CHAPTER]
TIMEBASE=1/1000
START=21500
END=30000
title=まとめ

TIMEBASE=1/1000 means that the values of START and END are written in units of 1/1000 second (milliseconds). So 8 seconds is written as 8000, and 21.5 seconds as 21500. The title= on the second line is the title of the whole video, not a chapter. If you don't need it, you can leave it out.

Load the video and the chapter file together

The command to add the chapters is as follows.

bash
ffmpeg -i input.mp4 -i chapters.txt -map_metadata 1 -codec copy output.mp4

-i loads the video (input 0) and chapters.txt (input 1). -map_metadata 1 takes the video-wide information, such as the title, from input 1. -codec copy copies the video and audio without re-encoding them. This form is the same as the example in the metadata file format description that writes metadata back.

When I checked the output with ffprobe, it contained three chapters.

bash
ffprobe -v error -show_chapters -of compact=p=0 output.mp4
text
id=0|time_base=1/1000|start=0|start_time=0.000000|end=8000|end_time=8.000000|tag:title=オープニング
id=1|time_base=1/1000|start=8000|start_time=8.000000|end=21500|end_time=21.500000|tag:title=本編
id=2|time_base=1/1000|start=21500|start_time=21.500000|end=30000|end_time=30.000000|tag:title=まとめ

The file size went from 3,697,631 bytes to 3,698,438 bytes, an increase of only 807 bytes.

I opened the file in mpv and displayed the chapter list (chapter-list) at the 10-second position. The screenshot is below. The arrow marks the current chapter, Main Part. The "Summary" chapter, which starts at 21.5 seconds, appears as 00:00:21 in mpv's list because the fractional seconds are cut off.

Center of the mpv screen showing three chapter lines: 00:00:00 (1) Opening, 00:00:08 (2) Main Part, 00:00:21 (3) Summary, with an arrow on the Main Part line. The labels in the image are in Japanese.
mpv screen at the 10-second position, showing the chapter list

I opened mpv at the 0-second position and sent the "next chapter" command (add chapter 1) twice over IPC. The playback position moved to 8.000 seconds (Main Part), and then to 21.500 seconds (Summary).

Even without -map_metadata 1, the same three chapters were added. However, the video-wide title written in chapters.txt (チャプターの検証) was not added. Chapters and video-wide information such as the title are handled separately.

In MP4, chapters are written in two forms

When I listed the contents with ffprobe, there was a third stream besides the video and audio, which was not in the original video. Here is part of the output.

text
  Chapters:
    Chapter #0:0: start 0.000000, end 8.000000
      Metadata:
        title           : オープニング
(略)
  Stream #0:2[0x3](eng): Data: bin_data (text / 0x74786574), 0 kb/s
    Metadata:
      handler_name    : SubtitleHandler
Unsupported codec with id 98314 for input stream 2

The last line is a message, but all three chapters could still be read.

According to the description of the disable_chpl option in the official documentation of the MP4 muxer options, an MP4 is written with both a Nero-style chapter list (a box called chpl) and a chapter track in the QuickTime style. The third stream is the QuickTime-style chapter track.

When I parsed the boxes in the MP4 with a Python script, I confirmed that both were present. The video and audio tracks refer to the third track through a chap box inside tref. chpl contained the start time and name of each chapter.

text
moov/udta/chpl entries=[(0.0, 'オープニング'), (8.0, '本編'), (21.5, 'まとめ')]

When -movflags +disable_chpl is added, chpl is not written and only the chapter track remains. In this case too, ffprobe read all three chapters. According to the official description, having Nero-style chapters can cause failures when some tag-editing software, such as mp3Tag or iTunes, reprocesses the file. This option is for that case.

Copying an MP4 with chapters using -map 0 adds a track

When I copied an MP4 with chapters using -map 0, which selects all streams of input 0, the chapter track was kept as an extra track.

bash
ffmpeg -i output.mp4 -map 0 -c copy map0.mp4
text
index=0|codec_name=h264|codec_type=video|codec_tag_string=avc1
index=1|codec_name=aac|codec_type=audio|codec_tag_string=mp4a
index=2|codec_name=bin_data|codec_type=data|codec_tag_string=gpmd
index=3|codec_name=bin_data|codec_type=data|codec_tag_string=text

The original chapter track was copied as a data stream, and it became a different type of track, gpmd. In addition, a new chapter track (the fourth) was created. When I selected only the video and audio with -map 0:v -map 0:a, there were still three streams: video, audio, and the chapter track. In both cases, all three chapters remained.

In MKV, chapters do not become a track

I changed the output file name to output.mkv.

bash
ffmpeg -i input.mp4 -i chapters.txt -map_metadata 1 -codec copy output.mkv

Three chapters were added from the same chapters.txt, and there were only two streams, video and audio. When I copied this output.mkv with -map 0, it still had two streams, and all three chapters remained.

If the video already has chapters, add -map_chapters

When I used the same command to replace the chapters of a video that already had them, they were not replaced. The following is the result of adding chapters2.txt to the output.mp4 created above. In chapters2.txt, 0–15 seconds is 前半 (First Half) and 15–30 seconds is 後半 (Second Half).

bash
ffmpeg -i output.mp4 -i chapters2.txt -map_metadata 1 -codec copy replace_a.mp4
text
id=0|time_base=1/1000|start=0|start_time=0.000000|end=8000|end_time=8.000000|tag:title=オープニング
id=1|time_base=1/1000|start=8000|start_time=8.000000|end=21500|end_time=21.500000|tag:title=本編
id=2|time_base=1/1000|start=21500|start_time=21.500000|end=30000|end_time=30.000000|tag:title=まとめ

According to the description of -map_chapters in the ffmpeg command documentation, if you don't specify it, chapters are copied from the first input that has one or more chapters. In this example, the chapters of output.mp4, which is input 0, were used as they were.

When I added -map_chapters 1 to take the chapters from input 1, they were replaced with the new ones.

bash
ffmpeg -i output.mp4 -i chapters2.txt -map_metadata 1 -map_chapters 1 -codec copy replace_b.mp4
text
id=0|time_base=1/1000|start=0|start_time=0.000000|end=15000|end_time=15.000000|tag:title=前半
id=1|time_base=1/1000|start=15000|start_time=15.000000|end=30000|end_time=30.000000|tag:title=後半

So that you don't have to check every time whether the original video has chapters, it is a good idea to always include -map_chapters 1.

Export the existing chapters and fix them

If you want to make small changes to existing chapters, you can export them with -f ffmetadata. This produces a text file in the same format.

bash
ffmpeg -i output.mp4 -f ffmetadata exported.txt
ini
;FFMETADATA1
major_brand=isom
minor_version=512
compatible_brands=isomiso2avc1mp41
title=チャプターの検証
encoder=Lavf60.16.100
[CHAPTER]
TIMEBASE=1/1000
START=0
END=8000
title=オープニング
(略)

I edited this file and then added the chapters again with -map_chapters 1. I changed the name of the Main Part chapter to 本編(直した) and moved its boundary at 21.5 seconds (21500) to 20 seconds (20000). I saved the result as edited.txt. The output of the new file follows.

bash
ffmpeg -i output.mp4 -i edited.txt -map_metadata 1 -map_chapters 1 -codec copy fixed.mp4
text
id=0|time_base=1/1000|start=0|start_time=0.000000|end=8000|end_time=8.000000|tag:title=オープニング
id=1|time_base=1/1000|start=8000|start_time=8.000000|end=20000|end_time=20.000000|tag:title=本編(直した)
id=2|time_base=1/1000|start=20000|start_time=20.000000|end=30000|end_time=30.000000|tag:title=まとめ

The names and boundaries changed as I wrote them, and the video-wide title (チャプターの検証) also remained.

What happened when I wrote it wrong

I wrote START=8 and END=30 as if they were seconds, without a TIMEBASE. The command finished without an error. However, the resulting "Opening" chapter was 0–0 seconds, and "Main Part" was 0–30 seconds. As the metadata file format description says, without TIMEBASE the numbers are read as nanoseconds. The 8 in the start of "Main Part" was read as 0.000000008 seconds, which rounds to 0 seconds.

text
id=0|time_base=1/1000|start=0|start_time=0.000000|end=0|end_time=0.000000|tag:title=オープニング
id=1|time_base=1/10000000|start=0|start_time=0.000000|end=300000000|end_time=30.000000|tag:title=本編

When I forgot to write END for the first chapter, the following message appeared (I omitted the number after @).

text
[ffmetadata @ …] Expected chapter end timestamp, found title=オープニング.

Even so, ffmpeg exited with code 0 and created the video. Both chapters were named 本編 (Main Part), and the first one ran from 0 to 22 seconds, a boundary I had not written anywhere.

Warning

Even if you get TIMEBASE or END wrong, ffmpeg does not stop and writes the video. After adding chapters, check the times and names with ffprobe -show_chapters.

According to the official description, to put =, ;, #, \, or a newline in a name, you put a \ in front of it. In my test environment, writing title=第2回 \#ffmpeg a\=b\; c with the escapes as described gave the name 第2回 #ffmpeg a=b; c. Writing title=第1回 #ffmpeg a=b; c without the \ also kept the part #ffmpeg a=b; c exactly as written.

Create a file from a list of "m:ss name" entries

If you keep the chapter boundaries in a list such as 0:08 本編, a short shell script can convert it to chapters.txt. The end of each chapter is the start of the next one, and the end of the last chapter is the length of the video, which I get with ffprobe.

bash
#!/bin/bash
# usage: ./to-ffmeta.sh timestamps.txt input.mp4 > chapters.txt
# One line per chapter, as "m:ss name" or "h:mm:ss name". The last chapter ends at the length of the video.
dur=$(ffprobe -v error -show_entries format=duration -of default=nw=1:nk=1 "$2")
awk -v dur="$dur" '
  function ms(t,  a, n) { n = split(t, a, ":"); return (n == 3 ? a[1]*3600 + a[2]*60 + a[3] : a[1]*60 + a[2]) * 1000 }
  NF { s[++n] = int(ms($1) + 0.5); sub(/^[^ ]+ +/, ""); name[n] = $0 }
  END {
    print ";FFMETADATA1"
    for (i = 1; i <= n; i++) {
      e = (i < n) ? s[i+1] : int(dur * 1000 + 0.5)
      print "[CHAPTER]"; print "TIMEBASE=1/1000"; print "START=" s[i]; print "END=" e; print "title=" name[i]
    }
  }' "$1"

I created a timestamps.txt with these three lines. The script produced a file with the same times and names as the one I wrote by hand (here, chapters_gen.txt), and adding it gave the same result.

text
0:00 オープニング
0:08 本編
0:21.5 まとめ
bash
./to-ffmeta.sh timestamps.txt input.mp4 > chapters_gen.txt
ffmpeg -i input.mp4 -i chapters_gen.txt -map_chapters 1 -codec copy gen.mp4

Chapters after re-encoding and after cutting

When I re-encoded the video with chapters using -c:v libx264 -crf 28 -c:a aac, all three chapters remained at the same times.

When I cut 15 seconds starting at the 10-second position with -ss 10 -t 15 -c copy, the chapters were recalculated to match the cut range. Main Part, which starts at 8 seconds, became 0–11.5 seconds in the cut video, and Summary became 11.5–15 seconds. Opening, which is outside the range, was removed.

bash
ffmpeg -ss 10 -i output.mp4 -t 15 -c copy cut.mp4
text
id=0|time_base=1/1000|start=0|start_time=0.000000|end=11500|end_time=11.500000|tag:title=本編
id=1|time_base=1/1000|start=11500|start_time=11.500000|end=15000|end_time=15.000000|tag:title=まとめ

If the chapter boundaries after cutting are not what you want, you can export and fix them with the steps above, then add them again with -map_chapters 1.

​Posted by 86