我一直在使用下面的脚本下载技术视频以供以后分析。该脚本对我来说效果很好,并检索了我需要的视频可用的最高分辨率版本。
现在我遇到了 4K YouTube video ,而我的脚本仅保存 1280x720 的 mp4。
我想知道是否有办法调整我当前的脚本以下载该视频的更高分辨率版本。我知道有一些 python 包可以解决这个问题,但现在如果可能的话,我想坚持这种分步方法。
上图:来自 Quicktime 和 OSX 的信息
"""
length: 175 seconds
quality: hd720
type: video/mp4; codecs="avc1.64001F, mp4a.40.2"
Last-Modified: Sun, 21 Aug 2016 10:41:48 GMT
Content-Type: video/mp4
Date: Sat, 01 Apr 2017 16:50:16 GMT
Expires: Sat, 01 Apr 2017 16:50:16 GMT
Cache-Control: private, max-age=21294
Accept-Ranges: bytes
Content-Length: 35933033
Connection: close
Alt-Svc: quic=":443"; ma=2592000
X-Content-Type-Options: nosniff
Server: gvs 1.
"""
import urlparse, urllib2
vid = "vzS1Vkpsi5k"
save_title = "YouTube SpaceX - Booster Number 4 - Thaicom 8 06-06-2016"
url_init = "https://www.youtube.com/get_video_info?video_id=" + vid
resp = urllib2.urlopen(url_init, timeout=10)
data = resp.read()
info = urlparse.parse_qs(data)
title = info['title']
print "length: ", info['length_seconds'][0] + " seconds"
stream_map = info['url_encoded_fmt_stream_map'][0]
vid_info = stream_map.split(",")
mp4_filename = save_title + ".mp4"
for video in vid_info:
item = urlparse.parse_qs(video)
print 'quality: ', item['quality'][0]
print 'type: ', item['type'][0]
url_download = item['url'][0]
resp = urllib2.urlopen(url_download)
print resp.headers
length = int(resp.headers['Content-Length'])
my_file = open(mp4_filename, "w+")
done, i = 0, 0
buff = resp.read(1024)
while buff:
my_file.write(buff)
done += 1024
percent = done * 100.0 / length
buff = resp.read(1024)
if not i%1000:
percent = done * 100.0 / length
print str(percent) + "%"
i += 1
break
最佳答案
好吧,所以我还没有花时间去探究这个问题的真相。然而,我确实发现当你这样做时:
stream_map = info['url_encoded_fmt_stream_map'][0]
不知何故,您只能选择一个 720p 选项,一个“中”和两个“小”。
但是,如果您将该行更改为:
stream_map = info['adaptive_fmts'][0]
您将获得所有可用版本,包括 2160p
版本。因此,4K 的。
PS:您必须注释掉打印质量
和打印类型
命令,因为这些标签在新吞吐量中并不总是可用。然而,当注释掉它们并按照上面的说明调整脚本时,我能够成功下载 4K 版本。
关于python - 有没有办法修改这个 YouTube 下载脚本来访问 4K 版本的视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43159565/