How to Get Chapter Durations with FFprobe

This article explains how to extract and calculate the duration of individual chapters in video or audio files using FFprobe. Because FFprobe stores chapter boundaries as start and end timestamps rather than pre-calculated lengths, getting each chapter's duration requires extracting these timestamps and subtracting the start time from the end time. Below are the most efficient commands to list each chapter alongside its calculated duration using standard command-line tools.

Extracting Chapter Durations Using FFprobe and jq

The cleanest way to parse and calculate chapter lengths is by exporting FFprobe's data in JSON format and piping it into jq.

Run the following command:

ffprobe -v error -show_chapters -print_format json input.mkv | jq -r '
  .chapters[] | 
  "Chapter: \(.tags.title // .id) | Duration: \((.end_time | tonumber) - (.start_time | tonumber)) seconds"
'

How It Works:

  • -v error: Suppresses unnecessary diagnostic output and banners.
  • -show_chapters: Instructs FFprobe to read the chapter metadata.
  • -print_format json: Formats the output as structured JSON.
  • jq -r: Parses each chapter entry, pulls the chapter title (or ID if no title exists), and subtracts .start_time from .end_time to output the exact duration in seconds.

Extracting Chapter Durations Using CSV and awk

If you do not have jq installed, you can use FFprobe's built-in CSV writer and calculate the difference using awk.

Run the following command:

ffprobe -v error -show_entries chapter=id,start_time,end_time:chapter_tags=title -of csv=p=0 input.mkv | awk -F',' '{duration = $3 - $2; printf "Chapter ID %s (%s): %.3f seconds\n", $1, $4, duration}'

How It Works:

  • -show_entries chapter=id,start_time,end_time:chapter_tags=title: Extracts only the essential chapter fields.
  • -of csv=p=0: Outputs the data as comma-separated values without the column prefix tags (p=0).
  • awk -F',': Sets the delimiter to a comma, computes duration = $3 - $2 (end_time - start_time), and prints the formatted result.

Displaying Durations in HH:MM:SS Format

If you prefer human-readable timestamps instead of total seconds, you can format the output using a Python one-liner combined with FFprobe:

ffprobe -v error -show_chapters -print_format json input.mkv | python3 -c '
import sys, json, datetime

data = json.load(sys.stdin)
for ch in data.get("chapters", []):
    title = ch.get("tags", {}).get("title", f"Chapter {ch[\"id\"]}")
    duration = float(ch["end_time"]) - float(ch["start_time"])
    formatted = str(datetime.timedelta(seconds=round(duration)))
    print(f"{title}: {formatted} ({duration:.2f}s)")
'

This processes the metadata and returns each chapter title followed by its duration formatted as hours, minutes, and seconds.