简体   繁体   English

从 Python 中的 URL 下载视频

[英]Download video from URL in Python

I am trying to download a video using the below code in Python.我正在尝试使用以下 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.但是此代码仅下载 382 kb 并且视频打开时出错。

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.编辑:我可以使用他们的下载链接下载此页面中的所有 .pdf 文件,但视频文件似乎存在一些问题。 Video does get downloaded int my local system, but with error视频确实在我的本地系统中下载,但有错误

In python 3,在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它对我有用,您可以在以下链接中查看脚本

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.如果您有权访问urllib2 ,则可以在url上使用urlopen ,这将返回一个response对象,您可以执行response.read() read数据,然后将其写入文件。

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 -我不确定coursera.org使用什么样的身份验证,但如果它的基本 HTTP 身份验证(我非常怀疑),你可以使用 -

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)

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

To download that video from that Coursera class, you need to be:要从 Coursera 课程下载该视频,您需要:

  1. signed into a session for Coursera.org登录 Coursera.org 的会话
  2. signed up for that class in Coursera.org在 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.完成此操作后,您可以在 HTTP 客户端进行身份验证(使用您的用户名/密码)并具有有效会话后下载视频。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM