首页 > 编程语言 >Python爬取m3u8视频(多线程)- OK好用,代码在文章尾

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

时间:2022-11-30 02:55:53浏览次数:60  
标签:count 视频 OK m3u8 ts mp4 多线程 下载

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

1.前言

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

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

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


 

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

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

2.分析网页

1.电影视频来源


http://www.caisetv.com/

2.分析m3u8加密目录


http://www.caisetv.com/dongzuopian/chaidanzhuanjia/0-1.html

 

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


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

 

这里的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

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

 

 

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

3.下载ts

1.下载ts分段视频

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

 

 

接下来通过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)

 

这样就可以把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")

 

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

4.合并ts

cmd合并文件


copy /b   *.ts   new.mp4

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

 

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)

 

标签:count,视频,OK,m3u8,ts,mp4,多线程,下载
From: https://www.cnblogs.com/LiuYanYGZ/p/16937272.html

相关文章

  • 网上一个哥们写的,使用Python写一个m3u8多线程下载器 -- 没用起来
    摘自:https://blog.csdn.net/muslim377287976/article/details/104340242/ 文章目录I.挖坑缘由II.功能/更新记录III.代码1.GUI2.下载工具类3.逻辑代码IV.下载地址I.挖坑......
  • [XState] Invoke actor
    InvokebyPromise:import{createMachine,interpret,send}from"xstate";constmachine=createMachine({initial:"loading",states:{loading:{......
  • react常用hook中useMemo的使用
     作用:首先useMemo它使用来做缓存用的,只有当一个依赖项改变的时候才会发生变化,否则拿缓存的值,就不用在每次渲染的时候再做计算useMemo主要用来解决使用Reacthooks产生的......
  • python接口自动化44- requests 库使用 hook 机制
    前言requests是Hooks即钩子方法,用于在某个框架固定的某个流程执行是捎带执行(钩上)某个自定义的方法。requests库只支持一个response的钩子,即在响应返回时可以捎带执......
  • jupyter notebook 踩坑记录
    1.安装软件路径不能是中文2.用户名不能是中文,否则找不到解释器#https://blog.csdn.net/weixin_51684729/article/details/124056544"""1.Win+R打开运行窗口,输入regedi......
  • [Zookeeper-3.6.2源码解析系列]-13-Zookeeper使用到的Reactor网络模型原理分析
    目录​​13-启服务端网络监听连接NIOServerCnxnFactory​​​​13.1简介​​​​13.2主从Reactor网络IO模型main-subreactor​​​​13.3NIOServerCnxnFactory的初始化......
  • 关于Cookie
     1.测试:Cookie和端口无关,和ip或域名有关localhost:81设置cookie           :   请求发现请求头有cookielocalhost:82不设置cookie  ......
  • java多线程模型
    一、线程状态:  新建状态(New):新创建了一个线程对象。就绪状态(可执行状态,Runnable):线程对象创建后,其他线程调用了该对象的start()方法。该状态的线程位于可运行线程池中,......
  • python爬取m3u8视频文件
    importrequestsimportosimportaiohttpimportasynciofirst_m3u8_url="https://cdn.zoubuting.com/20221129/waHIjBSS/index.m3u8"headers={'User-Agent......
  • hook_link和hook_link_alter钩子函…
    Drupal中有个钩子,叫做hook_link,参数如下:hook_link($type,$object,$teaser=FALSE),第一个参数$type,可能的值为node,comment等。$object,如果type为node,则......