This repository has been archived by the owner on Apr 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
70 lines (57 loc) · 3.01 KB
/
convert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# will return converted format as a list
def convert_to_required_format(path):
try:
chapters = []
with open(path, 'r') as f:
content = f.read()
if len(content) > 0:
from string import printable
for line in content.split('\n'):
# extracting content
line = line.strip().split()
if len(line) > 1:
timestamp = line[0]
# handle unnecessary non ascii characters and replace it with a space
for i in timestamp:
if i not in "0123456789:":
timestamp = timestamp.replace(i, '')
timestamp = timestamp.split(':')
# convert to hrs, mins and hrs
if len(timestamp) == 3:
hours = int(timestamp[0])
minutes = int(timestamp[1])
secs = int(timestamp[2])
elif len(timestamp) == 2:
hours = 0
minutes = int(timestamp[0])
secs = int(timestamp[1])
# calculating the required format time
minutes = (hours * 60) + minutes
seconds = secs + (minutes * 60)
final_time = (seconds * 1000)
title = ' '.join(line[1:])
print(f"{ final_time } : { title }")
chapter = {
"title": title,
"start_time": final_time
}
chapters.append(chapter)
else:
print("\nFile is empty!\n")
for i in range(len(chapters)-1):
if not chapters[i]["start_time"] < chapters[i+1]["start_time"]:
print(f'\n\nThere is some timing problem with the timestamps text file.\nProbably the timestamp order is wrong or of wrong format!\nProblem after the \'{chapters[i]["title"]}\' title!')
print("trying to sort the times!")
chapters.sort(key=lambda x : x.get("start_time"))
for i in range(len(chapters)-1):
if not chapters[i]["start_time"] < chapters[i+1]["start_time"]:
print(f'\n\nThere is some problem with the timestamps text file.\nNot able to sort and fix! Sorting didn\'t fix\nProblem after the \'{chapters[i]["title"]}\' title!')
break
else:
print("\nSorting the timestamps worked!")
return chapters
else:
print("\n\nChapters returned")
return chapters
except Exception as e:
print("\nSomething is not right with the timestamps text file!\nProbably the timestamps are not in right format!\nerror:",e)