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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
import json
|
| 5 |
+
from moviepy.editor import VideoFileClip
|
| 6 |
+
import yt_dlp
|
| 7 |
+
import uuid
|
| 8 |
+
|
| 9 |
+
ELEVENLABS_API_KEY = scribe_api
|
| 10 |
+
|
| 11 |
+
def download_video_from_url(url):
|
| 12 |
+
try:
|
| 13 |
+
filename = f"downloaded_video_{uuid.uuid4().hex[:8]}.mp4"
|
| 14 |
+
ydl_opts = {
|
| 15 |
+
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4',
|
| 16 |
+
'outtmpl': filename,
|
| 17 |
+
'quiet': True,
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 21 |
+
ydl.download([url])
|
| 22 |
+
|
| 23 |
+
return filename, None
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return None, f"Error downloading video: {str(e)}"
|
| 26 |
+
|
| 27 |
+
def extract_audio(video_path, output_format="mp3"):
|
| 28 |
+
if not video_path:
|
| 29 |
+
return None, "No video provided"
|
| 30 |
+
|
| 31 |
+
output_path = f"extracted_audio_{uuid.uuid4().hex[:8]}.{output_format}"
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
video = VideoFileClip(video_path)
|
| 35 |
+
video.audio.write_audiofile(output_path, verbose=False, logger=None)
|
| 36 |
+
video.close()
|
| 37 |
+
return output_path, f"Audio extracted successfully"
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return None, f"Error extracting audio: {str(e)}"
|
| 40 |
+
|
| 41 |
+
def save_transcription(transcription):
|
| 42 |
+
if "error" in transcription:
|
| 43 |
+
return None, transcription["error"]
|
| 44 |
+
|
| 45 |
+
transcript_filename = f"transcription_{uuid.uuid4().hex[:8]}.txt"
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
with open(transcript_filename, "w", encoding="utf-8") as f:
|
| 49 |
+
f.write(transcription.get('text', 'No text found'))
|
| 50 |
+
|
| 51 |
+
return transcript_filename, "Transcription saved as text file"
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return None, f"Error saving transcription: {str(e)}"
|
| 54 |
+
|
| 55 |
+
def process_video_file(video_file, output_format, api_key, model_id):
|
| 56 |
+
if video_file is None:
|
| 57 |
+
return None, "Please upload a video file", None, "No video provided"
|
| 58 |
+
|
| 59 |
+
audio_path, message = extract_audio(video_file, output_format)
|
| 60 |
+
|
| 61 |
+
if audio_path and os.path.exists(audio_path):
|
| 62 |
+
transcription = transcribe_audio(audio_path, api_key, model_id)
|
| 63 |
+
transcript_file, transcript_message = save_transcription(transcription)
|
| 64 |
+
return audio_path, message, transcript_file, transcript_message
|
| 65 |
+
else:
|
| 66 |
+
return None, message, None, "Audio extraction failed, cannot transcribe"
|
| 67 |
+
|
| 68 |
+
def process_video_url(video_url, output_format, api_key, model_id):
|
| 69 |
+
if not video_url.strip():
|
| 70 |
+
return None, "Please enter a video URL", None, "No URL provided"
|
| 71 |
+
video_path, error = download_video_from_url(video_url)
|
| 72 |
+
if error:
|
| 73 |
+
return None, error, None, "Video download failed, cannot transcribe"
|
| 74 |
+
|
| 75 |
+
audio_path, message = extract_audio(video_path, output_format)
|
| 76 |
+
if video_path and os.path.exists(video_path):
|
| 77 |
+
try:
|
| 78 |
+
os.remove(video_path)
|
| 79 |
+
except:
|
| 80 |
+
pass
|
| 81 |
+
|
| 82 |
+
if audio_path and os.path.exists(audio_path):
|
| 83 |
+
transcription = transcribe_audio(audio_path, api_key, model_id)
|
| 84 |
+
transcript_file, transcript_message = save_transcription(transcription)
|
| 85 |
+
return audio_path, message, transcript_file, transcript_message
|
| 86 |
+
else:
|
| 87 |
+
return None, message, None, "Audio extraction failed, cannot transcribe"
|
| 88 |
+
|
| 89 |
+
def transcribe_audio(audio_file, api_key, model_id="scribe_v1"):
|
| 90 |
+
if not api_key:
|
| 91 |
+
return {"error": "Please provide an API key"}
|
| 92 |
+
|
| 93 |
+
url = "https://api.elevenlabs.io/v1/speech-to-text"
|
| 94 |
+
headers = {
|
| 95 |
+
"xi-api-key": api_key
|
| 96 |
+
}
|
| 97 |
+
files = {
|
| 98 |
+
"file": open(audio_file, "rb"),
|
| 99 |
+
"model_id": (None, model_id)
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
try:
|
| 103 |
+
response = requests.post(url, headers=headers, files=files)
|
| 104 |
+
response.raise_for_status()
|
| 105 |
+
result = response.json()
|
| 106 |
+
return result
|
| 107 |
+
except requests.exceptions.RequestException as e:
|
| 108 |
+
return {"error": f"API request failed: {str(e)}"}
|
| 109 |
+
except json.JSONDecodeError:
|
| 110 |
+
return {"error": "Failed to parse API response"}
|
| 111 |
+
finally:
|
| 112 |
+
files["file"].close()
|
| 113 |
+
|
| 114 |
+
with gr.Blocks(title="Video to Audio to Transcription") as app:
|
| 115 |
+
gr.Markdown("# Video => Audio => Transcription")
|
| 116 |
+
gr.Markdown("Upload a video or provide a URL to extract audio and transcribe it using ElevenLabs' API.")
|
| 117 |
+
|
| 118 |
+
api_key = gr.Textbox(
|
| 119 |
+
placeholder="Enter your ElevenLabs API key",
|
| 120 |
+
label="ElevenLabs API Key",
|
| 121 |
+
type="password",
|
| 122 |
+
value=ELEVENLABS_API_KEY
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
model_id = gr.Dropdown(
|
| 126 |
+
choices=["scribe_v1", "eleven_turbo_v2"],
|
| 127 |
+
value="scribe_v1",
|
| 128 |
+
label="Transcription Model"
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
with gr.Tabs():
|
| 132 |
+
with gr.TabItem("Upload Video"):
|
| 133 |
+
with gr.Row():
|
| 134 |
+
with gr.Column():
|
| 135 |
+
video_input = gr.Video(label="Upload Video")
|
| 136 |
+
format_choice_file = gr.Radio(["mp3", "wav"], value="mp3", label="Output Format")
|
| 137 |
+
extract_button_file = gr.Button("Extract Audio & Transcribe")
|
| 138 |
+
|
| 139 |
+
with gr.Column():
|
| 140 |
+
audio_output_file = gr.Audio(label="Extracted Audio", type="filepath")
|
| 141 |
+
status_output_file = gr.Textbox(label="Audio Extraction Status")
|
| 142 |
+
transcript_file_output = gr.File(label="Transcription Text File")
|
| 143 |
+
transcript_status_output = gr.Textbox(label="Transcription Status")
|
| 144 |
+
|
| 145 |
+
extract_button_file.click(
|
| 146 |
+
fn=process_video_file,
|
| 147 |
+
inputs=[video_input, format_choice_file, api_key, model_id],
|
| 148 |
+
outputs=[audio_output_file, status_output_file, transcript_file_output, transcript_status_output]
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
with gr.TabItem("Video URL"):
|
| 152 |
+
with gr.Row():
|
| 153 |
+
with gr.Column():
|
| 154 |
+
url_input = gr.Textbox(label="Video URL (YouTube, etc.)", placeholder="https://www.youtube.com/watch?v=...")
|
| 155 |
+
format_choice_url = gr.Radio(["mp3", "wav"], value="mp3", label="Output Format")
|
| 156 |
+
extract_button_url = gr.Button("Extract Audio & Transcribe")
|
| 157 |
+
|
| 158 |
+
with gr.Column():
|
| 159 |
+
audio_output_url = gr.Audio(label="Extracted Audio", type="filepath")
|
| 160 |
+
status_output_url = gr.Textbox(label="Audio Extraction Status")
|
| 161 |
+
transcript_file_output_url = gr.File(label="Transcription Text File")
|
| 162 |
+
transcript_status_output_url = gr.Textbox(label="Transcription Status")
|
| 163 |
+
|
| 164 |
+
extract_button_url.click(
|
| 165 |
+
fn=process_video_url,
|
| 166 |
+
inputs=[url_input, format_choice_url, api_key, model_id],
|
| 167 |
+
outputs=[audio_output_url, status_output_url, transcript_file_output_url, transcript_status_output_url]
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
if __name__ == "__main__":
|
| 171 |
+
app.launch()
|