Update app.py
Browse files
app.py
CHANGED
@@ -3,42 +3,10 @@ import gradio as gr
|
|
3 |
import requests
|
4 |
import json
|
5 |
from moviepy import VideoFileClip
|
6 |
-
import yt_dlp
|
7 |
import uuid
|
8 |
|
9 |
ELEVENLABS_API_KEY = os.environ.get("ELEVENLABS_API_KEY", None)
|
10 |
|
11 |
-
def download_video_from_url(url):
|
12 |
-
try:
|
13 |
-
# Generate a unique filename
|
14 |
-
filename = f"downloaded_video_{uuid.uuid4().hex[:8]}.mp4"
|
15 |
-
|
16 |
-
# Configure yt-dlp options without browser cookies
|
17 |
-
ydl_opts = {
|
18 |
-
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4',
|
19 |
-
'outtmpl': filename,
|
20 |
-
'quiet': True,
|
21 |
-
'no_warnings': True,
|
22 |
-
'ignoreerrors': True,
|
23 |
-
'nocheckcertificate': True,
|
24 |
-
'extractor_args': {'youtube': {'skip': ['webpage']}}, # Skip webpage verification
|
25 |
-
'extractor_retries': 3,
|
26 |
-
# Add more formats as fallbacks
|
27 |
-
'merge_output_format': 'mp4',
|
28 |
-
}
|
29 |
-
|
30 |
-
# Download the video
|
31 |
-
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
32 |
-
ydl.download([url])
|
33 |
-
|
34 |
-
if os.path.exists(filename) and os.path.getsize(filename) > 0:
|
35 |
-
return filename, None
|
36 |
-
else:
|
37 |
-
return None, "Failed to download video. Try providing a direct video URL instead of YouTube."
|
38 |
-
|
39 |
-
except Exception as e:
|
40 |
-
return None, f"Error downloading video: {str(e)}"
|
41 |
-
|
42 |
def extract_audio(video_path, output_format="mp3"):
|
43 |
if not video_path:
|
44 |
return None, "No video provided"
|
@@ -47,7 +15,6 @@ def extract_audio(video_path, output_format="mp3"):
|
|
47 |
|
48 |
try:
|
49 |
video = VideoFileClip(video_path)
|
50 |
-
# Remove the verbose parameter that's causing issues
|
51 |
video.audio.write_audiofile(output_path, logger=None)
|
52 |
video.close()
|
53 |
return output_path, f"Audio extracted successfully"
|
@@ -57,8 +24,6 @@ def extract_audio(video_path, output_format="mp3"):
|
|
57 |
def save_transcription(transcription):
|
58 |
if "error" in transcription:
|
59 |
return None, transcription["error"]
|
60 |
-
|
61 |
-
# Create a filename for the transcription
|
62 |
transcript_filename = f"transcription_{uuid.uuid4().hex[:8]}.txt"
|
63 |
|
64 |
try:
|
@@ -86,15 +51,11 @@ def process_video_url(video_url, output_format, api_key, model_id):
|
|
86 |
if not video_url.strip():
|
87 |
return None, "Please enter a video URL", None, "No URL provided"
|
88 |
|
89 |
-
# Download the video
|
90 |
video_path, error = download_video_from_url(video_url)
|
91 |
if error:
|
92 |
return None, error, None, "Video download failed, cannot transcribe"
|
93 |
|
94 |
-
|
95 |
-
audio_path, message = extract_audio(video_path, output_format)
|
96 |
-
|
97 |
-
# Clean up the downloaded video
|
98 |
if video_path and os.path.exists(video_path):
|
99 |
try:
|
100 |
os.remove(video_path)
|
@@ -167,25 +128,6 @@ with gr.Blocks(title="Video to Audio to Transcription") as app:
|
|
167 |
inputs=[video_input, format_choice_file, api_key, model_id],
|
168 |
outputs=[audio_output_file, status_output_file, transcript_file_output, transcript_status_output]
|
169 |
)
|
170 |
-
|
171 |
-
with gr.TabItem("Video URL"):
|
172 |
-
with gr.Row():
|
173 |
-
with gr.Column():
|
174 |
-
url_input = gr.Textbox(label="Video URL (YouTube, etc.)", placeholder="https://www.youtube.com/watch?v=...")
|
175 |
-
format_choice_url = gr.Radio(["mp3", "wav"], value="mp3", label="Output Format")
|
176 |
-
extract_button_url = gr.Button("Extract Audio & Transcribe")
|
177 |
-
|
178 |
-
with gr.Column():
|
179 |
-
audio_output_url = gr.Audio(label="Extracted Audio", type="filepath")
|
180 |
-
status_output_url = gr.Textbox(label="Audio Extraction Status")
|
181 |
-
transcript_file_output_url = gr.File(label="Transcription Text File")
|
182 |
-
transcript_status_output_url = gr.Textbox(label="Transcription Status")
|
183 |
-
|
184 |
-
extract_button_url.click(
|
185 |
-
fn=process_video_url,
|
186 |
-
inputs=[url_input, format_choice_url, api_key, model_id],
|
187 |
-
outputs=[audio_output_url, status_output_url, transcript_file_output_url, transcript_status_output_url]
|
188 |
-
)
|
189 |
|
190 |
if __name__ == "__main__":
|
191 |
app.launch()
|
|
|
3 |
import requests
|
4 |
import json
|
5 |
from moviepy import VideoFileClip
|
|
|
6 |
import uuid
|
7 |
|
8 |
ELEVENLABS_API_KEY = os.environ.get("ELEVENLABS_API_KEY", None)
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def extract_audio(video_path, output_format="mp3"):
|
11 |
if not video_path:
|
12 |
return None, "No video provided"
|
|
|
15 |
|
16 |
try:
|
17 |
video = VideoFileClip(video_path)
|
|
|
18 |
video.audio.write_audiofile(output_path, logger=None)
|
19 |
video.close()
|
20 |
return output_path, f"Audio extracted successfully"
|
|
|
24 |
def save_transcription(transcription):
|
25 |
if "error" in transcription:
|
26 |
return None, transcription["error"]
|
|
|
|
|
27 |
transcript_filename = f"transcription_{uuid.uuid4().hex[:8]}.txt"
|
28 |
|
29 |
try:
|
|
|
51 |
if not video_url.strip():
|
52 |
return None, "Please enter a video URL", None, "No URL provided"
|
53 |
|
|
|
54 |
video_path, error = download_video_from_url(video_url)
|
55 |
if error:
|
56 |
return None, error, None, "Video download failed, cannot transcribe"
|
57 |
|
58 |
+
audio_path, message = extract_audio(video_path, output_format)
|
|
|
|
|
|
|
59 |
if video_path and os.path.exists(video_path):
|
60 |
try:
|
61 |
os.remove(video_path)
|
|
|
128 |
inputs=[video_input, format_choice_file, api_key, model_id],
|
129 |
outputs=[audio_output_file, status_output_file, transcript_file_output, transcript_status_output]
|
130 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
if __name__ == "__main__":
|
133 |
app.launch()
|