Streamlit YouTube Audio Downloader: Convert and Download YouTube Audio in WAV Format
To build a YouTube Audio Downloader using Streamlit, where users can input a YouTube URL, download the audio, and get a .wav
file, here’s a clear breakdown of how to do it. The app will fetch the audio stream from the YouTube video, convert it to a .wav
file, and let the user download it.
First, I will describe the main libraries to do the app:
Requirements:
First, make sure you have the required libraries installed. You will need:
pytubefix
(for downloading YouTube video streams)ffmpeg
(for audio conversion)streamlit
(for building the web interface)
To install them, run:
pip install pytubefix ffmpeg-python streamlit
Streamlit App Code:
Below is the complete Python code to create a YouTube Audio Downloader:
import streamlit as st
import pytubefix
import ffmpeg
from datetime import datetime
import os
# Function to download and convert YouTube audio
def download_audio(youtube_url):
try:
# Specify the output file name for the audio
filename = f"audios/"
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
filename += f"audio_{timestamp}.wav"
# Ensure the directory exists
os.makedirs("audios", exist_ok=True)
# Create a YouTube object and fetch the stream URL
yt = pytubefix.YouTube(youtube_url)
# Fetch the first available audio stream
stream = yt.streams[0].url # Get the first available stream (usually audio)
# Use ffmpeg to convert the audio stream to a .wav file
ffmpeg.input(stream).output(filename, format='wav', loglevel="error").run()
# Return the filename for download
return filename
except Exception as e:
st.error(f"Error: {e}")
return None
# Streamlit Dashboard UI
st.title("YouTube Audio Downloader")
# Input field for YouTube video URL
youtube_url = st.text_input("Enter YouTube Video URL")
if st.button("Download Audio"):
if youtube_url:
st.write("Downloading audio...")
filename = download_audio(youtube_url)
if filename:
st.success(f"Audio downloaded and saved as: {filename}")
# Allow user to download the file
with open(filename, "rb") as audio_file:
st.download_button(label="Download Audio", data=audio_file, file_name=os.path.basename(filename), mime="audio/wav")
else:
st.error("There was an issue with downloading the audio.")
else:
st.error("Please enter a valid YouTube URL.")
How it Works:
1. User Input:
- The user enters the YouTube video URL in the input box.
2. Audio Download and Conversion:
- The app uses
pytubefix
to fetch the audio stream from the YouTube video. - It then uses
ffmpeg
to convert the stream to a.wav
file and saves it locally with a unique filename based on the timestamp.
3. Download Button:
- Once the audio is downloaded and converted, the user is provided with a Download Audio button, which allows them to download the
.wav
file.
4. Error Handling:
- If there is an issue with the YouTube URL or the download, an error message is shown.
Running the Application:
- Save the code to a Python file (e.g.,
app.py
). - Run the app using the following command in the terminal:
streamlit run app.py
Interacting with the App:
- Open the URL provided by Streamlit in the browser.
- Enter a YouTube URL (e.g.,
https://www.youtube.com/watch?v=dQw4w9WgXcQ
). - Click the “Download Audio” button to initiate the download and conversion.
- Once the process is complete, a button will appear to download the
.wav
file.
Example Use Case:
- User Inputs:
https://www.youtube.com/watch?v=dQw4w9WgXcQ
- Output: A
.wav
file is created and made available for download with a unique filename (e.g.,audio_20241128123045.wav
).
Summary:
This Streamlit-based YouTube Audio Downloader allows users to easily download audio from YouTube videos, convert it to .wav
, and download it with just a few clicks. It’s simple, interactive, and works well for data science, content creators, and other users needing to extract audio from YouTube.
I hope this article can help you in your job! Thanks a lot!
Learn more YouTube Audio Downloader with Streamlit — Dashboard