This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Easy_Captions.py
175 lines (137 loc) · 5.05 KB
/
Easy_Captions.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from time import sleep
import speech_recognition as sr
import os
from set_path import setPath
import subprocess
from pydub import AudioSegment
from pydub.silence import split_on_silence
from termcolor import cprint
from pyfiglet import figlet_format
from clearscreen import clear
# -------functions-&-Vriables--initialized----------
r=sr.Recognizer()
setPath("Easy-Captions","bin")
cprint(f"Current Working Directory Set --> {os.getcwd()}",'green')
captions = []
raw_text = ""
video_name = ""
step = -1
# -----------Functions--Defined-----------
def recognize_Audio():
try:
global raw_text,step,captions
os.chdir("../temp")
cprint(f"Working in -> {os.getcwd()}",'green')
for all_temp in range(chunks_count):
with sr.AudioFile(f"chunk{all_temp}.mp3") as source:
audio_data = r.record(source)
try:
raw_text = r.recognize_google(audio_data)
cprint(f"Captured data : {raw_text}",'blue')
except:
cprint("\n\t\tError !",'red')
captions.append(raw_text)
os.remove(f"chunk{all_temp}.mp3")
cprint(f"Path of your caption file : {video_name[:-4]}.srt",'green')
except:
cprint("File Not Found ! or Invalid Path","red")
def split(filepath):
try:
global chunks_count
sound = AudioSegment.from_wav(filepath)
dBFS = sound.dBFS
chunks = split_on_silence(sound,
min_silence_len = 400,
silence_thresh = dBFS-37,
)
chunks_count = len(chunks)
os.chdir("../temp")
for i in range(chunks_count):
cprint(chunks[i],'red')
cprint("Exporting chunk{0}.mp3.".format(i),'green')
chunks[i].export(
".//chunk{0}.mp3".format(i),
bitrate = "192k",
format = "wav"
)
os.remove("../bin/con_audio.wav")
except:
cprint("File Not Found ! or Invalid Path","red")
def extract_Audio(video):
try:
global video_name
cprint(f"Working in -> {os.getcwd()}",'green')
command = f"ffmpeg -i {video} -ab 160k -ac 2 -ar 44100 -vn con_audio.wav"
subprocess.call(command, shell=True)
video_name = video
except:
cprint("File Not Found ! or Invalid Path","red")
exit(0)
def save_Caption_File():
global step
os.chdir('../Export')
# --------Caption-Specific-Variables--------
s_hrs = 0
s_mins = 0
s_secs = 0
e_hrs = 0
e_mins = 0
e_secs = 0
mili_Secs = 0
for i in range(len(captions)):
length = len(captions[i].split(" "))
start_Time = f"{s_hrs}:{s_mins}:{s_secs},{mili_Secs+350}"
end_Time = f"{e_hrs}:{e_mins}:{e_secs+5},{mili_Secs}"
if 9 < length <= 11:
start_Time = f"{s_hrs}:{s_mins}:{s_secs},{mili_Secs+350}"
end_Time = f"{e_hrs}:{e_mins}:{e_secs+9},{mili_Secs}"
elif 3 <= length <= 8:
start_Time = f"{s_hrs}:{s_mins}:{s_secs},{mili_Secs+350}"
end_Time = f"{e_hrs}:{e_mins}:{e_secs+3},{mili_Secs}"
elif 12 <= length <= 16:
start_Time = f"{s_hrs}:{s_mins}:{s_secs},{mili_Secs+350}"
end_Time = f"{e_hrs}:{e_mins}:{e_secs+5},{mili_Secs}"
elif 1 <= length <= 2:
start_Time = f"{s_hrs}:{s_mins}:{s_secs},{mili_Secs+350}"
end_Time = f"{e_hrs}:{e_mins}:{e_secs+1},{mili_Secs}"
elif 17 < length :
start_Time = f"{s_hrs}:{s_mins}:{s_secs},{mili_Secs+350}"
end_Time = f"{e_hrs}:{e_mins}:{e_secs+10},{mili_Secs}"
step+=1
e_secs+=3
s_secs = e_secs
try:
your_File = f"{video_name[0:-4]}.srt"
caption_Pattern = f"{step}\n{start_Time} --> {end_Time}\n{captions[i]}\n\n"
with open(your_File,"a") as ca:
ca.writelines(caption_Pattern)
ca.close()
except:
cprint("File Not Found ! or Invalid Path","red")
setPath("Easy-Captions","temp")
def controls():
cprint(figlet_format("Easy Captions"),'blue')
print("\n\t1 - Generate Captions")
print("\t2 - Exit")
try:
usr_Choice = int(input("Type Here - "))
if usr_Choice == 1:
video_name = input("Enter Video Path : ")
extract_Audio(video_name)
split("con_audio.wav")
recognize_Audio()
save_Caption_File()
elif usr_Choice == 2:
cprint(figlet_format("Thanks You"),'green')
sleep(2)
exit(0)
else:
cprint("Choose from above options only !","red")
clear()
controls()
except ValueError:
cprint("Choose from above options only !","red")
clear()
controls()
if __name__ == "__main__" :
controls()