Created
February 15, 2022 06:36
-
-
Save aifreedom/cad44f3866480ca6b46bba71d5d73132 to your computer and use it in GitHub Desktop.
Fix the invalid timestamp strings in the downloaded danmaku ass files
This file contains 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
# Fixes the invalid timestamp string in the downloaded danmaku ass files | |
# from Downkyi (https://github.com/leiurayer/downkyi). A pull request (https://github.com/leiurayer/downkyi/pull/219) | |
# has been opened on the repo. This script can be used to fix the downloaded | |
# ass files before it is merged. | |
# | |
# The script has only been tested with limited cases and provided AS-IS. | |
# The author will NOT be responsible for any data loss for using the script. | |
# | |
# Usage: python ./fix_downkyi_ass.py <paths_to_ass_folder> | |
import sys | |
import os | |
import os.path | |
import re | |
DIALOGUE_LINE = re.compile(r'Dialogue: ((?:.*?,){9})(.*)') | |
TIMESTAMP = re.compile(r'([0-9]{1,2}):([0-9]+):([0-9]{2})\.([0-9]{2})') | |
def fix_ass_timestamp(path): | |
print(f'Processing "{path}"...') | |
if os.path.isdir(path): | |
print(f'{path} is a folder. Will process ass files directly in it.') | |
for f in os.listdir(path): | |
full_file_path = os.path.join(path, f) | |
if f.endswith('.ass') and os.path.isfile(full_file_path): | |
fix_ass_timestamp(full_file_path) | |
return | |
if os.path.isfile(path): | |
if not path.endswith('.ass'): | |
print('Unsupported file extension.') | |
return | |
buffer = [] | |
with open(path, 'r', encoding='utf-8') as input: | |
for l in input: | |
l = l.strip() | |
match = DIALOGUE_LINE.fullmatch(l) | |
if not match: | |
buffer.append(l) | |
continue | |
( | |
layer, | |
start, | |
end, | |
style, | |
name, | |
margin_l, | |
margin_r, | |
margin_v, | |
effect, | |
) = match.group(1).strip()[:-1].split(',') | |
text = match.group(2) | |
new_start = fix_timestamp(start) | |
new_end = fix_timestamp(end) | |
buffer.append( | |
f'Dialogue: {layer},{new_start},{new_end},{style},{name},{margin_l},{margin_r},{margin_v},{effect},{text}' | |
) | |
with open(path, 'w', encoding='utf-8') as output: | |
output.write('\n'.join(buffer)) | |
print('Done.') | |
def fix_timestamp(time_str): | |
match = TIMESTAMP.fullmatch(time_str) | |
if not match: | |
print(f'Invalid timestamp: {time_str}') | |
raise Exception('Error in matching timestamp string.') | |
h, m, s, f = match.groups() | |
new_m = int(m) % 60 | |
return f'{h}:{new_m:02d}:{s}.{f}' | |
if __name__ == '__main__': | |
paths = sys.argv[1:] | |
for path in paths: | |
fix_ass_timestamp(path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment