我已使用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)
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
如果我使用pytube进行测试,则效果很好。
其他方法怎么了?
答案 0 :(得分:1)
要回答您的问题,使用其他方法不是下载视频而是下载页面。您可能会得到一个带有mp4文件扩展名的html文件。
因此,在尝试打开文件时会出现该错误。
如果pytube可以满足您的需求,我建议您使用那个。
如果要从其他平台下载视频,则可以考虑使用youtube-dl。
答案 1 :(得分:-1)
您好,您可以导入IPython.display进行音频显示
import IPython.display as ipd
ipd.Audio(video_name)
致谢 希望我能解决您的问题