• 欢迎光临~

Python爬取m3u8视频(多线程)- OK好用,代码在文章尾

开发技术 开发技术 2022-11-30 次浏览

摘自:https://www.cnblogs.com/python147/p/14511627.html

1.前言

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。

PS:如有需要Python学习资料的小伙伴可以点击下方链接自行获取

Python免费学习资料、代码以及交流解答点击即可加入


 

爬取视频的时候发现,现在的视频都是经过加密(m3u8),不再是mp4或者avi链接直接在网页显示,都是经过加密形成ts文件分段进行播放。

今天就教大家如果通过python爬取下载m3u8加密视频。

2.分析网页

1.电影视频来源


http://www.caisetv.com/
Python爬取m3u8视频(多线程)- OK好用,代码在文章尾

2.分析m3u8加密目录


http://www.caisetv.com/dongzuopian/chaidanzhuanjia/0-1.html
Python爬取m3u8视频(多线程)- OK好用,代码在文章尾

 

在视频播放的页面,通过F12可以查看网络数据包


https://xigua-cdn.haima-zuida.com/20210219/19948_fcbc225a/1000k/hls/index.m3u8
Python爬取m3u8视频(多线程)- OK好用,代码在文章尾

 

这里的ts就电影的加密分段视频


https://xigua-cdn.haima-zuida.com/20210219/19948_fcbc225a/1000k/hls/

上面的m3u8链接掉index.m3u8后,在拼上075a34cccdd000000.ts等ts名称就是分段视频的链接

如下所示:


https://xigua-cdn.haima-zuida.com/20210219/19948_fcbc225a/1000k/hls/075a34cccdd000000.ts

通过浏览器把这个分段视频下载后打开:

 

Python爬取m3u8视频(多线程)- OK好用,代码在文章尾

 

所以只要把所有的ts下载并合并就是完整的电影视频!!!

3.下载ts

1.下载ts分段视频

刚刚已经把ts的所有名称下载下来了

 

Python爬取m3u8视频(多线程)- OK好用,代码在文章尾

 

接下来通过python代码去读取这个文件,提取出名称,拼接链接后下载保存到一个文件夹里!


headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0',}

###下载ts文件
def download(url,name):
    r = requests.get(url, headers=headers)
    with open(name+"", "wb") as code:
        code.write(r.content)

with open("index.m3u8","r") as f:
    ts_list = f.readlines()

#去掉前面没用的信息
ts_list = ts_list[5:]
urlheader="https://xigua-cdn.haima-zuida.com/20210219/19948_fcbc225a/1000k/hls/"
count = 0
for i in ts_list:
    if "#" not in i:
        i = i.replace("n","")
        download(urlheader+""+i,"cdzj2/"+str(count)+".ts")
        count = count+1
        print(count)
Python爬取m3u8视频(多线程)- OK好用,代码在文章尾

 

这样就可以把ts文件全部下载下来,但是一个一个下载很慢,下面通过多线程下载,提升下载速度!!!

2.多线程下载ts视频


for i in ts_list:
    if "#" not in i:
        i = i.replace("n","")
        n = i[-7:]
        threading.Thread(target=download, args=(urlheader+""+i,"cdzj2/"+str(n),)).start()
        #download(urlheader+""+i,"cdzj2/"+str(count)+".ts")
Python爬取m3u8视频(多线程)- OK好用,代码在文章尾

 

通过多线程很快就可以将这些ts文件下载到本地!!!

4.合并ts

cmd合并文件


copy /b   *.ts   new.mp4

通过这个命令(cmd终端中运行),在含有ts文件的文件夹中就可以将ts文件合并(按名称顺序进行排列合并),并保存成new.mp4

 

Python爬取m3u8视频(多线程)- OK好用,代码在文章尾

5.总结

1.分析m3u8加密文件
2.python下载ts文件
3.cmd合并ts保存成mp4格式

 6.我的代码(全)

 1 # -*- coding:utf-8 -*-
 2 import os
 3 import requests
 4 import shutil
 5 import time
 6 import threading
 7 
 8 
 9 def download(url, name):
10     """下载ts文件"""
11 
12     headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0', }
13     r = requests.get(url, headers=headers)
14     with open(name + "", "wb") as code:
15         code.write(r.content)
16 
17 
18 def m3u8_download_multi_thread(m3u8, mp4, thread_max=10):
19     ts_in = mp4 + os.sep + "ts"
20     if not os.path.exists(ts_in):
21         os.makedirs(ts_in)  # mkdir -p ./a/b/c
22 
23     urlheader = os.path.dirname(m3u8)
24     m3u8_local = mp4 + "/index.m3u8"
25 
26     download(m3u8, m3u8_local)
27 
28     with open(m3u8_local, "r") as f:
29         ts_list = f.readlines()
30 
31     # 多线程
32     line = 0
33     count = 0
34     for i in ts_list:
35         # if count >= 10:
36         #     break
37         line = line + 1
38         while thread_max <= len(threading.enumerate()):
39             time.sleep(1)
40         if "#" not in i:
41             count = count + 1
42             i = i.replace("n", "")
43             print("line[%d]=%s" % (line, i))
44             n = os.path.basename(i)
45             try:
46                 threading.Thread(target=download, args=(urlheader + "/" + i, ts_in + "/" + str(n),)).start()
47                 # download(urlheader+""+i,"cdzj2/"+str(count)+".ts")
48             except Exception as e:
49                 print("threading.cnt=%d" % len(threading.enumerate()))
50 
51     # 等待所有子线程结束
52     while 1 < len(threading.enumerate()):
53         time.sleep(1)
54         # print("thread_count=%d" % len(threading.enumerate()))
55 
56 
57 def m3u8_download_single_thread(m3u8, mp4):
58     ts_in = mp4 + os.sep + "ts"
59     if not os.path.exists(ts_in):
60         os.makedirs(ts_in)
61 
62     urlheader = os.path.dirname(m3u8)
63     m3u8_local = mp4 + "/index.m3u8"
64 
65     download(m3u8, m3u8_local)
66 
67     with open(m3u8_local, "r") as f:
68         ts_list = f.readlines()
69 
70     # 单线程
71     line = 0
72     for i in ts_list:
73         line = line + 1
74         if "#" not in i:
75             print("line[%d]=%s" % (line, i))
76             i = i.replace("n", "")
77             n = os.path.basename(i)
78             download(urlheader + "/" + i, ts_in + "/" + str(n) + ".ts")
79 
80 
81 def ts_join(mp4):
82     if os.path.exists(mp4 + os.sep + mp4):
83         os.remove(mp4 + os.sep + mp4)
84 
85     os.chdir(mp4 + "/ts")
86 
87     # copy /b   *.ts   new.mp4
88     with os.popen("copy /b   *.ts   " + mp4, "r") as p:
89         l = p.read()
90         print(l)
91     shutil.move(mp4, "..")
92 
93 
94 if __name__ == '__main__':
95     m3u8 = "https://p2.bdstatic.com/rtmp.liveshow.lss-user.baidubce.com/live/stream_bduid_3041161111_7873701187/merged_1669738224764_293317_1160_30460.m3u8"
96     mp4 = "神舟十五号.mp4"
97     m3u8_download_multi_thread(m3u8, mp4)
98     # m3u8_download_single_thread(m3u8, mp4)
99     ts_join(mp4)

Python爬取m3u8视频(多线程)- OK好用,代码在文章尾

 

程序员灯塔
转载请注明原文链接:Python爬取m3u8视频(多线程)- OK好用,代码在文章尾
喜欢 (0)
违法和不良信息举报电话:022-22558618 举报邮箱:dljd@tidljd.com