Skip to content

Instantly share code, notes, and snippets.

@spajak
Last active December 7, 2022 08:44
Show Gist options
  • Save spajak/1462566cdf6a60319882fbdab72ae554 to your computer and use it in GitHub Desktop.
Save spajak/1462566cdf6a60319882fbdab72ae554 to your computer and use it in GitHub Desktop.
Simple OGM chapters generator for MKVToolNix
# Simple OGM chapters generator for MKV Video
# -------------------------------------------
# Use it like this:
# $ python ogm-chapters.py times.txt [chapters.txt]
# Where times.txt contains timestamps separated by a whitespace or comma
import sys
from pathlib import Path
import re
prefix = 'Chapter '
if len(sys.argv) <= 1:
print('Input file not specified')
sys.exit(1)
inputFileName = Path(sys.argv[1])
if not inputFileName.is_file():
print('Input file does not exist')
sys.exit(1)
outputFile = sys.stdout
if len(sys.argv) > 2:
outputFile = open(Path(sys.argv[2]), 'w', encoding='utf-8')
with open(inputFileName, 'r', encoding='utf-8') as inputFile:
times = re.split(r'[\s,]+', inputFile.read())
i = 0
digits = len(str(len(times)))
for t in times:
t = t.strip()
if re.fullmatch(r'[0-9.:]{2,}', t):
outputFile.write(f'CHAPTER{i:0{digits}d}={t}\n')
outputFile.write(f'CHAPTER{i:0{digits}d}NAME={prefix}{i+1:0{digits}d}\n')
i += 1
if outputFile != sys.stdout:
outputFile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment