python:m3u8转mp4

Posted on 2021-01-20 16:13  大白不白  阅读(864)  评论(0编辑  收藏  举报
import re,requests,time,os

import threadpool



res = requests.get(url="http://pili-vod.tebaobao.vip/recordings/z1.tebaobao.tbb_dev5_4706464/f1610716867.m3u8").text

# print(res)
m3u8_res = re.findall('/fragments(.*?)ts',res)
m_res = re.findall('fragments/z1.tebaobao.tbb_dev5_4706464/(.*?)ts',res)
li = []
# print(m3u8_res)
for i in range(len(m3u8_res)):
    m3_res = "http://pili-vod.tebaobao.vip/fragments" +m3u8_res[i] + "ts"
    li.append(m3_res)

print(li)
print(len(li))

w_li = []
for i in range(len(m3u8_res)):
    m3u_res = m_res[i] + "ts"
    w_li.append(m3u_res)

print(w_li)



r = requests.get(url='http://pili-vod.tebaobao.vip/fragments/z1.tebaobao.tbb_dev5_4706464/1610711419540-1610711423167.ts')
print(r.content)
c = 0
def download(c):


    for i in range(len(li)):
        with open("/Users/apple/Desktop/test/" + str(c) + ".ts","wb")as f:
            try:
                r = requests.get(url = li[i])

                f.write(r.content)
                print("下载成功!,第%d次"%c)
            except:
                print('请求失败!')
        c=c+1



def get_video():
    files = os.listdir("/Users/apple/Desktop/test/")
    # print(files)
    aa = 0
    for file in range(len(li)):

        with open("/Users/apple/Desktop/test/" + str(aa) + ".ts", 'rb') as f1:
            with open("1.mp4", 'ab') as f2:
                f2.write(f1.read())

        aa = aa+1

get_video()

# if __name__ == '__main__':
#
#     start_time = time.time()
#     pool = threadpool.ThreadPool(1000)
#     results = threadpool.makeRequests(download,args_list=range(1))
#     [pool.putRequest(req) for req in results]  # 多线程一块执行
#     pool.wait()  # 线程挂起,直到结束

 

Copyright © 2024 大白不白
Powered by .NET 9.0 on Kubernetes