日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

使用Python下载电视剧(二):下载ts片段

發布時間:2024/8/1 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Python下载电视剧(二):下载ts片段 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 1.任務介紹
  • 2.代碼實現

1.任務介紹

拿到每一集的m3u8文件后,可以根據文件內的若干鏈接下載到對應的ts片段,然后根據key對應的鏈接獲得視頻解碼的密鑰對所有下載的ts片段進行解碼。最后將所有片段拼接起來得到完整的一集視頻。

2.代碼實現

import requests,re,aiohttp,aiofiles,asyncio,os from Crypto.Cipher import AESdef decode_key(url):key_url = url.replace('index.m3u8','key.key')key_resp = requests.get(key_url)key = key_resp.textaes = AES.new(key=key, IV=b"0000000000000000", mode=AES.MODE_CBC)return aesdef ts_urls(url):resp = requests.get(url)pattern = re.compile('.*?\n(?P<ts_url>https://.*?.ts).*?',re.S)ts_urls = pattern.findall(resp.text)return ts_urlsasync def get_single_ts(session,i,aes):url = ts_urls[i]async with session.get(url) as resp:async with aiofiles.open('./cql_temp/{}.ts'.format(i),mode='wb') as f:await f.write(await resp.content.read())async with aiofiles.open('./cql_temp/{}.ts'.format(i),mode='rb') as f1, \aiofiles.open('./cql/{}.ts'.format(i),mode='wb') as f2:bs = await f1.read()await f2.write(aes.decrypt(bs))print(i,'done!')async def main():tasks = []async with aiohttp.ClientSession() as session:for i in range(len(ts_urls)):task = asyncio.create_task(get_single_ts(session,i,aes))tasks.append(task)await asyncio.wait(tasks)if __name__ == '__main__':url = 'https://pps.sd-play.com/20220314/VltFA5R5/1200kb/hls/index.m3u8' # 這里以第一集為例aes = decode_key(url)ts_urls = ts_urls(url)asyncio.run(main())

將所有ts文件合并到一起:

import os # windows: copy /b 1.ts+2.ts+3.ts xxx.mp4 # code = 'copy /b 0.ts+1.ts+2.ts+3.ts+4.ts+5.ts+6.ts cql.mp4' # os.system(code) files = os.listdir('./') files = [int(i.split('.')[0]) for i in files] files = sorted(files) part1 = [str(i)+'.ts' for i in files[:600]] part2 = [str(i)+'.ts' for i in files[600:]] # 因為ts文件太多(1200+),一次合并不完,所以分兩步進行 code1 = '+'.join(part1) code2 = '+'.join(part2) os.system('copy /b '+code1+' cql1.mp4') os.system('copy /b '+code2+' cql2.mp4') os.system('copy /b '+'cql1.mp4+cql2.mp4 cql.mp4')


總結

以上是生活随笔為你收集整理的使用Python下载电视剧(二):下载ts片段的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。