1

I have some videos encoded from The Criterion Collection, and their videos use these things called "chapter atoms". What is really odd is that ffprobe does not detect these. I assume that chapter atoms are a H.264/H.265/MP4/MOV thing, but Matroska video containers probably have their own kind of chapter metadata, right? Not sure...

For example, when I use ffprobe -show_chapters <video file>, it only gives me one chapter, like this:

[CHAPTER]
id=0
time_base=1/1000000000
start=7235652000000
start_time=7235.652000
end=7235652000000
end_time=7235.652000
TAG:title=XXXXXX
[/CHAPTER]

But when I open it with MKVToolNix GUI and use their "Info tool", I get a collapsable menu item at the very bottom that says "Chapters", and I expand that into "Edition entry", it has things like "Edition flag hidden", "Edition flag default", "Edition UID", and then finally the multiple amounts of "Chapter atoms". These are things that ffmpeg/ffprobe cannot detect within normal means.

Anyway, now to the problem I'm trying to solve. The script with the ffmpeg command line that I'm using to try to preserve all data, except for the subtitles, is as follows:

#!/bin/sh

if [ $# -eq 0 ]; then
    >&2 echo "Error: No filename(s) specified."
    exit 1
fi

# TODO: Sometimes it does not copy over chapter info
for f in "$@"; do
    ffmpeg -i "$f" -threads 4 \
    -c:v copy -c:a copy -sn \
    -map 0 -map_metadata 0 -map_chapters 0 \
    -movflags use_metadata_tags \
    "${f%.*}-subs-removed.${f##*.}"
done

The reason why I'm doing this is because OCD mostly. I've always preferred my subtitles to be separate. There's various reasons for that. Sometimes there's errors or mistakes in the subtitles that I'd like to fix, imprecise formatting problems, OCR conversion mistakes, removing annoying third-party Website Copyright/watermarks in the subtitles, adjusting subtitle timing, syncing and duration, easier for people to translate to other languages when redistributing, converting from one subtitle format to another, etc, etc...

I feel like I've done all that I can to try to preserve all data from one video file to another, including metadata. But it doesn't seem like FFmpeg is a competent enough tool to do exactly what I want it to do, since it keeps missing some custom tags and metadata in the file that should really otherwise be copied over.

So is ffmpeg the wrong tool for the job? It seems like mkvextract from mkvtoolnix is able to detect and extract these chapter atoms, but ffmpeg is not. Why is that?

Maybe there is a way to convert chapter atoms into regular Matroska video chapters? I'd like something more automated though since I have a few shell scripts ffmpeg-subs-remove.sh and ffmpeg-subs-extract.sh, the the one with subtitle removal is the snippet of code that I posted above, and it would be really great to get that working properly again.

1 Answer 1

0

This isn't really a solution for FFMpeg, but this can work along with MKVToolNix.

#!/bin/sh

if [ $# -eq 0 ]; then
    >&2 echo "Error: No filename(s) specified."
    exit 1
fi

for f in "$@"; do
    destfile="${f%.*}-subs-removed.${f##*.}"
    ffmpeg -i "$f" -threads 4 \
    -c:v copy -c:a copy -sn \
    -map 0 -map_metadata 0 -map_chapters 0 \
    -movflags use_metadata_tags+faststart \
    "$destfile"

    # We need to do more post-processing with mkvtoolnix because ffmpeg
    # is not an advanced enough tool yet to copy over all MKV container data
    if mkvinfo "$f" > /dev/null 2>&1; then
        tagsfile="/tmp/mkv-tags.xml"
        chaptersfile="/tmp/mkv-chapters.xml"
        echo "Notice: Matroska container format detected. Copying over extra data."
        mkvextract -f "$f" tags "$tagsfile"
        mkvextract -f "$f" chapters "$chaptersfile"
        if [ -r "$tagsfile" ]; then
            mkvpropedit "$destfile" --tags all:"$tagsfile"
            rm -f "$tagsfile"
        fi
        if [ -r "$chaptersfile" ]; then
            mkvpropedit "$destfile" --chapters "$chaptersfile"
            rm -f "$chaptersfile"
        fi
    fi
done

I haven't tested this with multiple files yet, and there's probably other test cases I'm missing. Not perfect, but it works for me for now.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .