Split MKV based on Chapters
I have a long MKV file which I want to split into its individual chapters.
Running ffmpeg -i long.mkv
gives me all the information about the chapters embedded in the file:
Duration: 01:23:45.80, start: 0.000000, bitrate: 8116 kb/s
Chapter #0.0: start 0.000000, end 235.000000
Metadata:
title : Chapter 01
Chapter #0.1: start 235.000000, end 450.160000
Metadata:
title : Chapter 02
Chapter #0.2: start 450.160000, end 789.400000
...
There are 10 chapters in the file - I want to end up with 10 separate files.
It looks like -map_chapters
might to something similar - but I can't find any documentation on it.
split mkv video by chapters using mkvmerge
mkvmerge -o output.mkv --split chapters:all input.mkv
https://www.bunkus.org/videotools/mkvtoolnix/doc/mkvmerge.html
I can't find a reliable way to do this with ffmpeg / avconv - but I can find a way to do this with HandBrakeCLI.
HandBrakeCLI -c 3 -i whatever.mkv -o 3.mkv
Will extract chapter 3 from an mkv
.
brute force solution, hehe:
ffmpeg -i long.mkv | grep 'start.*end.*[0-9]*' | sed -r 's/.*#[0-9]\.([0-9]*).* ([0-9]*\.[0-9]*).*( [0-9]*\.[0-9]*)/ ffmpeg -i long.mkv -ss \2 -to\3 -acodec copy -vcodec copy chapter\1.mkv/g;'
You can add xargs to run the output in cowboy style:
| xargs -I cmd bash -c 'cmd'