Created
December 15, 2022 09:05
-
-
Save KorigamiK/dfde82468cee04be2076194222a24f41 to your computer and use it in GitHub Desktop.
Create video chapter metadata script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Create a Metadata file with chapters for a Video | |
Usage: | |
* Place a `chapter.txt` file in the same directory as the video file. | |
with the following format: | |
00:00:00 Chapter 1 | |
00:00:00 Chapter 2 | |
... | |
* Running the script will create a `metadata.txt` file in the same directory | |
`python metadata.py` | |
* The `metadata.txt` file can be used with `ffmpeg` to add chapters to the Video | |
`ffmpeg -i input.mp4 -i metadata.txt -map_metadata 1 -c copy output.mp4` | |
""" | |
import re | |
chapters = list() | |
with open("chapters.txt", "r") as f: | |
for line in f: | |
x = re.match(r"(\d):(\d{1,2}):(\d{2}) (.*)", line) | |
assert x is not None | |
hrs = int(x.group(1)) | |
mins = int(x.group(2)) | |
secs = int(x.group(3)) | |
title = x.group(4) | |
minutes = (hrs * 60) + mins | |
seconds = secs + (minutes * 60) | |
timestamp = seconds * 1000 | |
chap = {"title": title, "startTime": timestamp} | |
chapters.append(chap) | |
text = "" | |
chapter_format = """ | |
[CHAPTER] | |
TIMEBASE=1/1000 | |
START={} | |
END={} | |
title={} | |
""" | |
for i in range(len(chapters) - 1): | |
chap = chapters[i] | |
title = chap["title"] | |
start = chap["startTime"] | |
end = chapters[i + 1]["startTime"] - 1 | |
try: | |
assert end > start | |
text += chapter_format.format(start, end, title) | |
except AssertionError: | |
print(f"Skipping {title} because end time is before start time") | |
with open("metadata.txt", "a") as myfile: | |
myfile.write(text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment