Download mp3 from Youtube Playlist

Trust me there is no easy way, until this

bedy kharisma
Coinmonks
Published in
4 min readJun 3, 2024

--

Photo by Oleg Sergeichik on Unsplash

Spotify and YouTube Music offer easy access to thousands of songs, but there’s a catch — they’re not free. Even with a paid membership, you only get limited access. The problem arises when you want free access to thousands of songs, which seems impossible nowadays. However, we used to live in an era where MP3 players were king!

Here, we want to take you back to that golden era and show you how to download MP3s from your YouTube playlist.

This story outlines a method involving two critical steps: first, manually obtaining the URLs of each video in a YouTube playlist using Power Automate Desktop, and second, downloading the audio from these URLs using a Python notebook.

Step 1: Obtaining Video URLs from a YouTube Playlist

The initial step in the process is to get the URLs of each video in a YouTube playlist. This can be particularly challenging because YouTube frequently changes its layout and the way it displays video links in playlists. Traditional web scraping libraries like BeautifulSoup or Selenium often become ineffective as they cannot keep up with these changes. Trust me, I have tried numerous libraries to scrape these video URLs, but none of them provided a reliable solution.

To overcome this challenge, we need to manually extract the URLs. This is where Power Automate Desktop comes in handy. Power Automate Desktop is a powerful tool that allows you to automate repetitive tasks on your computer. By using it, you can automate the process of copying each video’s URL in a playlist and saving them into an Excel file.

To do so. try to immitate the one I create below:

Essentially, it launches an Edge browser and opens your playlist. It provides a method to skip ads and resume playback if YouTube prompts a pause. Then, it retrieves the URL of each video played and saves it into an Excel file, looping until all videos in your playlist are processed.

Once we got all the url, cotinue step 2.

Step 2: Downloading the Audio from the URLs

Once you have the URLs saved, the next step is to download the audio from these videos and convert them into MP3 files. This can be efficiently done using a Python script with the help of the pytube library. Below is the Python notebook script that accomplishes this task:

import os
import pandas as pd
from pytube import YouTube

# Function to download audio as MP3
def download_audio(video_url, title):
stream = YouTube(video_url).streams.filter(only_audio=True).first()
filename = f"{title}.mp3"
file_path = os.path.join(save_dir, filename)
stream.download(output_path="", filename=filename)
os.rename(filename, file_path)

# Function to search for an Excel file in a specific directory and its subdirectories
def find_excel_file(folder_path):
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith(".xlsx"):
return os.path.join(root, file)
return None

# Read Excel file containing video URLs
def read_excel_and_download_audio(file_path):
if not file_path:
print("No Excel file found in the specified folder.")
return

# Get directory of the Excel file
excel_dir = os.path.dirname(file_path)

# Define directory to save audio files
global save_dir
save_dir = os.path.join(excel_dir, "audio")
os.makedirs(save_dir, exist_ok=True)

# Read Excel file
try:
df = pd.read_excel(file_path, engine='openpyxl')
except Exception as e:
print("Error reading Excel file:", e)
return

# Extract video URLs from the first column of Excel
video_urls = df.iloc[:, 0]

# Download audio for each video URL
for i, video_url in enumerate(video_urls, start=1):
try:
# Get YouTube video title from the URL
title = YouTube(video_url).title
print(f"Downloading audio for video {i}: {title}...")
download_audio(video_url, title)
print(f"Audio downloaded for video {i}")
except Exception as e:
print(f"Error downloading audio for video {i}: {e}")

# Define the directory to search for Excel files
search_directory = r'C:\Users\ASUS\Music\fav2'

# Search for an Excel file in the specified directory and its subdirectories
excel_file_path = find_excel_file(search_directory)

# Check if Excel file is found
if excel_file_path:
print("Excel file found:", excel_file_path)
# Read Excel and download audio
read_excel_and_download_audio(excel_file_path)
else:
print("No Excel file found in the specified folder.")

Essentially, it reads the Excel file generated in step 1, retrieves the video titles, and saves the audio files in the same folder where the Excel file is located.

--

--

bedy kharisma
Coinmonks

Indonesian Strategist,Analyst, Researcher, Achievement Addict who helps company grow their business by applying data-driven Management. Follow to follow