Download YouTube video with Python

How to download a Youtube video with Python easier than with Firefox Addons? Have your own youtube mp3 or mp4 downloader with Python script.

Install pytube Python library to download youtube videos:

pip install pytube

Downloading Youtube video to a folder with Python script

import pytube


# link with video to download
youtube_link = 'https://youtu.be/0cfss6jJejs'
youtube_video = pytube.YouTube(youtube_link)
stream = youtube_video.streams.get_highest_resolution()
stream.download()

Download the youtube video as mp3

Python youtube mp3 downloader. This script enables downloading YouTube videos as MP3 files..
The version where you are asked in prompt where to copy file.

# necessary libraries
from pytube import YouTube
import os

# request the user for the YouTube video URL
yt_link = YouTube(
str(input("Please provide the URL of the video you wish to download as mp3: \n>> ")))

# get the audio stream
audio_stream = yt_link.streams.filter(only_audio=True).first()

# ask for the desired download location
print("Specify the destination folder (hit Enter for the current directory)")
target_folder = str(input(">> ")) or '.'

# download and store the file
downloaded_file = audio_stream.download(output_path=target_folder)

# rename the file to mp3 format
file_base, file_ext = os.path.splitext(downloaded_file)
renamed_file = file_base + '.mp3'
os.rename(downloaded_file, renamed_file)

# inform the user of successful download
print(f"Successfully downloaded {yt_link.title} as an mp3.")

Here's a step-by-step breakdown of what the script does:

  1. The script first imports the necessary libraries - pytube for interacting with YouTube and os for interacting with the operating system.

  2. It then prompts the user to input a URL of a YouTube video they wish to download as an MP3.

  3. The script uses the YouTube function from the pytube library to create a YouTube object, which represents the YouTube video linked in the provided URL.

  4. It extracts the first audio-only stream from the video using the streams.filter(only_audio=True).first() method. This gives the highest quality audio stream available.

  5. The script then prompts the user to specify a download location. The user can input a path to a directory or hit Enter to use the current directory (the directory from where the script is being run).

  6. The download method is called on the audio stream to download the audio file, saving it to the specified (or current) directory.

  7. Once the file is downloaded, the script uses the os module to rename the file extension to .mp3. This is done by splitting the original file name into base and extension parts and then changing the extension to .mp3.

  8. Finally, the script prints a success message to the console, indicating that the download was successful and showing the title of the downloaded video.

The audio file's extension is changed to .mp3. The actual audio codec of the downloaded file will still be whatever YouTube used for the video (typically AAC or Opus). To convert the audio codec to MP3, a separate audio conversion library like pydub or ffmpeg would be needed.

Youtube video download as mp3, and manually add folder path and video URL

Here's the modified version of the code above, where you manually provide the YouTube video URL and download path in the script.

# necessary libraries
from pytube import YouTube
import os

# manually set YouTube video URL
youtube_video_url = "https://youtu.be/0cfss6jJejs" # replace with your video URL

# create YouTube video object
yt_link = YouTube(youtube_video_url)

# get the audio stream
audio_stream = yt_link.streams.filter(only_audio=True).first()

# manually set the download location
target_folder = "/path/to/your/folder" # replace with your target folder

# download and store the file
downloaded_file = audio_stream.download(output_path=target_folder)

# rename the file to mp3 format
file_base, file_ext = os.path.splitext(downloaded_file)
renamed_file = file_base + '.mp3'
os.rename(downloaded_file, renamed_file)

# inform the user of successful download
print(f"Successfully downloaded {yt_link.title} as an mp3.")

Replace the youtube_video_url and target_folder with your desired YouTube video URL and download path.

# importing packages
from pytube import YouTube
import os

# youtube url link
youtube_url = 'https://youtu.be/0cfss6jJejs'

# path to save mp3
destination = r'C:\Users\youtube-video-downloaded'

# url input from user
yt_video = YouTube(youtube_url)

# extract audio
video = yt_video.streams.filter(only_audio=True).first()

# check for a destination to save the file

destination = r'C:\Users\youtube-video-downloaded'

# download the file
out_file = video.download(output_path=destination)

# save the file
base, ext = os.path.splitext(out_file)
new_file = base + '.mp3'
os.rename(out_file, new_file)

Download all videos from the youtube playlist

This Python script will download all the videos in the given playlist, one after the other, in the highest available resolution, to the specified directory. If any of the videos are unavailable or cannot be downloaded for any reason, the script will skip them and move on to the next video.

import pytube
from pytube import Playlist

# Youtube playlist to download files
URL_PLAYLIST = "https://www.youtube.com/watch?v=_UlEPGDxvxk&list=PLQW82mL9dgCdc7lMuPHModILI4UJ6zfx-"
# path to save videos
destination = r'C:\Users\youtube-video-downloaded'

# get urls from youtube playlist
playlist = Playlist(URL_PLAYLIST)

for youtube_link in playlist:
youtube_video = pytube.YouTube(youtube_link)
stream = youtube_video.streams.get_highest_resolution()
out_file = stream.download(output_path=destination)

Here is what each part of the script does:

  • The pytube and Playlist modules from pytube library are imported initially. Pytube is a lightweight, dependency-free Python library used for downloading videos from the web.

  • URL_PLAYLIST is a variable that holds the URL of the YouTube playlist from which you want to download videos.

  • destination is a variable that stores the directory's path where the downloaded videos will be saved. It is set to a folder named youtube in the Users directory on the C: drive.

  • The Playlist function is called with URL_PLAYLIST as an argument, and the result (which represents the playlist) is stored in the playlist variable.

  • The script then loops over each video in the playlist. For each video, it does the following:

    • Calls the YouTube function from pytube with the video's URL as an argument to create a YouTube object representing the video. This object is stored in the youtube_video variable.

    • Calls the streams.get_highest_resolution() method on the YouTube object to get the highest resolution stream. This is stored in the stream variable.

    • Calls the download method on the stream object to download the video. The output_path argument is set to destination, which means the video will be saved in the directory specified by destination.

How to download all videos from the youtube channel Python?

The easiest option is to get the list of all playlists from the channel and use the above script.

All Python libraries to download youtube videos

There are a few other Python libraries that you can use to download videos from YouTube:

  • youtube-dl is a command-line tool that can be used to download videos from various websites, including YouTube. It is a more powerful tool than pytube but can be more challenging.
  • yt-dlp is a fork of youtube-dl that is actively maintained and updated. It has several features that are not available on youtube-dl, such as the ability to download videos from private channels.
  • pytube3 is a newer pytube version still under development. It is designed to be more stable and reliable than the original pytube library.
LibraryFeaturesProsCons
pytubeEasy to use, lightweightSimple to install, no dependenciesDoes not support all features of YouTube
youtube-dlPowerful, supports a wide range of websitesEasy to use, command-line interfaceCan be difficult to learn, not as well-maintained as pytube
yt-dlpFork of youtube-dl with additional featuresActively maintained, updated regularlyNot as well-known as youtube-dl
pytube3Newer version of pytube, more stableSimple to install, no dependenciesNot as many features as pytube

Ultimately, the best library for you will depend on your needs and preferences. If you are looking for a simple and easy-to-use library, then pytube is a good choice. If you need a more robust library with more features, then youtube-dl or yt-dlp are good options. And if you are looking for a newer version of pytube with more stability, then pytube3 is a good choice.

Published