从 Python 中的 URL 下载视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30953104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:13:05  来源:igfitidea点击:

Download video from URL in Python

pythondownload

提问by user3035935

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. Video does get downloaded int my local system, but with error

编辑:我可以使用他们的下载链接下载此页面中的所有 .pdf 文件,但视频文件似乎存在一些问题。视频确实在我的本地系统中下载,但有错误

回答by Grokify

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

要从 Coursera 课程下载该视频,您需要:

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

回答by Anand S Kumar

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

如果你有机会获得urllib2,可以使用urlopenurl,这将给回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.orguses, 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)

回答by Ankit Jain

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

它对我有用,您可以在以下链接中查看脚本

回答by Dmitry

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