-
Notifications
You must be signed in to change notification settings - Fork 2
/
transcribeAudioFile.py
65 lines (57 loc) · 2.04 KB
/
transcribeAudioFile.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
import json
import time
import requests
from pydub import AudioSegment
import os
import secrets
startTime = time.time()
witKey = secrets.witKey
filePath = secrets.filePath
file = input('Enter file name (including \'.mp3\')')
fullFilePath = filePath + file
sound = AudioSegment.from_mp3(fullFilePath)
f = open(file + '.txt', 'w')
for i in range(1, 31):
sleepTime = 5
print('waiting ' + str(sleepTime) + ' seconds')
time.sleep(sleepTime)
segmentStartTime = time.time()
split = len(sound) / 30
splitStartPoint = split * (i - 1)
splitEndPoint = split * i
if splitEndPoint > len(sound):
splitEndPoint = len(sound)
s, ms = divmod(splitEndPoint, 1000)
m, s = divmod(s, 60)
h, m = divmod(m, 60)
timeStamp = '%d:%02d:%02d.%02d' % (h, m, s, ms)
fileSegment = sound[splitStartPoint:splitEndPoint]
fileName = file.replace('.', '_' + str(i) + '.')
fileSegment.export(filePath + fileName, format="mp3")
fullFilePath = filePath + fileName
soundFile = open(fullFilePath, 'r')
url = "https://api.wit.ai/speech?v=20160526"
headers = {'Authorization': 'Bearer ' + witKey, 'Content-Type':
'audio/mpeg3'}
print(fileName)
results = requests.post(url, data=soundFile, headers=headers).json()
while '_text' not in results:
print(json.dumps(results))
sleepTime = 5
print('waiting ' + str(sleepTime) + ' seconds')
time.sleep(sleepTime)
soundFile = open(fullFilePath, 'r')
results = requests.post(url, data=soundFile, headers=headers).json()
results = results['_text']
f = open(file + '.txt', 'a')
f.write(results + '\n' + timeStamp + '\n')
print('Segment transcript created')
os.remove(fullFilePath)
elapsedTime = time.time() - segmentStartTime
m, s = divmod(elapsedTime, 60)
h, m = divmod(m, 60)
print('Segment processing time: ', '%d:%02d:%02d' % (h, m, s))
elapsedTime = time.time() - startTime
m, s = divmod(elapsedTime, 60)
h, m = divmod(m, 60)
print('Total script run time: ', '%d:%02d:%02d' % (h, m, s))