Download Free YouTube Videos and Spotify Music (v01).
Is it legal to download videos for free from platforms like YouTube? Now that I think about it, I'm not entirely sure (and it's not a priority for me). However, I find a significant portion of the content I'm interested in on YouTube. The problem is time: I often find topics that catch my attention and, for one reason or another, I'm not looking for them at the time. The solution for me is simple: download the video to watch offline whenever I can.
Many will think: "Just save it to a playlist and that's it!" But that method doesn't work for me: as I said before, I simply want the video on my device and available at all times. And of course, that's where the problems begin: To download, I have to access websites full of dubious ads, paid apps, or apps from unknown sources... The real headache we experience on a daily basis. A simple Python script is better! And as the saying goes: "on to something else, butterfly".
Before using the script, you need Python 3.x installed on your computer. Ideally, you should create a virtual environment (venv) to avoid conflicts with other libraries. Additionally, the script requires installing some additional Python packages. These dependencies are essential; with them installed, you'll be able to run the script without any problems:
- pytubefix: library for downloading YouTube videos (
pip install pytubefix
). - tqdm: displays progress bars (
pip install tqdm
). - spotDL: tool for downloading music from Spotify (
pip install spotdl
). - FFmpeg: required for spotDL to process audio. (
spotdl --download-ffmpeg
), or using your system's package manager, for example: sudo apt install ffmpeg on Linux.
How does the script work? (without too much technical jargon). The main flow of the program is simple. It basically performs the following steps:
1. The script asks the user for a list of URLs (from YouTube or Spotify).
2. For each link entered:
- If it's from YouTube, it calls `download_youtube(link)`.
- If it's from Spotify, it calls `download_spotify(link)`.
- If the link doesn't belong to any of these platforms, it displays an "unsupported link" message.
3. After processing all the links, the program exits.
Using the script.
To use the script, run it from the terminal with Python. Upon launch, it will prompt you to enter URLs one by one. Paste a link and press Enter; repeat with as many links as you want to download. When finished, leave a blank line, press Enter again, and the program will begin processing them. Each YouTube video will be downloaded to the current directory in MP4 format, and each Spotify song will be downloaded as an MP3 (with metadata included). The script will detect which function to use in each case, display the progress bar or spotDL status, and then print "Download complete" for each successful entry. If there are errors (e.g., an invalid link or no connection), a corresponding error message will be displayed.
STEPS:
1. Open Notepad.
2. Copy and paste the code.
--------------βοΈ
import os
from pytubefix import YouTube, request
from tqdm import tqdm
import subprocess
request.default_range_size = 1048576
progress_bar = None
def on_progress(video_stream, data_chunk, bytes_remaining):
global progress_bar
current_downloaded = video_stream.filesize - bytes_remaining
progress_update = current_downloaded - progress_bar.n
progress_bar.update(progress_update)
def download_youtube(video_url):
yt = YouTube(video_url, on_progress_callback=on_progress)
highest_res_stream = yt.streams.get_highest_resolution()
global progress_bar
progress_bar = tqdm(
total=highest_res_stream.filesize,
unit='B',
unit_scale=True,
desc=yt.title[:15]
)
highest_res_stream.download()
progress_bar.close()
print("β
Download complete:", yt.title)
def download_spotify(spotify_url):
print("π΅ Downloading from Spotify:", spotify_url)
try:
subprocess.run(["spotdl", spotify_url], check=True)
print("β
Spotify download complete!")
except subprocess.CalledProcessError as e:
print("β Error downloading from Spotify:", e)
def main():
video_urls = []
print("\nπ₯ Enter YouTube/YouTube Music/Spotify URLs (Press Enter on a blank line when done):")
while True:
link = input("URL: ").strip()
if not link:
break
video_urls.append(link)
if not video_urls:
print("β No URLs provided.")
return
for link in video_urls:
if "youtube.com" in link or "youtu.be" in link or "music.youtube.com" in link:
try:
download_youtube(link)
except Exception as e:
print("β Error downloading YouTube:", link, e)
elif "spotify.com" in link:
try:
download_spotify(link)
except Exception as e:
print("β Error downloading Spotify:", link, e)
else:
print("β Unsupported link:", link)
if __name__ == "__main__":
main()
--------------βοΈ
β¬ Download (Script) π https://drive.google.com/file/d/1yEUquXf2oXRxQTV0KSWYILGvBVW0krRc/view?usp=sharing
3. Save the file as `xxx.py` (or whatever you like).
4. Then double-click the file.
5. Copy and paste the URLs.
6. Done!
As you can see, a small Python script can solve the hassle of downloading content from YouTube and Spotify. Instead of dealing with pages full of ads or paid or subscription-based programs, we can also use open-source tools. The script is easy to use and scalable: you can download multiple links in batches with a single launch of the program. Plus, the visual feedback during the download makes everything more user-friendly. Ultimately, this homemade solution gives you full control over your downloads. Just remember that it's not magic: the final quality depends on what YouTube or Spotify offers, and downloads can take longer depending on the file size and internet speed.
Note: In the next article, I'll publish another version adding an additional script that creates a list of URLs for those who want to download an entire channel. This will save you the hassle of copying and pasting each address separately.
A brief disclaimer that can't be missed: This script is for educational purposes. I believe downloading content from YouTube or Spotify for personal use is permitted, but only if you always respect copyright laws and the platforms' policies. Actually, now that I read it, YouTube's Terms of Service expressly prohibit unauthorized downloading of its videos, and that it's only legal to download videos for private use and with the creator's permission. Therefore, each user is responsible for their use of the script. Always check your country's legislation and make sure you respect the authors' rights before downloading any content.
OTHER LINKS OF INTEREST:
π Get Your π³ BANCUS Crypto Debit Cards βΏ π (NO KYC
). This is a real game changer! One of the biggest barriers to mass adoption of cryptocurrencies has been the difficulty of spending them on a daily basis. BANCUS allows you to spend your crypto anywhere that accepts traditional debit cards. Imagine the freedom! Plus, you can stake or hold digital assets, including stablecoins, with significant returns π° 1 month: 0.83% (10% per year!), 3 months: 1% (12% per year!), 6 months: 1.33% (16% per year!), 12 months: 1.5% (18% per year!). π² Need capital but don't want to sell your precious Bitcoin or Ethereum? BANCUS has the solution! Crypto-collateralized loans work similarly to traditional loans, but using your crypto assets as collateral. Instead of a house or a car, you can use cryptocurrency as collateral to receive a loan in stablecoins or fiat currencies. π₯³ And best of all: BANCUS charges no hidden fees or account maintenance charges. Total transparency, just the way we like it! π
π https://go.BANCUS.io/?ref=V8KK8M5P
π DO YOU WANT TO MAKE MONEY? π° Access to financial opportunities may be limited for many, but... what if I told you that with π Zaffex Broker and the π€ GPTrading robot, you can forget about investment strategies and let technology work for you? π This is one of the easiest ways to earn cryptocurrencies βΏ. It optimizes and automatically executes your trading operations with high efficiency using Artificial Intelligence. And to give you a kick-start, you receive a $π²5 bonus upon registration, with no hidden conditions!
- π€ GPTrading: π https://app.gptrading.ai/invite/01JZJT6KQNTM3KZVMRQYA7NPGX
- π Zaffex Broker: π https://broker.zaffex.com/auth/register?referrerUserId=01JZJSMDFAW5ECTRZH852EYR0J
- βοΈ Tutorial: π Set up GPTrading BOT + Zaffex Broker + $5 (USDT) Bonus. π€ Automate your trading with GPTrading. πΈ Your success can start here.
https://youtu.be/XjS6ijIPP_A?si=rGz6BEEJ3gTIJZkw
π― REMEMBER: Trading is something anyone can do, but it's not for everyone. π² Investing in financial assets involves risk and can lead to losses if you don't trade with discipline and knowledge. Always trade responsibly and define your risk profile before starting.