# Create FFMETADATA files. Run this first. import os import re import glob import subprocess header_template = ''';FFMETADATA1 title={title} artist=OMSCS 6200 ''' chapter_template = """ [CHAPTER] TIMEBASE=1/1000 START={start} END={end} title={title}""" import contextlib import os base_dir = os.getcwd() dirs = [os.path.join(base_dir, d) for d in next(os.walk('.'))[1] if d.startswith('P') and 'subtitles' not in d] for dir in dirs: print(dir) os.chdir(dir) with open(os.path.join(dir, 'FFMETADATAFILE'), 'wt') as metadata_file: lecture = re.sub(r'[ _]+', ' ', dir) print(header_template.format(title=lecture), file=metadata_file) files = glob.glob('*.mp4') files.sort(key=lambda n: int(n.split('_')[0])) start = 0 for f in files: title = re.match(r'\d+_-_(.*)\.mp4', f).group(1).replace('_', ' ') p = subprocess.run(['ffprobe', '-i', f, '-show_entries', 'format=duration', '-v', 'quiet', '-of', 'csv=p=0'], check=True, capture_output=True) duration = int(float(p.stdout.strip()) * 1000) end = start + duration print(chapter_template.format(start=start, end=end, title=title), file=metadata_file) start = end + 1