Skip to content

Instantly share code, notes, and snippets.

@ArjixWasTaken
Last active February 3, 2021 14:16
Show Gist options
  • Save ArjixWasTaken/c0226796b67b2015fa8fa68a8c91f665 to your computer and use it in GitHub Desktop.
Save ArjixWasTaken/c0226796b67b2015fa8fa68a8c91f665 to your computer and use it in GitHub Desktop.
from bs4 import BeautifulSoup # pip install bs4
import re
with open(r"PATH/TO/CHAPTERS/XML/FILE.xml", 'r') as f:
soup = BeautifulSoup(f.read(), 'html.parser')
chapters = soup.select('editionentry > chapteratom')
chapters_array = []
for chapter in chapters:
time = re.search(r'(\d{2}):(\d{2}):(\d{2})', str(chapter))
hrs = int(time.group(1))
mins = int(time.group(2))
secs = int(time.group(3))
minutes = (hrs * 60) + mins
seconds = secs + (minutes * 60)
timestamp = (seconds * 1000)
chap = {
"title": re.sub(r'(=|;|#|\n)', r'\\\1', chapter.find('chapterstring').text),
"startTime": timestamp
}
chapters_array.append(chap)
text = ";FFMETADATA1\n\n"
for i in range(len(chapters_array)-1):
chap = chapters_array[i]
title = chap['title']
start = chap['startTime']
end = chapters_array[i+1]['startTime']-1
text += f"\n[CHAPTER]\nTIMEBASE=1/1000\nSTART={start}\nEND={end}\ntitle={title}\n"
text = text.strip()
print(text)
@ArjixWasTaken
Copy link
Author

ArjixWasTaken commented Feb 3, 2021

this script takes a matroska (xml) chapter file and converts it to an FFMPEG METADATA file
for more info look at

https://ffmpeg.org/ffmpeg-formats.html#Metadata-1

and

https://www.matroska.org/technical/chapters.html

@ArjixWasTaken
Copy link
Author

to add the chapters to an mp4
ffmpeg -i input.mp4 -i FFMETA.txt -map_metadata 1 -map_chapters 1 -codec copy output.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment