user3035935
user3035935

Reputation: 241

Download video from URL in Python

I am trying to download a video using the below code in Python.

import urllib
dwn_link = 'https://class.coursera.org/textanalytics-001/lecture/download.mp4?lecture_id=73'

file_name = 'trial_video.mp4' 
urllib.retrieve(dwn_link, file_name)

But this code downloads only 382 kb and video open with an error.

Any help?

Edit: I could download all .pdf files in this page using their download links, but there seems to be some issue with video files. Video does get downloaded int my local system, but with error

Upvotes: 24

Views: 160880

Answers (5)

ddelange
ddelange

Reputation: 1585

To download a file with minimal memory footprint, you can use smart_open.

The code becomes quite pythonic, and it keeps only a small portion of the file in memory at a time:

# pip install smart_open[http]
from smart_open import open

def stream_uri(uri_in, uri_out, chunk_size=1 << 18):  # 256kB chunks
    """Write from uri_in to uri_out with minimal memory footprint."""
    with open(uri_in, "rb") as fin, open(uri_out, "wb") as fout:
        while chunk := fin.read(chunk_size):
            fout.write(chunk)

# from https to disk
stream_uri("https://ik.imagekit.io/demo/sample-video.mp4", "./sample-video.mp4")
# from s3 to ftp
stream_uri("s3://bucket1/example.pdf", "ftp://192.168.178.1:21/example.pdf")

They support a range of protocols which can be combined here.

Upvotes: 0

Dmitry Leiko
Dmitry Leiko

Reputation: 4412

You can use the library requests:

def download_video_series(video_links): 

for link in video_links: 

    '''iterate through all links in video_links 
    and download them one by one'''

    # obtain filename by splitting url and getting  
    # last string 
    file_name = link.split('/')[-1]    

    print "Downloading file:%s"%file_name 

    # create response object 
    r = requests.get(link, stream = True) 

    # download started 
    with open(file_name, 'wb') as f: 
        for chunk in r.iter_content(chunk_size = 1024*1024): 
            if chunk: 
                f.write(chunk) 

    print "%s downloaded!\n"%file_name 

print "All videos downloaded!"
return

Upvotes: 15

Ankit Jain
Ankit Jain

Reputation: 1036

In python 3,

import urllib.request
urllib.request.urlretrieve(url_link, 'video_name.mp4') 

It works for me and you can see the script at the following link

Upvotes: 44

Anand S Kumar
Anand S Kumar

Reputation: 90999

If you have access to urllib2 , you can use urlopen on the url , this would give back a response object , you can do response.read() to read the data and then write it to a file.

Example -

import urllib2
dwn_link = 'https://class.coursera.org/textanalytics-001/lecture/download.mp4?lecture_id=73'

file_name = 'trial_video.mp4' 
rsp = urllib2.urlopen(dwn_link)
with open(file_name,'wb') as f:
    f.write(rsp.read())

Also you need to make sure that you have authenticated to the server , if that is required for downloading the video.

I am not sure what kind of authentication coursera.org uses, but if its Basic HTTP Authentication (Which I highly doubt) , you can use -

password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
top_level_url = "http://class.coursera.org/"
password_mgr.add_password(None, top_level_url, username, password)

handler = urllib2.HTTPBasicAuthHandler(password_mgr)

# create "opener" (OpenerDirector instance)
opener = urllib2.build_opener(handler)

# use the opener to fetch a URL
opener.open(dwn_link)

Upvotes: 11

Grokify
Grokify

Reputation: 16354

To download that video from that Coursera class, you need to be:

  1. signed into a session for Coursera.org
  2. signed up for that class in Coursera.org

Once you do that, you can download the video after your HTTP client authenticates (with your username / password) and has a valid session.

Upvotes: 4

Related Questions