无法打开使用请求和 urllib 下载的已下载 .mp4 文件

Can't open downloaded .mp4 files downloaded with requests and urllib

我使用 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 文件时,出现以下错误

Cannot play

This file cannot be played. This can happen because the file type is not supported, the file extension is incorrect or the file is corrupted.

0xc00d36c4

如果我用 测试它,它工作正常。

其他方法有什么问题?

您好,您可以导入 IPython.display 用于音频显示

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

问候 希望我能解决你的问题

回答你的问题,其他方法不是下载视频,而是下载页面。您可能获得的是一个文件扩展名为 mp4 的 html 文件。

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

如果 能满足您的需要,我建议您使用那个。

如果你想从其他平台下载视频,你可以考虑youtube-dl