Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,43 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import yt_dlp
|
| 4 |
+
from io import StringIO
|
| 5 |
+
import sys
|
| 6 |
|
| 7 |
+
class OutputLogger:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
self.output = ""
|
| 10 |
|
| 11 |
+
def write(self, msg):
|
| 12 |
+
self.output += msg
|
| 13 |
|
| 14 |
+
def flush(self):
|
| 15 |
+
pass
|
| 16 |
+
|
| 17 |
+
def download_video(url):
|
| 18 |
+
logger = OutputLogger()
|
| 19 |
+
sys.stdout = logger
|
| 20 |
+
|
| 21 |
+
ydl_opts = {
|
| 22 |
+
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
|
| 23 |
+
'outtmpl': '%(title)s.%(ext)s',
|
| 24 |
+
'verbose': True,
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 28 |
+
info = ydl.extract_info(url, download=True)
|
| 29 |
+
filename = ydl.prepare_filename(info)
|
| 30 |
+
|
| 31 |
+
sys.stdout = sys.__stdout__
|
| 32 |
+
|
| 33 |
+
return gr.File(filename), "Video downloaded successfully! Click the download button to save the file.", logger.output
|
| 34 |
+
|
| 35 |
+
iface = gr.Interface(
|
| 36 |
+
fn=download_video,
|
| 37 |
+
inputs=gr.Textbox(label="YouTube URL", value="https://www.youtube.com/watch?v=PEC1C4p0k3E"),
|
| 38 |
+
outputs=[gr.File(label="Downloaded Video"), gr.Textbox(label="Status"), gr.Textbox(label="Output")],
|
| 39 |
+
title="YouTube Video Downloader",
|
| 40 |
+
description="Enter a YouTube video URL and click 'Submit' to download the video.",
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
iface.launch(debug=True, share=True)
|