Skip to content

Instantly share code, notes, and snippets.

@FossPrime
Forked from dcondrey/ffmpegchapters-explicit.sh
Last active August 18, 2017 16:21
Show Gist options
  • Save FossPrime/6fcf168cd57cc1d686e4d0a378956dd0 to your computer and use it in GitHub Desktop.
Save FossPrime/6fcf168cd57cc1d686e4d0a378956dd0 to your computer and use it in GitHub Desktop.
Use ffmpeg to split file by chapters. Python version and bash version
#!/bin/bash
# m4v and mp4 video, Chapter Marker splitter
# May be particularly good with final cut DVD chapter markers
# This standard seems to be popular in MP3's... cant find the official spec
# Chapter #0:0: start 0.000000, end 1290.013333
# Metadata:
# title : 3.4 - Rigging and Replacement
# http://linuxcommand.org/lc3_man_pages/readh.html
# based on
# Author: http://crunchbang.org/forums/viewtopic.php?id=38748#p414992
# m4bronto
while [ $# -gt 0 ]; do
filename=$(basename "$1")
extension="${filename##*.}"
basedir="${1%$filename}"
dir="${basedir}chapters for ${filename%.$extension}/"
mkdir "$dir"
ffmpeg -i "$1" 2> "/tmp/vidsplit.txt"
while read -r first _ _ start _ end; do
if [[ $first = Chapter ]]; then
read # discard line with Metadata:
read _ _ chapter
ffmpeg -i "$1" -ss "${start%?}" -to "$end" -codec copy -f mp4 "${dir}$chapter.$extension" </dev/null
fi
done < "/tmp/vidsplit.txt"
find "${dir}.." -type d -empty -delete
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment