2021.6.18补充:未失效,但需要一些特殊的方法绕过cdn的限制

本脚本已于2020/12/16失效,大概讲讲相应的思路吧。

1.腾讯视频存在部分付费资源,例如《云首发相声大会》等,需要付费才能完整观看。
我们简单看一下这种付费的m3u8链接和普通的m3u8文件有何不同吧。

https://*.v.smtcdns.com/vipts.tc.qq.com/.../055_z00341icz95.321004.ts.m3u8?ver=4
 
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-TARGETDURATION:15
#EXT-X-PLAYLIST-TYPE:VOD
#EXTINF:12.000,
00_z00341icz95.321004.1.ts?index=0&start=0&end=12000&brs=0&bre=2900275&ver=4
#EXTINF:12.280,
01_z00341icz95.321004.1.ts?index=1&start=12000&end=24280&brs=2900276&bre=6104547&ver=4
#EXTINF:11.560,
02_z00341icz95.321004.1.ts?index=2&start=24280&end=35840&brs=6104548&bre=9642143&ver=4
#EXTINF:12.360,
03_z00341icz95.321004.1.ts?index=3&start=35840&end=48200&brs=9642144&bre=16151831&ver=4
#EXTINF:9.520,
04_z00341icz95.321004.1.ts?index=4&start=48200&end=57720&brs=16151832&bre=19113019&ver=4
#EXT-X-ENDLIST

可以看到里面只有四个分片。

https://*.v.smtcdns.com/vipts.tc.qq.com/.../o0035306o0e.321004.ts.m3u8?ver=4
 
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-TARGETDURATION:14
#EXT-X-PLAYLIST-TYPE:VOD
#EXTINF:6.875,
00_o0035306o0e.321004.1.ts?index=0&start=0&end=6875&brs=0&bre=1163343&ver=4
#EXTINF:12.500,
01_o0035306o0e.321004.1.ts?index=1&start=6875&end=19375&brs=1163344&bre=3374787&ver=4
#EXTINF:12.500,
02_o0035306o0e.321004.1.ts?index=2&start=19375&end=31875&brs=3374788&bre=5565739&ver=4
#EXTINF:12.500,
03_o0035306o0e.321004.1.ts?index=3&start=31875&end=44375&brs=5565740&bre=11094067&ver=4
#EXTINF:12.500,
04_o0035306o0e.321004.1.ts?index=4&start=44375&end=56875&brs=11094068&bre=13035731&ver=4
#EXTINF:12.500,
05_o0035306o0e.321004.1.ts?index=5&start=56875&end=69375&brs=13035732&bre=16923007&ver=4
...
#EXTINF:5.548,
0537_o0035306o0e.321004.17.ts?index=537&start=5649833&end=5655381&brs=131368008&bre=131484003&ver=4
#EXT-X-ENDLIST

2.大概几年前的时候,腾讯视频在网页端提供的还是5-6分钟一段的MP4片段,后来虽然开始做分片,但完整片段的url依然是可以使用的,简单来说就可以把m3u8文件简化为形如https://*.v.smtcdns.com/vipts.tc.qq.com/…/vid.321004.*.ts?ver=4的片段。
对于付费资源来说,我们可能只能获取到第一个分片的地址,如https://*.v.smtcdns.com/vipts.tc.qq.com/…/vid.321004.1.ts?ver=4,但把分片的index手动替换后会发现,后续的分片也是可以下载的,例如https://*.v.smtcdns.com/vipts.tc.qq.com/…/vid.321004.4.ts?ver=4

3.想法大概是,腾讯(以前)没有做url是普通链接还是预览链接的鉴定,所以只需要拼出分片就可以下载。所以写了如下的脚本来将预览链接变为可下载的链接。

4.但是2020/12/16发现,腾讯做了相关的鉴权,前几个分片可以自由下载,但之后的分片被限速了,根本不可能完整下载完整个视频。

5.因为本脚本已经失效,所以将相关的思路整理到这里,感兴趣的也可以试试看有没有办法解除速度限制。over:)

  1. import requests
  2. import os
  3. from urllib.parse import urljoin
  4.  
  5. cfg ={
  6.     "corePath": r"D:\Tool\N_m3u8DL-CLI\N_m3u8DL-CLI_v2.5.7.exe",
  7.     "workDir": r"D:\Tool\N_m3u8DL-CLI"
  8. }
  9.  
  10.  
  11. def download(name):
  12.     print("Ready to download...")
  13.     os.system(cfg["corePath"]+" \"" + name + ".m3u8\" --workDir \"" + cfg["workDir"] + "\" --saveName \"" + name + "\" --minThreads \"30\" --enableDelAfterDone")
  14.     print("Download Complete!")
  15.  
  16.  
  17. def createdata(url):
  18.     headers = {
  19.         "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36",
  20.         "Cookie": "",
  21.         "Range": "bytes=0-10"
  22.     }
  23.     data = "#EXTM3U\n#EXT-X-VERSION:3\n#EXT-X-TARGETDURATION:360\n"
  24.     index = 1
  25.     while True:
  26.         clipurl = url.replace("*", str(index))
  27.         print("Trying to get clip %d..." % index, end="")
  28.         i = 1
  29.         while i <= 3:
  30.             try:
  31.                 r = requests.get(clipurl, headers=headers, timeout=2)
  32.                 if r.status_code == 206:
  33.                     print("OK")
  34.                     data += "#EXTINF:300\n%s\n" % clipurl
  35.                     i = 4
  36.                 elif r.status_code == 404:
  37.                     print("Not Exist...Leaving...")
  38.                     i = 4
  39.                 else:
  40.                     print("Unexpected Error...Leaving...")
  41.                     i = 4
  42.             except requests.exceptions.RequestException:
  43.                 i += 1
  44.         if r.status_code != 206:
  45.             break
  46.         index += 1
  47.     data += "#EXT-X-ENDLIST"
  48.     return data
  49.  
  50.  
  51. def createfile(data, name):
  52.     print("Starting writing m3u8 file...")
  53.     with open(name + ".m3u8", "w+", encoding="utf8") as file:
  54.         file.write(data)
  55.     print("Writing to m3u8 file...Finished")
  56.  
  57.  
  58. def standardclip(url):
  59.     relapath = url.split("/")[-1].split("_")[-1].replace(".m3u8", "")
  60.     index = max(relapath.find("321004"), relapath.find("320181"), relapath.find("326002"), relapath.find("322016"), relapath.find("322014"))
  61.     relapath = relapath[:index+6] + ".*" + relapath[index+6:]
  62.     return urljoin(url, relapath)
  63.  
  64.  
  65. def main():
  66.     print("This tool is using to download video from TencentVideo for free...")
  67.     m3url = input("Please input the m3u8 url:")
  68.     taskname = input("Please input the task name:")
  69.     clipurl = standardclip(m3url)
  70.     data = createdata(clipurl)
  71.     createfile(data, taskname)
  72.     download(taskname)
  73.  
  74.  
  75. if __name__ == '__main__':
  76.     main()

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注