无法打开通过请求和urllib下载的下载的.mp4文件

时间:2020-06-15 12:33:19

标签: python windows python-requests urllib mp4

我已使用requests

下载了一些文件
url = 'https://www.youtube.com/watch?v=gp5tziO5lXg&feature=youtu.be'
video_name = url.split('/')[-1]

print("Downloading file:%s" % video_name)

# download the url contents in binary format
r = requests.get(url)

# open method to open a file on your system and write the contents
with open('saved.mp4', 'wb') as f:
    f.write(r.content)

并使用urllib.requests

url = 'https://www.youtube.com/watch?v=gp5tziO5lXg&feature=youtu.be'
video_name = url.split('/')[-1]

print("Downloading file:%s" % video_name)

# Copy a network object to a local file
urllib.request.urlretrieve(url, "saved2.mp4")

当我尝试打开.mp4文件时,出现以下错误

无法播放

该文件无法播放。发生这种情况的原因是文件类型为 不支持,文件扩展名不正确或文件是 损坏。

0xc00d36c4

Cannot open .mp4 file

如果我使用pytube进行测试,则效果很好。

其他方法怎么了?

2 个答案:

答案 0 :(得分:1)

要回答您的问题,使用其他方法不是下载视频而是下载页面。您可能会得到一个带有mp4文件扩展名的html文件。

因此,在尝试打开文件时会出现该错误。

如果pytube可以满足您的需求,我建议您使用那个。

如果要从其他平台下载视频,则可以考虑使用youtube-dl

答案 1 :(得分:-1)

您好,您可以导入IPython.display进行音频显示

import IPython.display as ipd
ipd.Audio(video_name)

致谢 希望我能解决您的问题

相关问题