38

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?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
speller
  • 1,641
  • 2
  • 20
  • 27
  • 1
    See this question and answers, this might be helpfull : http://stackoverflow.com/questions/2678051/cant-download-youtube-video – COD3BOY Nov 10 '11 at 15:32
  • 3
    possible duplicate of [Downloading video's in flv format from youtube](http://stackoverflow.com/questions/4287748/downloading-videos-in-flv-format-from-youtube) – Jakob Bowyer Nov 10 '11 at 18:42

5 Answers5

35

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.

Lycha
  • 9,937
  • 3
  • 38
  • 43
  • 13
    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. – John May 28 '14 at 11:21
  • 1
    @John That page links to [YouTube Terms of Service](https://www.youtube.com/t/terms) 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." – Lycha May 29 '14 at 05:34
  • 7
    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). – Scott Dec 27 '15 at 01:52
  • 11
    what if you are the owner of the channel and wish to download your own content? – filthy_wizard Sep 30 '19 at 14:09
  • There should be a download link (a medium-sized button) in the creator dashboard – py660 Apr 14 '23 at 21:05
  • @Scott 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. – ylj Aug 20 '23 at 14:28
29

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

Richie Bendall
  • 7,738
  • 4
  • 38
  • 58
Holy Mackerel
  • 3,259
  • 1
  • 25
  • 41
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.

poundifdef
  • 18,726
  • 23
  • 95
  • 134
dorvak
  • 9,219
  • 4
  • 34
  • 43
  • 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 video – Michael Mallett Jan 10 '13 at 18:35
  • 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. – dorvak Jan 29 '13 at 13:02
  • 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. – Luca Steeb Mar 25 '17 at 22:39
  • 1
    Nice! Any idea what protocol is used to download content. ? – Rilwan Mar 28 '17 at 07:49
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()
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Anil_M
  • 10,893
  • 6
  • 47
  • 74
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
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
JKumar
  • 11
  • 1