Spaces:
Build error
Build error
| from pytube import YouTube | |
| import gradio as gr | |
| from moviepy.editor import AudioFileClip | |
| import os | |
| def download_audio_as_mp3(url): | |
| try: | |
| yt = YouTube(url) | |
| # Get the highest quality audio stream | |
| audio_stream = yt.streams.filter(only_audio=True).first() | |
| if not audio_stream: | |
| return None, "No audio streams available." | |
| audio_file = audio_stream.download(filename="temp_audio") | |
| print(f"Downloaded audio: {yt.title}") | |
| # Convert to mp3 using moviepy | |
| audio_clip = AudioFileClip(audio_file) | |
| mp3_file = f"{yt.title}.mp3" | |
| audio_clip.write_audiofile(mp3_file) | |
| audio_clip.close() | |
| # Remove the temporary file | |
| os.remove(audio_file) | |
| return mp3_file, None | |
| except Exception as e: | |
| return None, f"An error occurred: {e}" | |
| def audio_downloader(url): | |
| file_path, error_message = download_audio_as_mp3(url) | |
| if error_message: | |
| return None, error_message | |
| else: | |
| return file_path, None | |
| # Create a Gradio interface | |
| iface = gr.Interface( | |
| fn=audio_downloader, | |
| inputs=gr.Textbox(label="YouTube URL"), | |
| outputs=[gr.File(label="Download MP3"), gr.Textbox(label="Error Message")], | |
| title="YouTube Audio Downloader", | |
| description="Enter a YouTube URL and download the audio as an MP3." | |
| ) | |
| # Launch the interface | |
| iface.launch(share=True) | |