-
Notifications
You must be signed in to change notification settings - Fork 0
/
Run.py
173 lines (142 loc) · 7.54 KB
/
Run.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
#-------------------------------------------------------------------#
# BatchWhisper-Transcription-Translation [LOCAL & API] #
#-------------------------------------------------------------------#
# Author: SABIAN HIBBS #
# License: MIT #
# Version: 3.4 #
#-------------------------------------------------------------------#
import os
import shutil
import subprocess
import argparse
import language_dict
import output_format_type
# Create an argument parser and provide a description for the argument
parser = argparse.ArgumentParser(description="Choose the type of process to run.")
parser.add_argument('--type', type=int, choices=range(1, 7), required=True, help="Type of process to run (1 to 6)")
parser.add_argument('--info', type=str, required=False, help="""
Process Types linked to --type variable
--type 1 | Text to Audio Segments
--type 2 | Text to Audio Segments with Translation
--type 3 | Audio Translation (CPU)
--type 4 | Audio Translation (GPU)
--type 5 | Audio Transcription (CPU)
--type 6 | Audio Transcription (GPU)""")
args = parser.parse_args()
# Ask for the language input if the process type requires it
if args.type in [3, 4, 5, 6]:
print("Available languages:", language_dict.available_languages)
# Loop until a valid language is selected
while True:
print("---------------------------")
full_language_name = input("Enter the full name of the language: ")
language = language_dict.language_dict.get(full_language_name)
if language is not None:
print("Selected language:", language)
break # Exit the loop
else:
print("Unknown language:", full_language_name)
# Ask for the output format if the process type requires it
if args.type in [3, 4, 5, 6]:
print("Available Output Formats:", output_format_type.available_op_format)
while True:
print("---------------------------")
output_file_t = input("Enter the output format: ")
out_format = output_format_type.output_format_type.get(output_file_t)
if out_format is not None:
print("Selected output format:", out_format)
break # Exit the loop
else:
print("Unknown output format:", output_file_t)
# Create the directories if they don't exist
if not os.path.exists('Videos'):
os.mkdir('Videos')
num_files = len(os.listdir('Input-Videos'))
i = 1
root_directory = os.getcwd()
# Loop until all files have been processed
while num_files > 0:
video_folder_name = f'Video - {i}'
while os.path.exists(os.path.join('Videos', video_folder_name)):
i += 1
video_folder_name = f'Video - {i}'
file_to_process = os.listdir('Input-Videos')[0]
print(f"Processing file: {file_to_process}")
shutil.move(os.path.join('Input-Videos', file_to_process), file_to_process)
file_extension = os.path.splitext(file_to_process)[-1].lower()
valid_audio_file = file_extension in ['.mp3', '.wav']
# Convert non-audio files to WAV format if necessary
if not valid_audio_file and args.type in [3, 4, 5, 6]:
output_file = f'{os.path.splitext(file_to_process)[0]}.wav'
subprocess.run(['ffmpeg','-loglevel', 'error', '-stats', '-i', file_to_process, '-acodec', 'pcm_s16le', '-ar', '44100', output_file])
converted_file = output_file
os.chdir(root_directory)
# Run the specified process based on the process type
if args.type == 1:
subprocess.run(['python', 'Text_AudioSegments.py', file_to_process])
elif args.type == 2:
subprocess.run(['python', 'Text_AudioSegments_Translate.py', file_to_process])
elif args.type == 3:
subprocess.run(
f'whisper "{output_file}" --device cpu --model large --language {language} --task translate --output_format {out_format}',
shell=True)
elif args.type == 4:
subprocess.run(f'whisper "{output_file}" --device cuda --model large --language {language} --task translate --output_format {out_format}',
shell=True)
elif args.type == 5:
subprocess.run(
f'whisper "{output_file}" --device cpu --model large --language {language} --task transcribe --output_format {out_format}',
shell=True)
elif args.type == 6:
subprocess.run(f'whisper "{output_file}" --device cuda --model large --language {language} --task transcribe --output_format {out_format}',
shell=True)
try:
# Create a new directory for the processed video and move the files
os.mkdir(os.path.join('Videos', video_folder_name))
shutil.move(file_to_process, os.path.join('Videos', video_folder_name))
if args.type in [3, 4, 5, 6]:
output_file_base = os.path.splitext(output_file)[0]
output_file_txt = f'{output_file_base}.{out_format}'
shutil.move(output_file_txt, os.path.join('Videos', video_folder_name, output_file_txt))
shutil.move(output_file, os.path.join('Videos', video_folder_name))
if args.type in [1, 2]:
shutil.move('transcripts.txt', os.path.join('Videos', video_folder_name))
shutil.move('Audio_Segment', os.path.join('Videos', video_folder_name))
num_files -= 1
i += 1
except Exception as e:
print(f"Processing failed with error: {e}")
print("Reversing the file operations...")
# Move the files back to their original locations
shutil.move(os.path.join('Videos', video_folder_name, file_to_process), '.')
if args.type in [3, 4, 5, 6]:
shutil.move(os.path.join('Videos', video_folder_name, output_file_txt), '.')
shutil.move(os.path.join('Videos', video_folder_name, output_file), '.')
if args.type in [1, 2]:
for format in output_format_type.available_op_format.values():
output_file_format = f'{output_file_base}.{format}'
shutil.move(os.path.join('Videos', video_folder_name, output_file_format), '.')
shutil.move(os.path.join('Videos', video_folder_name, 'Audio_Segment'), '.')
# Delete the (file_to_process).wav file
os.remove(f'{file_to_process}.wav')
print("File operations reversed.")
#legacy code here for any issues reinstate:
"""# Create a new directory for the processed video and move the files
os.mkdir(os.path.join('Videos', video_folder_name))
shutil.move(file_to_process, os.path.join('Videos', video_folder_name))
if args.type in [3, 4, 5, 6]:
output_file_base = os.path.splitext(output_file)[0]
output_file_txt = f'{output_file_base}.{out_format}'
shutil.move(output_file_txt, os.path.join('Videos', video_folder_name, output_file_txt))
shutil.move(output_file, os.path.join('Videos', video_folder_name))
if args.type in [1, 2]:
shutil.move('transcripts.txt', os.path.join('Videos', video_folder_name))
shutil.move('Audio_Segment', os.path.join('Videos', video_folder_name))
num_files -= 1
i += 1"""
cwd = os.getcwd()
directory = os.path.join(cwd, "Videos")
# Clean up the temporary files and directories
for subdir in os.listdir(directory):
subdir_path = os.path.join(directory, subdir)
subprocess.run(['python', 'CleanUp.py'])