Extract Video Chapters with FFmpeg on Linux

This article provides a straightforward guide on how to extract chapter information from a video file using FFmpeg on Linux. You will learn how to inspect metadata to view chapters directly in the terminal, export them to a portable text file, and convert them into the FFmpeg metadata format for advanced editing.

Viewing Chapters in the Terminal

Before exporting any data, you can quickly verify and view the embedded chapters of a video file using ffprobe, which comes bundled with FFmpeg. Running the following command parses the video container and displays the chapter timestamps and titles:

ffprobe -i input.mp4 -show_chapters -print_format compact

If you prefer a cleaner, human-readable overview without the technical clutter, you can filter the output using grep or awk:

ffmpeg -i input.mp4 2>&1 | grep -E "(Chapter|Stream)"

Exporting Chapters to a Text File

To extract the chapter details into a clean, readable text format, you can leverage ffprobe alongside the jq utility (a command-line JSON processor). This method allows you to isolate the start times, end times, and chapter tags, and then save them directly to a file.

ffprobe -i input.mp4 -print_format json -show_chapters -v quiet > chapters.json

If you prefer a simple flat text file detailing just the timestamps and names, you can format the JSON output like this:

jq -r '.chapters[] | "\(.tags.title): \(.start_time) - \(.end_time)"' chapters.json > chapters.txt

Extracting into FFmpeg Metadata Format

If your goal is to extract the chapters so you can edit them or remux them into a different video file, you should export them into FFmpeg’s native metadata format. This creates a text file that preserves the exact layout required by FFmpeg for injections.

Run the following command to isolate and dump the metadata:

ffmpeg -i input.mp4 -f ffmetadata metadata.txt

When you open the resulting metadata.txt file, you will see sections structured with timebase scales and precise start and end integers:

[CHAPTER]
TIMEBASE=1/1000
START=0
END=295000
title=Introduction

Reapplying Extracted Chapters to a New Video

Once you have extracted and modified your metadata.txt file, you can easily inject those chapters back into the original video or a completely new video file without re-encoding the streams:

ffmpeg -i input.mp4 -i metadata.txt -map_metadata 1 -c copy output.mp4