-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimed_proc.py
143 lines (117 loc) · 5.59 KB
/
timed_proc.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
import os
import shutil
import subprocess
import concurrent.futures
import pynvml
import queue
import signal
import sys
import time
def get_gpu_memory_info():
pynvml.nvmlInit()
handle = pynvml.nvmlDeviceGetHandleByIndex(0)
info = pynvml.nvmlDeviceGetMemoryInfo(handle)
pynvml.nvmlShutdown()
return info.total, info.free
def process_file(file_to_process, video_folder_name):
try:
# Move the file to the processing directory
shutil.move(os.path.join('Input-Videos', file_to_process), file_to_process)
print(f"Processing file: {file_to_process}")
# Run Processing
subprocess.run(f'whisper "{file_to_process}" --device cuda --model large-v2 --language en --task transcribe --fp16 False --output_format all', shell=True)
# Create a new directory for the processed video and move all related files
new_folder_path = os.path.join('Videos', video_folder_name)
os.mkdir(new_folder_path)
# Move the original file and all related output files
shutil.move(file_to_process, new_folder_path)
output_file_base = os.path.splitext(file_to_process)[0]
for filename in os.listdir('.'):
if filename.startswith(output_file_base):
shutil.move(filename, new_folder_path)
except Exception as e:
print(f"Processing failed with error: {e}")
print("Reversing the file operations...")
if os.path.exists(os.path.join('Videos', video_folder_name, file_to_process)):
shutil.move(os.path.join('Videos', video_folder_name, file_to_process), '.')
if os.path.exists(os.path.join('Videos', video_folder_name)):
shutil.rmtree(os.path.join('Videos', video_folder_name))
move_and_clear_videos()
def worker(file_queue):
while not file_queue.empty():
try:
file_to_process = file_queue.get_nowait()
except queue.Empty:
break
video_folder_name = f'Video - {file_to_process[1]}'
process_file(file_to_process[0], video_folder_name)
file_queue.task_done()
def process_files_LMT2_batch():
total_memory, free_memory = get_gpu_memory_info()
vram_per_process = 11 * 1024**3 # Convert 11.7 GB to bytes
max_processes = int(free_memory // vram_per_process)
input_dir = 'Input-Videos'
files_to_process = os.listdir(input_dir)
num_files = len(files_to_process)
file_queue = queue.Queue()
for i, file_to_process in enumerate(files_to_process, 1):
file_queue.put((file_to_process, i))
with concurrent.futures.ThreadPoolExecutor(max_workers=max_processes) as executor:
futures = [executor.submit(worker, file_queue) for _ in range(max_processes)]
concurrent.futures.wait(futures)
def cleanup_filenames():
videos_folder = "./Videos"
for subdir in os.listdir(videos_folder):
subdir_path = os.path.join(videos_folder, subdir)
if os.path.isdir(subdir_path):
files = os.listdir(subdir_path)
if files:
largest_file = max(files, key=lambda f: os.path.getsize(os.path.join(subdir_path, f)))
new_name = os.path.splitext(largest_file)[0]
os.rename(subdir_path, os.path.join(videos_folder, new_name))
def move_and_clear_videos():
current_directory = os.path.dirname(os.path.abspath(__file__))
target_directory = os.path.join(current_directory, 'Input-Videos')
videos_directory = os.path.join(current_directory, 'Videos')
extensions_to_remove = ['.json', '.srt', '.tsv', '.txt', '.vtt']
# Create 'Input-Videos' directory if it doesn't exist
if not os.path.exists(target_directory):
os.makedirs(target_directory)
# Find and move all .mp4 files
for root, dirs, files in os.walk(current_directory):
for file in files:
if file.endswith('.mp4'):
source_path = os.path.join(root, file)
destination_path = os.path.join(target_directory, file)
print(f"Moving {source_path} to {destination_path}")
shutil.move(source_path, destination_path)
# Only process the top directory (current directory)
break
# Remove specified files in the current directory
for file in os.listdir(current_directory):
if any(file.endswith(ext) for ext in extensions_to_remove):
file_path = os.path.join(current_directory, file)
print(f"Removing file {file_path}")
os.remove(file_path)
# Clear the 'Videos' directory
if os.path.exists(videos_directory):
for root, dirs, files in os.walk(videos_directory, topdown=False):
for file in files:
file_path = os.path.join(root, file)
print(f"Removing file {file_path}")
os.remove(file_path)
for dir in dirs:
dir_path = os.path.join(root, dir)
print(f"Removing directory {dir_path}")
os.rmdir(dir_path)
os.rmdir(videos_directory) # Remove the 'Videos' directory itself
if __name__ == '__main__':
try:
start_time = time.time() # Record the start time
process_files_LMT2_batch()
cleanup_filenames()
end_time = time.time() # Record the end time
elapsed_time = end_time - start_time # Calculate the elapsed time
print(f"Script completed in {elapsed_time:.2f} seconds")
except Exception as e:
move_and_clear_videos() # Basic cleanup on error