#!/usr/bin/python3 import ox import os import json import sys import re api = ox.api.signin("https://power.0x2620.org/api/") prefix = sys.argv[1] def fix_timecode(match): ts, ms = match[0].split(',') seconds = int(ms)/1000 for i, v in enumerate(list(reversed(ts.split(':')))): seconds += float(v) * pow(60, i) return ox.format_duration(seconds * 1000, years=False).replace('.', ',') def fix_timecodes(srt_data): reg = re.compile(r'(\d\d:\d\d:\d\d,\d\d\d\d)') old = srt_data srt_data = re.sub(reg, fix_timecode, srt_data) if srt_data != old: rows1 = old.split('\n') rows2 = srt_data.split('\n') for n, row in enumerate(rows1): if rows1[n] != rows2[n]: print(rows1[n], '->', rows2[n]) return srt_data for root, folders, files in os.walk(prefix): for file in sorted(files): if file.endswith('.mp3'): print(file) chapter = '%02d' % int(file.split('-')[0].replace('ch', '')) file = os.path.join(root, file) srt = file.replace('.mp3', '_eng.srt') if not os.path.exists(srt): name = os.path.basename(srt) srt = srt.replace(name, name.replace(' ', '_')) with open(srt) as fd: srt_data = fd.read() srt_data = fix_timecodes(srt_data) subs = ox.srt.loads(srt_data) oshash = ox.oshash(file) url = '%supload/direct/' % api.url avinfo = ox.avinfo(file) r = api.addMedia({ 'id': oshash, 'filename': os.path.basename(file), 'info': avinfo }) id = r['data']['item'] if api.upload_chunks(url, file, {'id': oshash}): info = { "id": id, "type": 'voice over', "chapter": [chapter] } api.edit(info) for sub in subs: sub['value'] = sub['value'].replace('\n', '
\n') api.addAnnotations({ "item": id, "layer": "subtitles", "annotations": subs })