speller
speller

Reputation: 1741

How to download a YouTube video using the YouTube's API

I looked at the Python's API overview: Developer's Guide: Python

But there isn’t any reference to how to download a video. How can I download videos using the API?

Upvotes: 41

Views: 139869

Answers (6)

wasif shah
wasif shah

Reputation: 21

You can also do it using yt_dlp to download youtube videos from what i know pytube does not work anymore,Here is the code:

#pip install yt_dlp # to install dependencies

import yt_dlp

SAVE_PATH = "/sui"  # Update the path where you want to save the video
link = "https://youtu.be/1-xGerv5FOk?si=NZmZQ9KnXPPgeBIY"

ydl_opts = {
    'format': 'best',
    'outtmpl': f'{SAVE_PATH}/%(title)s.%(ext)s',
}

try:
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        ydl.download([link])
    print('Video downloaded successfully!')
except Exception as e:
    print(f"Some Error! {e}") 

Upvotes: 2

Anil_M
Anil_M

Reputation: 11473

I know this posting is old, but thought would put in recent developments for anyone interested. From 2018 pytube is available which is lightweight library written in Python. It has no third party dependencies and aims to be highly reliable.

From the github page

pytube is a very serious, lightweight, dependency-free Python library (and command-line utility) for downloading YouTube Videos.

Downloading from youtube is easy one-liner.

from pytube import YouTube
YouTube('https://youtu.be/9bZkp7q19f0').streams.first().download()
yt = YouTube('http://youtube.com/watch?v=9bZkp7q19f0')
yt.streams
   .filter(progressive=True, file_extension='mp4')
   .order_by('resolution')
   .desc()
   .first()
   .download()

Upvotes: 20

JKumar
JKumar

Reputation: 11

I could download youTube video successfully using following code, if this helps someone. Note: i'm using colab

# install python dependencies
!pip install -q youtube-dl

from IPython.display import YouTubeVideo

YOUTUBE_ID = 'M6KD0pDrBkk'

YouTubeVideo(YOUTUBE_ID)

# download the youtube with the given ID
!youtube-dl -f 'bestvideo[ext=mp4]' --output "youtube.%(ext)s" https://www.youtube.com/watch?v=$YOUTUBE_ID

# cut the seconds between 15 and 20
!ffmpeg -y -loglevel info -i youtube.mp4 -ss 00:00:01.0 -t 5 video.mp4

Upvotes: 1

dorvak
dorvak

Reputation: 9719

There is obviously no api-side option, but you can simply use youtube-dl and call it via subprocess inside your python script, which is way easier/stable than using on standalone youtube-downloaders.

Upvotes: 26

Holy Mackerel
Holy Mackerel

Reputation: 3299

Check out Python API for YouTube, it downloads videos or can just get the direct URL to the video: https://pythonhosted.org/Pafy/

Upvotes: 31

Lycha
Lycha

Reputation: 10187

Downloading Youtube videos is against their Terms of Service, so their API's will not support that.

Page linked above refers to Youtube ToS that states:

You shall not download any Content unless you see a “download” or similar link displayed by YouTube on the Service for that Content.

Upvotes: 38

Related Questions