Python解析m3u8拼接下载mp4视频文件的示例代码
作者:404NooFound 时间:2022-04-22 13:20:16
一、关于m3u8:
m3u8是苹果公司推出一种视频播放标准,是m3u的一种,不过编码方式是utf-8,是一种文件检索格式,将视频切割成一小段一小段的ts格式的视频文件,然后存在服务器中(现在为了减少I/o访问次数,一般存在服务器的内存中),通过m3u8解析出来路径,然后去请求。
示例:
#EXTM3U
#EXT-X-TARGETDURATION:10
#EXTINF:9,
http://data.video.iqiyi.com/videos/vts/20210301/69/b8/73ad4ef04fde4586ef2799ecd67241ce.ts?qypid=2645242154145600_04000000001000000000_96&start=496696&end=779448&contentlength=282752&sd=20200&qdv=1&qd_uid=0&qd_tvid=2645242154145600&qd_vip=0&qd_src=02029022240000000000&qd_tm=1614590393988&qd_ip=0&qd_p=0&qd_k=4eb685f1966cbd08e6a9648fe0b8c007&ve=&sgti=&dfp=&qd_sc=f9a4f133a622871b6739734615ef178d
#EXTINF:9,
http://data.video.iqiyi.com/videos/vts/20210301/69/b8/73ad4ef04fde4586ef2799ecd67241ce.ts?qypid=2645242154145600_04000000001000000000_96&start=779448&end=1008432&contentlength=228984&sd=29200&qdv=1&qd_uid=0&qd_tvid=2645242154145600&qd_vip=0&qd_src=02029022240000000000&qd_tm=1614590393988&qd_ip=0&qd_p=0&qd_k=4eb685f1966cbd08e6a9648fe0b8c007&ve=&sgti=&dfp=&qd_sc=f9a4f133a622871b6739734615ef178d
#EXTINF:9,
http://data.video.iqiyi.com/videos/vts/20210301/69/b8/73ad4ef04fde4586ef2799ecd67241ce.ts?qypid=2645242154145600_04000000001000000000_96&start=5934408&end=6141020&contentlength=206612&sd=198733&qdv=1&qd_uid=0&qd_tvid=2645242154145600&qd_vip=0&qd_src=02029022240000000000&qd_tm=1614590393988&qd_ip=0&qd_p=0&qd_k=4eb685f1966cbd08e6a9648fe0b8c007&ve=&sgti=&dfp=&qd_sc=f9a4f133a622871b6739734615ef178d
#EXT-X-ENDLIST
预览器打开会出现下载ts文件
我们想要的mp4文件就是一个个ts文件按照顺序拼接成的,废话不多说直接上代码。
# -*- coding:utf-8 -*-
"""
Author:SPIDERMAN
Time: 2021/3/1
Software: PyCharm
"""
import logging
import os
from glob import iglob
import requests
import m3u8
from urllib.parse import urljoin
from concurrent.futures import ThreadPoolExecutor
from natsort import natsorted
class M3u8Download:
def __init__(self,m3u8_url):
self.m3u8_url = m3u8_url
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
}
self.threadpool = ThreadPoolExecutor(max_workers=10)
self.file_name = 'weibo.mp4'
logging.basicConfig(format='[%(asctime)s][*%(levelname)s]:%(message)s',
level=logging.INFO)
def get_ts_url(self,m3u8_url):
"""
解析ts_url
:param m3u8_url:
:return:
"""
m3u8_obj = m3u8.load(m3u8_url)
base_uri = m3u8_obj.base_uri
logging.info('[*]get_base_uri'+base_uri)
for seg in m3u8_obj.segments:
yield urljoin(base_uri, seg.uri)
def download__ts(self, urlinfo):
"""
下载ts文件
:param urlinfo:
:return:
"""
url, ts_name = urlinfo
res = requests.get(url, headers=self.headers)
with open(ts_name, 'wb') as fp:
fp.write(res.content)
logging.info('[*download]'+ts_name)
def download_all_ts(self):
"""
下载所有函数
:return:
"""
ts_urls = self.get_ts_url(self.m3u8_url)
logging.info('[*download]download:'+self.m3u8_url)
for index, ts_url in enumerate(ts_urls):
print(ts_url)
self.threadpool.submit(self.download__ts, [ts_url, f'{index}.ts'])
self.threadpool.shutdown()
def remove_ts(self,ts_path):
"""
删除ts文件
:param ts_path:
:return:
"""
for ts in iglob(ts_path):
os.remove(ts)
logging.info('[*remove]remove all *.ts')
def run(self):
self.download_all_ts()
ts_path = '*.ts'
all_ts = iglob(ts_path)
with open(self.file_name, 'wb') as fn:
#根据ts排序
for ts in natsorted(all_ts):
#读ts写mp4
with open(ts, 'rb') as ft:
scline = ft.read()
fn.write(scline)
self.remove_ts(ts_path)
if __name__ == '__main__':
m3u8Download = M3u8Download('https://cache.m.iqiyi.com/mus/1618469868576801/a34fec3fc63db2c1bb4c15f53cd513e1/afbe8fd3d73448c9/0/20210301/69/b8/670962cfd6b9166c87a21728808fe6a2.m3u8?qd_originate=tmts_py&tvid=2645242154145600&bossStatus=0&qd_vip=0&px=&src=02029022240000000000&prv=&previewType=&previewTime=&from=&qd_time=1614590979725&qd_p=0&qd_asc=636bb14ab52facec684335546d2d60ec&qypid=2645242154145600_04000000001000000000_96&qd_k=4eb685f1966cbd08e6a9648fe0b8c007&isdol=0&code=2&ff=f4v&iswb=1&vf=2198359907d7f173fed0b6eabca18b29&np_tag=nginx_part_tag&pt_sc=d855f47d523c5a4fab67f5b10be3c475&pt=180&pt_tag_tm=1614590979827')
m3u8Download.run()
console.log("公众号:Java技术迷")
console.log("wx:spiderskill")
来源:https://blog.csdn.net/qq_41179280/article/details/114267921
标签:Python,m3u8,mp4,视频文件
0
投稿
猜你喜欢
仿豆瓣分页原型(Javascript版)
2007-11-05 14:04:00
让长字符自动换行 比如URL和Email地址等
2008-07-01 14:41:00
Go语言线程安全之互斥锁与读写锁
2024-05-09 09:56:18
MYSQL大量写入问题优化详解
2024-01-24 04:26:56
使用python删除nginx缓存文件示例(python文件操作)
2021-10-16 03:41:32
python用plotly实现绘制局部放大图
2021-06-13 06:30:44
python实现kNN算法识别手写体数字的示例代码
2021-07-14 12:35:03
Python3利用Dlib实现摄像头实时人脸检测和平铺显示示例
2021-12-14 16:37:30
最长用最基本的MSSQL数据库备份与还原
2024-01-17 18:23:07
python的sorted用法详解
2022-04-09 20:21:14
详解MySQL中的pid与socket
2024-01-26 15:07:56
python使用paramiko模块通过ssh2协议对交换机进行配置的方法
2022-05-16 03:03:17
asp如何计算下载一个文件需要多长时间?
2009-11-25 20:17:00
利用ASP实现事务处理的方法
2010-05-11 16:53:00
MySQL 两张表数据合并的实现
2024-01-28 07:25:49
简述Redis和MySQL的区别
2024-01-25 01:18:34
python分布式环境下的限流器的示例
2023-07-11 19:25:38
Python3实现转换Image图片格式
2021-06-06 21:04:25
关于MySQL编码问题的经验总结
2007-08-23 16:10:00
影响SQL Server性能的关键三个方面
2009-02-13 16:59:00