Skip to content

Instantly share code, notes, and snippets.

@itsjef
Created April 20, 2023 17:15
Show Gist options
  • Save itsjef/4a28583b3dce1c238b1118966cca8f86 to your computer and use it in GitHub Desktop.
Save itsjef/4a28583b3dce1c238b1118966cca8f86 to your computer and use it in GitHub Desktop.
"""
from:
1 OVERVIEW AND GENERAL CONCEPTS - 0:00
2 ENTRIES - 12:12
STANDING OPPONENT - 13:33
KNEELING OPPONENT - 20:31
BRINGING IT TOGETHER - 28:12
to:
CHAPTER01=00:00:00.000
CHAPTER01NAME=1 OVERVIEW AND GENERAL CONCEPTS
CHAPTER02=00:12:12.000
CHAPTER02NAME=2 ENTRIES
CHAPTER03=00:13:33.000
CHAPTER03NAME=STANDING OPPONENT
CHAPTER04=00:20:31.000
CHAPTER04NAME=KNEELING OPPONENT
CHAPTER05=00:28:12.000
CHAPTER05NAME=BRINGING IT TOGETHER
ref: https://mkvtoolnix.download/doc/mkvmerge.html#mkvmerge.chapters
"""
def label_to_title_timestamp(label):
title, timestamp = label.strip().rsplit(' - ', 1)
timestamp = timestamp.split(':')
if len(timestamp) < 3:
h, m, s = '00', *timestamp
else:
h, m, s = timestamp
h = f'{h:0>2}'
m = f'{m:0>2}'
s = f'{s:0>2}' + '.000'
timestamp = f"{h}:{m}:{s}"
return title, timestamp
def to_chapters(ttlist):
chapters = []
for idx, (title, timestamp) in enumerate(ttlist):
prefix = f'CHAPTER{idx+1:02}'
chapters.append(prefix + '=' + timestamp)
chapters.append(prefix + 'NAME=' + title)
return chapters
def file_to_chapters(path):
with open(path) as f:
labels = f.readlines()
title_and_timestamp_list = list(map(label_to_title_timestamp, labels))
chapters = to_chapters(title_and_timestamp_list)
return chapters
def chapters_to_file(chapters, path):
with open(path, 'w') as f:
for line in chapters:
f.write(line + '\n')
if __name__ == '__main__':
for idx in range(7):
input_path = f'{idx+1}.txt'
output_path = 'vol-' + input_path
chapters = file_to_chapters(input_path)
chapters_to_file(chapters, output_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment