Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download video from a direct URL with Python

Tags:

python

video

I want to download videos with python. I tried using youtube-dl, but the site I wish to download videos from is not supported. How to download videos in Python. First I tried to get the direct link of the video I want to download from keepvid.com after following this link http://www.kmcgraphics.com/bits-of-code/how-to-get-the-direct-url-path-to-flv-video-files-on-youtube-for-free/. I got the following link http://www.animefun.com/dl/googDev.php?url=/108994262975881368074/Po270 When I tried to run the following piece of code I am getting errors.

import urllib
test=urllib.URLopener()
test.retrieve("http://www.animefun.com/dl/googDev.php?url=/108994262975881368074/Po270.flv","testout.flv")

Error:

Traceback (most recent call last):
File "downl.py", line 14, in <module>
test.retrieve("http://www.animefun.com/dl/googDev.php?url=/108994262975881368074/Po270.flv","testout.flv")
File "/usr/lib/python2.7/urllib.py", line 240, in retrieve
fp = self.open(url, data)
File "/usr/lib/python2.7/urllib.py", line 208, in open
return getattr(self, name)(url)
File "/usr/lib/python2.7/urllib.py", line 359, in open_http
return self.http_error(url, fp, errcode, errmsg, headers)
File "/usr/lib/python2.7/urllib.py", line 376, in http_error
return self.http_error_default(url, fp, errcode, errmsg, headers)
File "/usr/lib/python2.7/urllib.py", line 381, in http_error_default
raise IOError, ('http error', errcode, errmsg, headers)
IOError: ('http error', 301, 'Moved Permanently', <httplib.HTTPMessage instance at 0x7f094d5d5290>)

I am new to Python. So please help me out.

like image 423
harun_a Avatar asked Jan 02 '15 14:01

harun_a


People also ask

Can I use Python to download files from website?

Requests is a versatile HTTP library in python with various applications. One of its applications is to download a file from web using the file URL.

How do you download an image from a URL in Python?

Use requests. get() to download an image get(url) with url as the address of the file to download and save the response to a variable. Use open(filename, mode) with mode as "wb" to open a stream of filename in write-and-binary mode.

Can I download YouTube videos with Python?

Using Python, this task is very easy. Few lines of code will download the video from YouTube for you. For this, there a python library named as 'pytube'. pytube is a lightweight, dependency-free Python library which is used for downloading videos from the web.


1 Answers

urllib.URLopener doesn't handle redirects by default

Use urllib.FancyURLopener instead:

import urllib
test=urllib.FancyURLopener()
test.retrieve("http://www.animefun.com/dl/googDev.php?url=/108994262975881368074/Po270.flv","testout.flv")
like image 98
Ngenator Avatar answered Oct 03 '22 21:10

Ngenator