python下载m3u8

python下载m3u8视频

依赖下载

防止反爬

agent_list = [ "Mozilla/5.0 (Linux; U; Android 2.3.6; en-us; Nexus S Build/GRK39F) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1", "Avant Browser/1.2.789rel1 (http://www.avantbrowser.com)", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.249.0 Safari/532.5", "Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/532.9 (KHTML, like Gecko) Chrome/5.0.310.0 Safari/532.9", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.514.0 Safari/534.7", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/534.14 (KHTML, like Gecko) Chrome/9.0.601.0 Safari/534.14", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.14 (KHTML, like Gecko) Chrome/10.0.601.0 Safari/534.14", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.20 (KHTML, like Gecko) Chrome/11.0.672.2 Safari/534.20", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.27 (KHTML, like Gecko) Chrome/12.0.712.0 Safari/534.27", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.24 Safari/535.1", "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.36 Safari/535.7", "Mozilla/5.0 (Windows; U; Windows NT 6.0 x64; en-US; rv:1.9pre) Gecko/2008072421 Minefield/3.0.2pre", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10" ]

AES安装

pip uninstall crypto
pip uninstall pycryptodome
pip install pycryptodome

m3u8安装

pip uninstall m3u8
pip install m3u8

tqdm安装

pip uninstall tqdm
pip install tqdm

引入依赖

import m3u8
import re
import requests
import time
from tqdm import tqdm
from concurrent.futures import ThreadPoolExecutor
from Crypto.Cipher import AES

代码实现

载入m3u8

m3u8_obj = m3u8.load(url)
m3u8_list=re.sub('#E.*','',str(m3u8_obj.segments)).split()

针对加密的m3u8进行解码

def cryptor(m3u8_obj):
    if (not m3u8_obj.keys[0] is None):
        key = re.search('URI="(.*?)"', str(m3u8_obj.keys[0])).group(1)
        key_contetn = requests.get(url=key, headers=headers).content
        return AES.new(key_contetn, AES.MODE_CBC, key_contetn)
    return

使用线程池下载文件

for value in tqdm(m3u8_list):
        with ThreadPoolExecutor(max_workers=10) as pool:
            pool.submit(download, value, count)
            count = count + 1
            time.sleep(1)


def download(value, count):
    ts_content = requests.get(
        url='https://1258712167.vod2.myqcloud.com/25121a6avodtransbj1258712167/e715910f387702306493093622/drm/'+value).content
    with open('{}/{}.ts'.format('F:\mp4', count), 'ab') as file:
        file.write(aesObj.decrypt(ts_content))
        time.sleep(1)
        count = count + 1

完整的代码

import m3u8
import re
import requests
import time
from tqdm import tqdm
from concurrent.futures import ThreadPoolExecutor
from Crypto.Cipher import AES

url = "https://1258712167.vod2.myqcloud.com/25121a6avodtransbj1258712167/e715910f387702306493093622/drm/voddrm.token.dWluPTE0NDExNTIzNjAzNjE2NDQ4MTtleHQ9OWIwNjhhNzA3ZWExMzkzY2IzMTczZGU5ZGZiYjAzNDBiMTExZGJjZDVkYTI2MjJhODc1MjhhMTQzZTU2MWQ5MjA3MzI2MzRlNGJjOWRkODIxOWRiMjYyMWVhNDQ1MDE1ODU5MTliM2RhYjY1ZDU3NWE3MWE1MjU1YjZlY2ZjN2NjZWRjN2ExM2UyZmZkN2JlO3VpZF90eXBlPTEwMTI7dWlkX29yaWdpbl91aWRfdHlwZT0wO3VpZF9vcmlnaW5fYXV0aF90eXBlPTEwMDM7Y2lkPTU4OTI2ODk7dGVybV9pZD0xMDYxMDk5NzE7dm9kX3R5cGU9MDtwbGF0Zm9ybT0z.v.f30742.m3u8?exper=0&sign=7f6ea570ed6c288eba5df044512cc2dd&t=6449D009&us=946049041817278558"

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'
}

aesObj = None


def init():
    m3u8_obj = m3u8.load(url)
    m3u8_list = re.sub('#E.*', '', str(m3u8_obj.segments)).split()
    count = 0
    global aesObj
    aesObj = cryptor(m3u8_obj)
    for value in tqdm(m3u8_list):
        with ThreadPoolExecutor(max_workers=10) as pool:
            pool.submit(download, value, count)
            count = count + 1
            time.sleep(1)


def download(value, count):
    ts_content = requests.get(
        url='https://1258712167.vod2.myqcloud.com/25121a6avodtransbj1258712167/e715910f387702306493093622/drm/'+value).content
    with open('{}/{}.ts'.format('F:\mp4', count), 'ab') as file:
        file.write(aesObj.decrypt(ts_content))
        time.sleep(1)
        count = count + 1


def cryptor(m3u8_obj):
    if (not m3u8_obj.keys[0] is None):
        key = re.search('URI="(.*?)"', str(m3u8_obj.keys[0])).group(1)
        key_contetn = requests.get(url=key, headers=headers).content
        return AES.new(key_contetn, AES.MODE_CBC, key_contetn)
    return


if __name__ == '__main__':
    print('开始下载')
    init()
    print('下载完成')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值