-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuto_sub_creation
More file actions
342 lines (275 loc) · 10.1 KB
/
Copy pathAuto_sub_creation
File metadata and controls
342 lines (275 loc) · 10.1 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 8 23:01:01 2022
@author: sarat
"""
import pandas
import cv2
import os
import speech_recognition as sr
import os
import speech_recognition as sr
from pydub import AudioSegment
from pydub.silence import split_on_silence
import librosa
r = sr.Recognizer()
from googletrans import Translator
translator = Translator()
import datetime
import moviepy.editor as mp
from pydub import AudioSegment
from playsound import playsound
# =============================
cap = cv2.VideoCapture('')
#while(cap.isOpened()):
# ret, frame = cap.read()
# gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# cv2.imshow('frame',gray)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
# =============================================================================
# cap.release()
# cv2.destroyAllWindows()
# import os
# import speech_recognition as sr
# from pydub import AudioSegment
# from pydub.silence import split_on_silence
# r = sr.Recognizer()
#
#
# # open the file
# with sr.AudioFile('WhatsApp Ptt 2022-06-12 at 10.04.11 PM.wav') as source:
# # listen for the data (load audio to memory)
# audio_data = r.record(source, offset = 3)
# # recognize (convert from speech to text)
# text = r.recognize_google(audio_data, language="ta-IN")
# print(translator.translate(text).text)
#
# from googletrans import Translator
# translator = Translator()
# =============================================================================
# =============================================================================
#
#
# #### Counting and line
#
# audio_files = 'WhatsApp Ptt 2022-06-12 at 10.04.11 PM.wav'
# count = 0
# for audio_path in audio_files:
# audio = FLAC(audio_list[count] + '.' + output_format) #specify audio file for length calculation
# audio_length = audio.info.length #get length of audio file
#
# #n.b. mutagen module used for calculating audio length
#
# number_of_iterations = int(audio_length/2)
#
# if number_of_iterations == 0:
# number_of_iterations = 1
#
# file = sr.AudioFile(audio_list[count] + '.' + output_format)
#
#
# for i in range(number_of_iterations):
# with file as source:
# audio = r.record(source, offset = i*20, duration = 20)
#
# google = r.recognize_google(audio, language = 'ta-IN' )
# count = count + 1
# print(google)
# =============================================================================
############################################################################
dic_translation = {}
file = 'Thanmatra_audio.wav'
count = 0
while (count<348):
with sr.AudioFile('Thanmatra_audio.wav') as source:
# listen for the data (load audio to memory)
print(count)
audio_data = r.record(source, offset = count*10, duration=10)
# recognize (convert from speech to text)
try:
text = r.recognize_google(audio_data, language="ml-IN")
print(text)
print(translator.translate(text).text)
dic_translation[count] = translator.translate(text).text
except:
dic_translation[count]= ''
count = count+1
with sr.AudioFile('Thanmatra_audio.wav') as source:
audio_data = r.record(source, offset = 0*3, duration=3)
text = r.recognize_google(audio_data, language="ml-IN")
print(translator.translate(text).text)
count = 0
start_time = datetime.timedelta(seconds=0)
f = open('subtitle_thanmathra2', 'w')
while(count<=348):
print(count)
f.write((str(count)))
f.write('\n')
f.write(str(start_time))
f.write(',0')
f.write(' --> ')
f.write(str(start_time + datetime.timedelta(seconds=10)))
f.write(',0')
f.write('\n')
try:
f.write(dic_translation[count])
except:
f.write('')
f.write('\n')
f.write('\n')
count = count + 1
start_time = start_time + datetime.timedelta(seconds=10)
f.close()
with sr.AudioFile('Thanmatra_audio.wav') as source:
# listen for the data (load audio to memory)
audio_data = r.record(source, offset = 1872, duration=20)
text = r.recognize_google(audio_data, language="ml-IN")
print(translator.translate(text).text)
###########################################################################################
def get_audio(movie):
my_clip = mp.VideoFileClip(r"movie.mp4")
my_clip.audio.write_audiofile(r"traffic_audio.wav")
def movie_translation_subtitle(movie_audio_wav, length_each_translation):
dic_translation = {}
audio_length = librosa.get_duration(filename=movie_audio_wav)
count = 0
while (count<(audio_length/length_each_translation)):
with sr.AudioFile(movie_audio_wav) as source:
# listen for the data (load audio to memory)
print(count)
audio_data = r.record(source, offset = count*length_each_translation,
duration=length_each_translation)
# recognize (convert from speech to text)
try:
text = r.recognize_google(audio_data, language="ml-IN")
print(text)
print(translator.translate(text).text)
dic_translation[count] = translator.translate(text).text
except:
dic_translation[count]= ''
count = count+1
return dic_translation
def write_file(dic, file_name, length_each_translation ):
count = 0
l = len(dic)
start_time = datetime.timedelta(seconds=0)
f = open(file_name, 'w')
l = len(dic)
while(count<=l):
print(count)
f.write((str(count)))
f.write('\n')
f.write(str(start_time))
f.write(',0')
f.write(' --> ')
f.write(str(start_time + datetime.timedelta(seconds=length_each_translation)))
f.write(',0')
f.write('\n')
try:
f.write(dic_translation[count])
except:
f.write('')
f.write('\n')
f.write('\n')
count = count + 1
start_time = start_time + datetime.timedelta(seconds=length_each_translation)
f.close()
dic_translation = movie_translation_subtitle('traffic_audio.wav', 15)
write_file(dic_translation, 'traffic_subtitle', 15)
###########################################################################################
### Cutting more naturally
def silence_based_conversion(path = "traffic_audio.wav"):
# open the audio file stored in
# the local system as a wav file.
song = AudioSegment.from_wav(path)
# open a file where we will concatenate
# and store the recognized text
#fh = open("recognized.txt", "w+")
# split track where silence is 0.5 seconds
# or more and get chunks
chunks = split_on_silence(song,
# must be silent for at least 0.5 seconds
# or 500 ms. adjust this value based on user
# requirement. if the speaker stays silent for
# longer, increase this value. else, decrease it.
min_silence_len = 500,
# consider it silent if quieter than -16 dBFS
# adjust this per requirement
silence_thresh = -16
)
# create a directory to store the audio chunks.
try:
os.mkdir('audio_chunks')
except(FileExistsError):
pass
# move into the directory to
# store the audio files.
os.chdir('audio_chunks')
dic_translation = {}
audio_len_dic ={}
chunk_list=[]
i = 0
# process each chunk
for chunk in chunks:
# Create 0.5 seconds silence chunk
chunk_silent = AudioSegment.silent(duration = 10)
# add 0.5 sec silence to beginning and
# end of audio chunk. This is done so that
# it doesn't seem abruptly sliced.
audio_chunk = chunk_silent + chunk + chunk_silent
# export audio chunk and save it in
# the current directory
chunk_list.append(audio_chunk)
return chunk_list
chunk_list = silence_based_conversion("traffic_audio.wav")
def movie_translation_subtitle(chunk_list):
dic_translation = {}
length_dic = {}
count = 0
for chunk in chunk_list:
audio_length = librosa.get_duration(filename=chunk)
length_dic[count] = audio_length
with sr.AudioFile(chunk) as source:
# listen for the data (load audio to memory)
print(count)
audio_data = r.record(source).
# recognize (convert from speech to text)
try:
text = r.recognize_google(audio_data, language="ml-IN")
print(text)
translate_text = translator.translate(text).text
print(translate_text)
dic_translation[count] = translate_text
except:
dic_translation[count]= ''
count = count+1
return dic_translation, length_dic
#################### Trying to Get silence #################################
song = AudioSegment.from_wav("C:/Users/sarat/downloads/traffic_audio.wav")
fh = open("traffic_subtitle.txt", "w+")
chunks = split_on_silence(song,
# must be silent for at least 0.5 seconds
# or 500 ms. adjust this value based on user
# requirement. if the speaker stays silent for
# longer, increase this value. else, decrease it.
min_silence_len = 500,
# consider it silent if quieter than -16 dBFS
# adjust this per requirement
silence_thresh = -16
)
os.chdir('traffic_audio_chunks')
for chunk in chunks:
# Create 0.5 seconds silence chunk
chunk_silent = AudioSegment.silent(duration = 10)
# add 0.5 sec silence to beginning and
# end of audio chunk. This is done so that
# it doesn't seem abruptly sliced.
audio_chunk = chunk_silent + chunk + chunk_silent
# export audio chunk and save it in
# the current directory.
print("saving chunk{0}.wav".format(i))
# specify the bitrate to be 192 k
audio_chunk.export("./chunk{0}.wav".format(i), bitrate ='192k', format ="wav")
# the name of the newly created chunk
filename = 'chunk'+str(i)+'.wav'