Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from pytube import YouTube
|
| 3 |
+
from moviepy.editor import *
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
def download_audio_as_wav(youtube_url):
|
| 7 |
+
output_filename = "downloaded_audio"
|
| 8 |
+
# Download YouTube video audio stream
|
| 9 |
+
yt = YouTube(youtube_url)
|
| 10 |
+
audio_stream = yt.streams.filter(only_audio=True).first()
|
| 11 |
+
audio_file = audio_stream.download(filename='temp_audio.mp4')
|
| 12 |
+
|
| 13 |
+
# Convert downloaded audio to .wav format
|
| 14 |
+
clip = AudioFileClip(audio_file)
|
| 15 |
+
output_path = f"{output_filename}.wav"
|
| 16 |
+
clip.write_audiofile(output_path)
|
| 17 |
+
|
| 18 |
+
# Clean up the temporary file
|
| 19 |
+
clip.close()
|
| 20 |
+
os.remove('temp_audio.mp4')
|
| 21 |
+
|
| 22 |
+
return output_path
|
| 23 |
+
|
| 24 |
+
# Gradio interface
|
| 25 |
+
iface = gr.Interface(
|
| 26 |
+
fn=download_audio_as_wav,
|
| 27 |
+
inputs=[gr.Textbox(label="Enter YouTube Video URL")],
|
| 28 |
+
outputs=[gr.Audio(label="Downloaded Audio")],
|
| 29 |
+
title="YouTube Audio Downloader",
|
| 30 |
+
description="Enter a YouTube video URL to download its audio as a .wav file."
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Launch the app
|
| 34 |
+
iface.launch(debug=True)
|