DEV Community

Caesar Herman Hinlo
Caesar Herman Hinlo

Posted on

Download multiple YouTube videos and audio using Python.

In this series, we will download YouTube videos and audio using the PyTubeFix and MoviePy libraries.
Let's get started!

Please follow the instructions:

  1. Create a new folder called "SongDownload" in your documents. You can use any name for the folder.
  2. Inside the SongDownload folder create a new file called index.py
  3. Open a terminal and CD to your songDownload Folder and install the following libraries. pip install moviepy and pip install pytubefix
  4. Copy the code from this document and paste it into the index.py file.
From pytubefix import YouTube
from pytubefix.cli import on_progress
from moviepy.editor import *

#Modify the URLs with your YouTube links
urls = [
    "https://www.youtube.com/watch?v=PU7f1t0Q-Ww&t=1433s",  # Bee gees
    "https://www.youtube.com/watch?v=P5aCBIY3ZeA",  # Queen
    "https://www.youtube.com/watch?v=LI0Gt5hmi68",  # Carpenters
    "https://www.youtube.com/watch?v=H16mqmLYia4",  # Abba
    "https://www.youtube.com/watch?v=JJtBKODCUTQ",  # Air supply

]

for index, url in enumerate(urls):
    yt = YouTube(url, on_progress_callback=on_progress)
    print(f"Progress:({index+1}/{len(urls)})")
    print(f"Title: {yt.title}")

    video_download = yt.streams.get_highest_resolution()
    print("Downloading Video...")
    video_download.download(filename=f"{yt.title}.mp4")

    video = VideoFileClip(f"{yt.title}.mp4")
    video.audio.write_audiofile(f"{yt.title}.mp3")

    print("-" * 60)
print("DOWNLOAD FINISHED. OPEN DESTINATION FOLDER AND ENJOY!")
Enter fullscreen mode Exit fullscreen mode
  1. Modify the array named urls with your youtube links
  2. On your terminal execute the command python index.py
  3. Enjoy

Top comments (4)

Collapse
 
martinbaun profile image
Martin Baun

Interesting, so now i dont need shady websites with loads of ads? Thanks

Collapse
 
wilyz360 profile image
William Zhicay

How do I do to download only the song from Youtube?

Collapse
 
ninacomputer profile image
Nina Chan

looks like it downloads the whole mp4 and then extracts the mp3. so you can just write a line to rm the mp4 and leave the mp3 perhaps?