Updated formatting & removed redundancies

This commit is contained in:
bender
2023-02-02 18:16:32 -05:00
parent 1d58b6f50f
commit 48fe05290a
10 changed files with 112 additions and 93 deletions
+10 -9
View File
@@ -1,4 +1,4 @@
#Confirmed working as of 1/22/23
'''Converts label durations to milliseconds for pydub to read'''
import os
import re
@@ -6,29 +6,30 @@ from datetime import datetime, timedelta
from config import annex
def duration_to_milliseconds(duration):
'''Conversion from Seconds to Milliseconds'''
hours, minutes, seconds = duration.split(':')
seconds = int(hours)*3600 + int(minutes)*60 + float(seconds)
return int(seconds * 1000)
# path to directory containing the files
'''path to directory containing the files'''
directory = annex
# loop through all files in directory tree
'''loop through all files in directory tree'''
for dirpath, dirnames, filenames in os.walk(directory):
for filename in filenames:
if filename.startswith("overdrive_chapters") and filename.endswith(".txt"):
# open the file and read the contents
'''open the chapters file and read the contents'''
with open(os.path.join(dirpath, filename), 'r') as f:
lines = f.readlines()
# create a new file with the same name but with "milliseconds" added to the end
'''create a new file with the same name but with "ms" added to the end'''
new_filename = filename[:-4] + "_ms.txt"
with open(os.path.join(dirpath, new_filename), 'w') as new_file:
# loop through each line in the file
'''loop through each line in the file'''
for line in lines:
# split the line into duration and label using regular expression
'''split the line into duration and label name using regular expression'''
duration, label = re.split(r"\s+", line.strip(), 1)
# convert the duration to milliseconds
'''convert the duration to milliseconds'''
milliseconds = duration_to_milliseconds(duration)
# write the converted duration and label to the new file
'''write the converted duration and label to the new file'''
new_file.write(f'{milliseconds} {label}\n')