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_96start=496696end=779448contentlength=282752sd=20200qdv=1qd_uid=0qd_tvid=2645242154145600qd_vip=0qd_src=02029022240000000000qd_tm=1614590393988qd_ip=0qd_p=0qd_k=4eb685f1966cbd08e6a9648fe0b8c007ve=sgti=dfp=qd_sc=f9a4f133a622871b6739734615ef178d
#EXTINF:9,
http://data.video.iqiyi.com/videos/vts/20210301/69/b8/73ad4ef04fde4586ef2799ecd67241ce.ts?qypid=2645242154145600_04000000001000000000_96start=779448end=1008432contentlength=228984sd=29200qdv=1qd_uid=0qd_tvid=2645242154145600qd_vip=0qd_src=02029022240000000000qd_tm=1614590393988qd_ip=0qd_p=0qd_k=4eb685f1966cbd08e6a9648fe0b8c007ve=sgti=dfp=qd_sc=f9a4f133a622871b6739734615ef178d
#EXTINF:9,
http://data.video.iqiyi.com/videos/vts/20210301/69/b8/73ad4ef04fde4586ef2799ecd67241ce.ts?qypid=2645242154145600_04000000001000000000_96start=5934408end=6141020contentlength=206612sd=198733qdv=1qd_uid=0qd_tvid=2645242154145600qd_vip=0qd_src=02029022240000000000qd_tm=1614590393988qd_ip=0qd_p=0qd_k=4eb685f1966cbd08e6a9648fe0b8c007ve=sgti=dfp=qd_sc=f9a4f133a622871b6739734615ef178d
#EXT-X-ENDLIST
# -*- 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_pytvid=2645242154145600bossStatus=0qd_vip=0px=src=02029022240000000000prv=previewType=previewTime=from=qd_time=1614590979725qd_p=0qd_asc=636bb14ab52facec684335546d2d60ecqypid=2645242154145600_04000000001000000000_96qd_k=4eb685f1966cbd08e6a9648fe0b8c007isdol=0code=2ff=f4viswb=1vf=2198359907d7f173fed0b6eabca18b29np_tag=nginx_part_tagpt_sc=d855f47d523c5a4fab67f5b10be3c475pt=180pt_tag_tm=1614590979827')
m3u8Download.run()
到此这篇关于Python解析m3u8拼接下载mp4视频文件的文章就介绍到这了,更多相关Python下载mp4视频文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!