Shivam Mitra
Shivam Mitra

Reputation: 1052

Getting YouTube URL for downloading video

I am writing a Python script for downloading a YouTube video. I am sending a request to a certain YouTube URL and getting it's response. Now, I want to get the URL of that video so that I can download it. All the answers I have found are outdated. Where in the response is the video URL hidden? Or there is no way to get the video URL?

import urllib2
import json
f = open("video.txt","w")
response = urllib2.urlopen("https://youtu.be/OulN7vTDq1I")
f.write(response.read())

And I got something like this in embed part of response:

<link rel="alternate" type="application/json+oembed" href="http://www.youtube.com/oembed?format=json&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DOulN7vTDq1I" title="Badshah - DJ Waley Babu | feat Aastha Gill | Party Anthem Of 2015 | DJ Wale Babu">
  <link rel="alternate" type="text/xml+oembed" href="http://www.youtube.com/oembed?format=xml&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DOulN7vTDq1I" title="Badshah - DJ Waley Babu | feat Aastha Gill | Party Anthem Of 2015 | DJ Wale Babu">

Upvotes: 3

Views: 15142

Answers (1)

mrDinkelman
mrDinkelman

Reputation: 496

Use pyTube - lightweight, dependency-free Python library (and command-line utility) for downloading YouTube Videos.

https://github.com/nficano/pytube

Upvotes: 1

Related Questions