Spaces:
Runtime error
Runtime error
Pranjal Pruthi
commited on
Commit
·
c79705c
1
Parent(s):
ac40121
Add application files and dependencies
Browse files- video_processor.py +30 -0
video_processor.py
CHANGED
|
@@ -4,12 +4,17 @@ import glob
|
|
| 4 |
import re
|
| 5 |
import select
|
| 6 |
import shutil
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Dictionary to store the status of each video processing task
|
| 9 |
status_dict = {}
|
|
|
|
| 10 |
|
| 11 |
def run_command_with_progress(command, session_id, tool_name, is_trimming=False):
|
| 12 |
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True)
|
|
|
|
| 13 |
|
| 14 |
status_dict[session_id]["status"] = f"Using {tool_name}..."
|
| 15 |
|
|
@@ -19,6 +24,11 @@ def run_command_with_progress(command, session_id, tool_name, is_trimming=False)
|
|
| 19 |
duration = None
|
| 20 |
|
| 21 |
while True:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
reads = [process.stdout.fileno(), process.stderr.fileno()]
|
| 23 |
ret = select.select(reads, [], [], 0.1)
|
| 24 |
|
|
@@ -65,6 +75,7 @@ def run_command_with_progress(command, session_id, tool_name, is_trimming=False)
|
|
| 65 |
|
| 66 |
return return_code
|
| 67 |
|
|
|
|
| 68 |
def process_video(url, session_id):
|
| 69 |
output_dir = f"./output_folder_{session_id}"
|
| 70 |
os.makedirs(output_dir, exist_ok=True)
|
|
@@ -135,6 +146,7 @@ def process_video(url, session_id):
|
|
| 135 |
status_dict[session_id]["status"] = f"Processing completed! Directory: {trimmed_dir}"
|
| 136 |
return trimmed_dir
|
| 137 |
|
|
|
|
| 138 |
def get_status(session_id):
|
| 139 |
return status_dict.get(session_id, {}).get("status", "Invalid session ID.")
|
| 140 |
|
|
@@ -144,3 +156,21 @@ def get_error_details(session_id):
|
|
| 144 |
def cancel_job(session_id):
|
| 145 |
if session_id in status_dict:
|
| 146 |
status_dict[session_id]["status"] = "Cancelled"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import re
|
| 5 |
import select
|
| 6 |
import shutil
|
| 7 |
+
import threading
|
| 8 |
+
import psutil
|
| 9 |
+
import signal
|
| 10 |
|
| 11 |
# Dictionary to store the status of each video processing task
|
| 12 |
status_dict = {}
|
| 13 |
+
process_dict = {}
|
| 14 |
|
| 15 |
def run_command_with_progress(command, session_id, tool_name, is_trimming=False):
|
| 16 |
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True)
|
| 17 |
+
process_dict[session_id] = process
|
| 18 |
|
| 19 |
status_dict[session_id]["status"] = f"Using {tool_name}..."
|
| 20 |
|
|
|
|
| 24 |
duration = None
|
| 25 |
|
| 26 |
while True:
|
| 27 |
+
if status_dict[session_id].get("status") == "Cancelled":
|
| 28 |
+
process.terminate()
|
| 29 |
+
process.wait()
|
| 30 |
+
return -1
|
| 31 |
+
|
| 32 |
reads = [process.stdout.fileno(), process.stderr.fileno()]
|
| 33 |
ret = select.select(reads, [], [], 0.1)
|
| 34 |
|
|
|
|
| 75 |
|
| 76 |
return return_code
|
| 77 |
|
| 78 |
+
|
| 79 |
def process_video(url, session_id):
|
| 80 |
output_dir = f"./output_folder_{session_id}"
|
| 81 |
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
| 146 |
status_dict[session_id]["status"] = f"Processing completed! Directory: {trimmed_dir}"
|
| 147 |
return trimmed_dir
|
| 148 |
|
| 149 |
+
|
| 150 |
def get_status(session_id):
|
| 151 |
return status_dict.get(session_id, {}).get("status", "Invalid session ID.")
|
| 152 |
|
|
|
|
| 156 |
def cancel_job(session_id):
|
| 157 |
if session_id in status_dict:
|
| 158 |
status_dict[session_id]["status"] = "Cancelled"
|
| 159 |
+
if session_id in process_dict:
|
| 160 |
+
process = process_dict[session_id]
|
| 161 |
+
if process.poll() is None: # Check if the process is still running
|
| 162 |
+
parent = psutil.Process(process.pid)
|
| 163 |
+
for child in parent.children(recursive=True):
|
| 164 |
+
child.terminate()
|
| 165 |
+
parent.terminate()
|
| 166 |
+
process_dict.pop(session_id)
|
| 167 |
+
return True
|
| 168 |
+
return False
|
| 169 |
+
|
| 170 |
+
def cleanup_cancelled_jobs():
|
| 171 |
+
for session_id in list(status_dict.keys()):
|
| 172 |
+
if status_dict[session_id]["status"] == "Cancelled":
|
| 173 |
+
output_dir = f"./output_folder_{session_id}"
|
| 174 |
+
if os.path.exists(output_dir):
|
| 175 |
+
shutil.rmtree(output_dir)
|
| 176 |
+
status_dict.pop(session_id)
|