-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscribe.py
More file actions
44 lines (35 loc) · 1.49 KB
/
Copy pathtranscribe.py
File metadata and controls
44 lines (35 loc) · 1.49 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
import time
# Imports the Google Cloud client library
from google.cloud import speech
# Import the google service account api authentication
from google.oauth2 import service_account
class Transcriber:
def __init__(self,URI):
self.credentials = service_account.Credentials.from_service_account_file(
'key.json')
self.client = speech.SpeechClient(credentials=self.credentials)
# Audio file to be transcribed from google cloud bucket
self.gcs_uri = URI
self.audio = speech.RecognitionAudio(uri=self.gcs_uri)
self.config = speech.RecognitionConfig(
encoding=speech.RecognitionConfig.AudioEncoding.FLAC,
language_code="en-US",
audio_channel_count=2,
enable_separate_recognition_per_channel=True,
)
def transcribe(self):
transcript = ''
# Detects speech in the audio file
operation = self.client.long_running_recognize(
config=self.config, audio=self.audio)
start_time = time.time()
print("Waiting for operation to complete...")
response = operation.result()
for result in response.results:
transcript += result.alternatives[0].transcript
print('Transcribing completed')
print(f'Transcription took {time.time()-start_time}seconds')
#Writing transcript into text file
print('saving transcript')
with open('transcript.txt', 'w') as file:
file.write(transcript)