爬取m3u8加密视频




import random
import os
import re
import requests
import asyncio
import aiohttp
import time
from lxml import etree
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad,unpad url = "https://www.9tata.cc/play/17360-2-0.html"
user_agent_list = [
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 "
"(KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1",
"Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 "
"(KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 "
"(KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 "
"(KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6",
"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 "
"(KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 "
"(KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5",
"Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 "
"(KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 "
"(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 "
"(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3 "
"(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 "
"(KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 "
"(KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 "
"(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 "
"(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 "
"(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
"Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 "
"(KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24 "
"(KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24",
"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 "
"(KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24"
]
headers ={
"User-Agent":random.choice(user_agent_list)
} async def get_m3u8_url():
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers,ssl=False) as response:
text = await response.text()
html = etree.HTML(text) # 1. 获取页面html
video_name = html.xpath('//div[@class="page-header"]/h2/a[2]//text()')[0]
m3u8_1 = re.search('now="(.*?m3u8)";', text).group(1)
return m3u8_1, video_name # 2. 从网页源码(js)获取m3u8地址
async def get_m3u8_detail(m3u8_url):
async with aiohttp.ClientSession() as session:
async with session.get(m3u8_url, headers=headers,ssl=False) as response:
text = await response.text()
path = text.split('\n')[2]
# m3u8_2_path = os.path.join(os.path.dirname(m3u8_1), path)
m3u8_2_path = os.path.dirname(os.path.dirname(os.path.dirname(m3u8_url)))+path
return m3u8_2_path,os.path.dirname(path) async def get_key(m3u8_url):
"""获取Key"""
print(m3u8_url)
async with aiohttp.ClientSession() as session:
async with session.get(m3u8_url+"/key.key", headers=headers,ssl=False) as response:
text = await response.text()
with open("videos/key.key", "w") as f:
f.write(text)
return text # 4. 获取m3u8文件顺序
async def get_ts_list(m3u8_detail,path):
async with aiohttp.ClientSession() as session:
async with session.get(m3u8_detail, headers=headers,ssl=False) as response:
text = await response.text()
with open("videos/old_index.m3u8","w") as f:
text = text.replace(path+'/',os.path.abspath("videos")+'\\')
# text = text.replace("\\","\\\\")
f.write(text)
ts_list = re.findall(r"\\([A-Za-z0-9]*\.ts)",text)
print(ts_list)
base_path = os.path.dirname(m3u8_detail)
return ts_list, base_path async def download(base_path, ts, aes):
# 5. 下载ts文件
ts_path = base_path+'/'+ts
ts_name = os.path.basename(ts)
print(ts_path)
with open(f"videos/{ts_name}", "wb") as f:
async with aiohttp.ClientSession() as session:
async with session.get(ts_path, headers=headers,ssl=False) as response:
f.write(aes.decrypt(pad(await response.content.read(), AES.block_size)))
# f.write(aes.decrypt(await response.content.read()))
print(f"{ts_name}下载完成") def change_m3u8():
"""视频ts已经解密,不需要在解密 #EXT-X-KEY这行删除"""
with open("videos/old_index.m3u8",'r') as f:
lines = f.readlines()
with open("index.m3u8", 'w') as file:
for line in lines:
if "#EXT-X-KEY" not in line:
file.readline(line) def merge(video_name="flash"):
"""视频合并"""
os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)),"videos"))
os.system(f"ffmpeg -allowed_extensions ALL -i index.m3u8 -c copy {video_name}.mp4") async def main():
m3u8_url, name = await get_m3u8_url()
m3u8_detail,path = await get_m3u8_detail(m3u8_url)
key = await get_key(os.path.dirname(m3u8_detail))
aes = AES.new(key.encode('utf-8'), AES.MODE_CBC,b"0000000000000000")
ts_list, base_path = await get_ts_list(m3u8_detail,path)
tasks = [asyncio.create_task(download(base_path,ts, aes)) for ts in ts_list]
await asyncio.wait(tasks)
change_m3u8()
merge(name) start = time.time()
asyncio.run(main())
print(f"耗时:{time.time()-start}")
爬取m3u8加密视频的更多相关文章
- 利用selenium和ffmpeg爬取m3u8 ts视频《进击的巨人》
需求 想看下动漫<进击的巨人>,发现到处被和谐,找不到资源,但是在一个视频网站找到了在线播放,https://www.55cc.cc/dongman/17890/player-2-1.ht ...
- 爬虫之爬取B站视频及破解知乎登录方法(进阶)
今日内容概要 爬虫思路之破解知乎登录 爬虫思路之破解红薯网小说 爬取b站视频 Xpath选择器 MongoDB数据库 爬取b站视频 """ 爬取大的视频网站资源的时候,一 ...
- 如何手动写一个Python脚本自动爬取Bilibili小视频
如何手动写一个Python脚本自动爬取Bilibili小视频 国庆结束之余,某个不务正业的码农不好好干活,在B站瞎逛着,毕竟国庆嘛,还让不让人休息了诶-- 我身边的很多小伙伴们在朋友圈里面晒着出去游玩 ...
- python爬虫:爬取慕课网视频
前段时间安装了一个慕课网app,发现不用注册就可以在线看其中的视频,就有了想爬取其中的视频,用来在电脑上学习.决定花两天时间用学了一段时间的python做一做.(我的新书<Python爬虫开发与 ...
- Python爬取B站视频信息
该文内容已失效,现已实现scrapy+scrapy-splash来爬取该网站视频及用户信息,由于B站的反爬封IP,以及网上的免费代理IP绝大部分失效,无法实现一个可靠的IP代理池,免费代理网站又是各种 ...
- Python爬虫实战:爬取腾讯视频的评论
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 易某某 PS:如有需要Python学习资料的小伙伴可以加点击下方链 ...
- python 爬取腾讯视频的全部评论
一.网址分析 查阅了网上的大部分资料,大概都是通过抓包获取.但是抓包有点麻烦,尝试了F12,也可以获取到评论.以电视剧<在一起>为例子.评论最底端有个查看更多评论猜测过去应该是 Ajax ...
- Python爬取某短视频热点
写在前面的一些话: 随着短视频的大火,不仅可以给人们带来娱乐,还有热点新闻时事以及各种知识,刷短视频也逐渐成为了日常生活的一部分.本文以一个简单的小例子,简述如何通过Pyhton依托Selenium来 ...
- Python 自动爬取B站视频
文件名自定义(文件格式为.py),脚本内容: #!/usr/bin/env python #-*-coding:utf-8-*- import requests import random impor ...
- Python爬取抖音视频
最近在研究Python爬虫,顺便爬了一下抖音上的视频,找到了哥们喜欢的小姐姐居多,咱们给他爬下来吧. 最终爬取结果 好了废话补多说了,上代码! #https://www.iesdouyin.com/a ...
随机推荐
- P9562 [SDCPC2023] G-Matching 题解
题目描述 给定长度为 \(n\) 的整数序列 \(a_1, a_2, \cdots, a_n\),我们将从该序列中构造出一张无向图 \(G\).具体来说,对于所有 \(1 \le i < j \ ...
- Golang 与 JS 的字符串截取大同小异
Golang 和 JS 的字符串截取都可以利用索引定位的方式. Golang: str := "abcdef" sub := str[1: 2] JS: const str = ' ...
- WPF 通过 GetMessageExtraInfo 方法获取当前收到的鼠标消息是否由触摸转换过来
本文将告诉大家如何在 WPF 或者其他 Win32 应用里面,在收到鼠标消息时,通过 GetMessageExtraInfo 方法获取当前收到的鼠标消息是否由触摸消息提升而来 大家都知道,在不开启 W ...
- dotnet 部署 github 的 Action 进行持续集成
被微软收购的 GitHub 现在十分土豪,提供了免费的服务器给咱构建.刚好微软对 dotnet 的支持是特别好的,毕竟还算半个自家的东西,大概只需要 3 分钟就可以在 github 上通过 Actio ...
- vue-cli快速搭建项目的几个文件(一)
===========app.vue文件============= <template> <div id="app"> <router ...
- 对象存储服务的Lambda特性
AWS S3提供了Lambda服务,详见Amazon S3 Object Lambda. 技术方案 作为兼容AWS S3能力的对象存储服务,交付Lambda特性时,关注点有: 实现方式 SDK 独立进 ...
- Educational Codeforces Round 160 (Rated for Div. 2)
A 直接模拟,注意细节 #include<bits/stdc++.h> #define ll long long using namespace std; ll p[15] = {1}; ...
- R6_ES在互联网公司应用案例汇总参考
Elasticsearch 是一个实时分布式搜索数据分析引擎,内部使用lucene做索引与搜索,能够解决常规和各种类型数据的存储及检索需求,典型的应用场景有:数据分析,站内搜索,ELK,电商等,主要特 ...
- PyTorch的安装与使用
技术背景 PyTorch是一个非常常用的AI框架,主要归功于其简单易用的特点,深受广大科研人员的喜爱.在前面的一篇文章中我们介绍过制作PyTorch的Singularity镜像的方法,这里我们单独抽出 ...
- 01. Linux 如何安装rvm和ruby
参考: https://blog.csdn.net/qq_35641923/article/details/86493822 https://www.runoob.com/ruby/ruby-inst ...