-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateTextFromSpeech.py
115 lines (94 loc) · 4.72 KB
/
GenerateTextFromSpeech.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
#################################################################################################
# importing the necessary libraries
#################################################################################################
import argparse
import os
from shutil import rmtree
import speech_recognition as sr
import subprocess
import time
import tempfile
#################################################################################################
# Create folder routine
#################################################################################################
def createPath(s):
try:
os.mkdir(s)
except OSError:
assert False, "Creation of the directory %s failed (The TEMP directory may already exist.)"
#################################################################################################
# Folder cleanup routine
#################################################################################################
def deletePath(s): # Dangerous! Watch out!
try:
rmtree(s, ignore_errors=False)
except OSError:
print("Deletion of the directory %s failed" % s)
print(OSError)
#################################################################################################
# Print progress bar
#################################################################################################
def printProgressBar(iteration, total, prefix='', suffix='', decimals=1, length=100, fill='█', printEnd="\r"):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end=printEnd)
# Print New Line on Complete
if iteration == total:
print()
#################################################################################################
# Get arguments from command line
#################################################################################################
parser = argparse.ArgumentParser(
description='Translate audio from video file, generate subtitles, and burn them into a new video file')
parser.add_argument('--input_file', type=str, help='The video file you want to generate subtitles for')
parser.add_argument('--output_file', type=str, help="the _output location to write the subtitle file")
parser.add_argument('--language', type=str, default="en-US", help="the _output location to write the subtitled video")
args = parser.parse_args()
#################################################################################################
# Show usage and end if required inputs were not provided
#################################################################################################
if not args.input_file or not args.output_file:
parser.print_usage()
quit()
with tempfile.TemporaryDirectory() as tmpdirname:
print('Created temporary directory', tmpdirname)
print("Creating audio only file: {}/audio.wav".format(tmpdirname))
# using only no video (-vn) flag to keep the original sample and bit rate
command = "ffmpeg -i '{}' -vn {}/audio.wav".format(args.input_file, tmpdirname)
subprocess.call(command, shell=True)
print("Generating transcript from: {}/audio.wav".format(tmpdirname))
# create recognizer instance
r = sr.Recognizer()
#r.energy_threshold = 4000
fh = open(args.output_file, "w+")
# subtitle count
subtitle_number = 0
# Read Audio File
with sr.WavFile("{}/audio.wav".format(tmpdirname)) as source: # use "test.wav" as the audio source
audio_length = source.DURATION # get length of audio file
audio = r.record(source)
# Recognize speech using Google
try:
subtitle_number += 1
rec = r.recognize_google(audio, language=args.language)
fh.write("{}\n".format(rec))
except sr.UnknownValueError:
pass
#fh.write("*** Could not understand audio ***\n")
except sr.RequestError as e:
print("RequestError {0}".format(e))
fh.close()
print("Text written to: {}".format(args.output_file))