Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from pytube import Playlist, YouTube
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
def download_audio(url):
|
| 6 |
+
playlist = Playlist(url)
|
| 7 |
+
audio_files = []
|
| 8 |
+
for video_url in playlist.video_urls:
|
| 9 |
+
yt = YouTube(video_url)
|
| 10 |
+
stream = yt.streams.filter(only_audio=True).first()
|
| 11 |
+
audio_file = stream.download(output_path=".", filename=f"{yt.title}.mp3")
|
| 12 |
+
audio_files.append(audio_file)
|
| 13 |
+
return audio_files
|
| 14 |
+
|
| 15 |
+
def create_player(url):
|
| 16 |
+
audio_files = download_audio(url)
|
| 17 |
+
return gr.Dropdown.update(choices=audio_files, label="Select a track"), gr.Audio.update(value=audio_files[0])
|
| 18 |
+
|
| 19 |
+
with gr.Blocks() as demo:
|
| 20 |
+
with gr.Row():
|
| 21 |
+
url_input = gr.Textbox(label="YouTube Playlist URL")
|
| 22 |
+
submit_button = gr.Button("Load Playlist")
|
| 23 |
+
|
| 24 |
+
with gr.Row():
|
| 25 |
+
track_dropdown = gr.Dropdown(label="Select a track")
|
| 26 |
+
audio_player = gr.Audio()
|
| 27 |
+
|
| 28 |
+
submit_button.click(create_player, inputs=url_input, outputs=[track_dropdown, audio_player])
|
| 29 |
+
|
| 30 |
+
demo.launch()
|