In this series, we will download YouTube videos and audio using the PyTubeFix and MoviePy libraries.
Let's get started!
Please follow the instructions:
- Create a new folder called "SongDownload" in your documents. You can use any name for the folder.
- Inside the SongDownload folder create a new file called index.py
- Open a terminal and CD to your songDownload Folder and install the following libraries. pip install moviepy and pip install pytubefix
- 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!")
- Modify the array named urls with your youtube links
- On your terminal execute the command python index.py
- Enjoy
Top comments (6)
First line in the code should start with
from
instead ofFrom
. Otherwise works. Thanks a lot!yes! i forgot to change that. thanks for pointing out :)
Interesting, so now i dont need shady websites with loads of ads? Thanks
Yes! You're welcome! :)
How do I do to download only the song from Youtube?
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?