How to download a YouTube video using the YouTube's API
Asked Answered
C

5

39

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?

Closestool answered 10/11, 2011 at 15:10 Comment(2)
See this question and answers, this might be helpfull : https://mcmap.net/q/409079/-can-t-download-youtube-videoUnseen
possible duplicate of Downloading video's in flv format from youtubeLibretto
B
36

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.

Byyourleave answered 10/11, 2011 at 15:16 Comment(6)
The world "download" doesn't appear in their terms of services, and even if it did, why on earth wouldn't they repeat that information on in the api documentation? A video hosting platform's api not supporting video downloads - you'd think that might be worth a sentence.Sanction
@Sanction That page links to YouTube Terms of Service 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."Byyourleave
yes, I realize that it's a bit too late to comment much anymore, but I gave a -1 because your answer didn't really answer the question. It obviously is possible (youtubeinmp3.com, for example).Latinize
what if you are the owner of the channel and wish to download your own content?Vacillation
There should be a download link (a medium-sized button) in the creator dashboardAgnosia
@Latinize The questioner asked if it is possible to use the API and the answer is no, it is not possible to use the API. You can download using the stream recording - not with the API. The answer also explains why.Humphries
O
30

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

Oringa answered 25/5, 2013 at 11:3 Comment(0)
D
26

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.

Drillstock answered 30/8, 2012 at 15:17 Comment(4)
Do you happen to know if its possible to get this program to output a publicly accessible link to download the content, rather than actually downloading the content? I want to pass the link to zencoder to do the actual processing of the videoAlatea
No, i think this is not possible (i do not know the basics behind the video-stream, but i think the is no direct link available, but a cryptic combination). Maybe the coders of youtube-dl might help you regarding this one.Drillstock
Just adding this for people who might see this in the future: you can simple use --dump-json to fetch a json object with the qualities, urls and infos.Feeling
Nice! Any idea what protocol is used to download content. ?Hayes
L
19

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()
Letha answered 28/10, 2018 at 2:27 Comment(0)
R
1

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
Romanfleuve answered 25/6, 2022 at 1:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.