Spaces:
Sleeping
Sleeping
Delete video_processing.py
Browse files- video_processing.py +0 -89
video_processing.py
DELETED
@@ -1,89 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import requests
|
3 |
-
import uuid
|
4 |
-
import subprocess
|
5 |
-
import time
|
6 |
-
|
7 |
-
def extract_audio(video_path, output_format="mp3"):
|
8 |
-
if not video_path:
|
9 |
-
return None, "No video provided"
|
10 |
-
|
11 |
-
output_path = f"audio_{uuid.uuid4().hex[:6]}.{output_format}"
|
12 |
-
|
13 |
-
try:
|
14 |
-
cmd = [
|
15 |
-
"ffmpeg",
|
16 |
-
"-i", video_path,
|
17 |
-
"-vn",
|
18 |
-
"-c:a", "libmp3lame" if output_format == "mp3" else output_format,
|
19 |
-
"-q:a", "9",
|
20 |
-
"-ac", "1",
|
21 |
-
"-ar", "12000",
|
22 |
-
"-y",
|
23 |
-
output_path
|
24 |
-
]
|
25 |
-
|
26 |
-
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
27 |
-
|
28 |
-
if os.path.exists(output_path):
|
29 |
-
return output_path, "Audio extracted"
|
30 |
-
else:
|
31 |
-
return None, "Audio extraction failed"
|
32 |
-
except Exception as e:
|
33 |
-
return None, f"Error: {str(e)}"
|
34 |
-
|
35 |
-
def transcribe_audio(audio_path, api_key):
|
36 |
-
if not api_key:
|
37 |
-
return {"error": "API key required"}
|
38 |
-
|
39 |
-
url = "https://api.elevenlabs.io/v1/speech-to-text"
|
40 |
-
headers = {"xi-api-key": api_key}
|
41 |
-
|
42 |
-
try:
|
43 |
-
with open(audio_path, "rb") as file:
|
44 |
-
response = requests.post(
|
45 |
-
url,
|
46 |
-
headers=headers,
|
47 |
-
files={"file": file, "model_id": (None, "scribe_v1")},
|
48 |
-
timeout=120
|
49 |
-
)
|
50 |
-
|
51 |
-
if response.status_code == 200:
|
52 |
-
return response.json()
|
53 |
-
else:
|
54 |
-
return {"error": f"API error: {response.status_code}", "text": ""}
|
55 |
-
except Exception as e:
|
56 |
-
return {"error": f"Request failed: {str(e)}", "text": ""}
|
57 |
-
|
58 |
-
def process_video_file(video_file, output_format, elevenlabs_api_key, model_id="scribe_v1"):
|
59 |
-
print("Starting fast video processing...")
|
60 |
-
start = time.time()
|
61 |
-
|
62 |
-
if video_file is None:
|
63 |
-
return None, "Please upload a video file", None, "No video provided", None
|
64 |
-
|
65 |
-
audio_path, message = extract_audio(video_file, output_format)
|
66 |
-
if not audio_path:
|
67 |
-
print(f"Audio extraction failed: {message}")
|
68 |
-
return None, message, None, "Audio extraction failed", None
|
69 |
-
|
70 |
-
print(f"Audio extracted in {time.time() - start:.2f}s. Transcribing...")
|
71 |
-
transcription = transcribe_audio(audio_path, elevenlabs_api_key)
|
72 |
-
|
73 |
-
if "error" in transcription:
|
74 |
-
print(f"Transcription error: {transcription['error']}")
|
75 |
-
return audio_path, message, None, transcription["error"], None
|
76 |
-
|
77 |
-
transcript_text = transcription.get("text", "")
|
78 |
-
transcript_file = f"transcript_{uuid.uuid4().hex[:6]}.txt"
|
79 |
-
|
80 |
-
try:
|
81 |
-
with open(transcript_file, "w", encoding="utf-8") as f:
|
82 |
-
f.write(transcript_text)
|
83 |
-
transcript_message = "Transcription saved successfully"
|
84 |
-
except Exception as e:
|
85 |
-
transcript_file = None
|
86 |
-
transcript_message = f"Error saving transcription: {str(e)}"
|
87 |
-
|
88 |
-
print(f"Total video processing time: {time.time() - start:.2f}s")
|
89 |
-
return audio_path, message, transcript_file, transcript_message, transcript_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|