介绍
这篇文章将为大家详细讲解有关怎么在python中利用多线程下载m3u8格式视频,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
import datetime import 操作系统 import 再保险 import 线程 import 请求 得到queue import 队列 #,预下载,获取m3u8文件,读出ts链接,并写入文档 def (): #,才能m3u8链接 时间=url 才能;& # 39;https://ali video.acfun.cn/mediacloud/acfun/acfun_video/segment/3zf_gaw6nfmudxrtll89ozyoz4mwxgoash6uczbsj1_6eaxuxtp3xm8wfmgmnonz.m3u8?auth_key=1573739375 - 474267152 - 0 - a5aa2b6df4cb4168381bf8b04d88ddb1& # 39; #,才能当ts文件链接不完整时,需拼凑 #,才能大部分网站可使用该方法拼接,部分特殊网站需单独拼接 时间=base_url 才能;re.split (r" [a-zA-Z0-9 _ \] + \ .m3u8",, url) [0] #,才能打印(base_url) 时间=resp 才能;requests.get (url) m3u8_text 才能=resp.text #,才能打印(m3u8_text) #,才能按行拆分m3u8文档 ts_queue 才能=,队列(10000) 时间=lines 才能;m3u8_text.split (& # 39; \ n # 39;) #,才能找到文档中含有ts字段的行 时间=concatfile 才能;& # 39;缓存/& # 39;,+,“s", +, & # 39; . txt # 39; for 才能;line 拷贝: ,,,if & # 39; .ts& # 39;,拷贝: ,,,,,if & # 39; http # 39;,拷贝: ,,,,,,,#,打印(““ts>,,行) ,,,,,,,ts_queue.put(线) ,,,,,其他的: ,,,,,,,line =, base_url +线 ,,,,,,,ts_queue.put(线) ,,,,,,,#,打印(& # 39;ts>祝辞& # 39;,线) ,,,,,filename =, re.search (& # 39; ([a-zA-Z0-9 -] + .ts) & # 39;,,行).group (1) .strip () ,,,,,#,一定要先写文件,因为线程的下载是无序的,文件无法按照 ,,,,,#,123456…去顺序排序,而文件中的命名也无法保证是按顺序的 ,,,,,#,这会导致下载的ts文件无序,合并时,就会顺序错误,导致视频有问题。 ,,,,,开放(concatfile, & # 39; a + & # 39;) .write (“file  % s \ n", %,文件名) return 才能;ts_queue concatfile #,线程模式,执行线程下载 def 运行(ts_queue): tt_name 才能=,threading.current_thread () . getname () while 才能;not  ts_queue.empty (): ,,,url =, ts_queue.get () ,,,r =, requests.get (url,流=True) ,,,filename =, re.search (& # 39; ([a-zA-Z0-9 -] + .ts) & # 39;,, url) .group (1) .strip () ,,,with 开放(& # 39;缓存/& # 39;,+,文件名,,& # 39;wb # 39;), as 外交政策: ,,,,,for chunk 拷贝r.iter_content (5242): ,,,,,,,if 块: ,,,,,,,,,fp.write(块) ,,,print(时间+ tt_name “,“, +, filename +, & # 39;,下载成功& # 39;) #,视频合并方法,使用ffmpeg def 合并(concatfile,,的名字): 尝试才能: ,,,path =, & # 39;缓存/& # 39;,+,name +, & # 39; mp4 # 39; ,,,command =, & # 39; ffmpeg -y -f concat 小姐;% s -crf 18, -ar 48000, -vcodec libx264 - c: a aac -r 25, -g 25, -keyint_min 25, -strict 2, % & # 39;, %, (concatfile,路径) ,,,os.system(命令) ,,,print(& # 39;视频合并完成& # 39;) 除了才能: ,,,print(& # 39;合并失败& # 39;) if __name__ ==, & # 39; __main__ # 39;: name =,才能输入(& # 39;请输入视频名称:& # 39;) 时间=start 才能;datetime.datetime.now () .replace(微秒=0) 年代,才能concatfile =, () #,才能打印(年代,concatfile) 时间=threads 才能;[] for 才能小姐:拷贝范围(15): ,,,t =, threading.Thread(目标=运行,name=& # 39; th & # 39; + str (i), kwargs={& # 39; ts_queue& # 39;:,年代}) ,,,threads.append (t) for 才能;t 拷贝线程: ,,,t.start () for 才能;t 拷贝线程: ,,,t.join () 最终获得才能=,datetime.datetime.now () .replace(微秒=0) 打印才能(& # 39;下载耗时:& # 39;,+,str(最终获得;安康;开始)) 合并才能(concatfile名称) 配套,才能=,datetime.datetime.now () .replace(微秒=0) null怎么在python中利用多线程下载m3u8格式视频