python将下载到本地m3u8视频合成MP4的代码详解
作者:东方佑 时间:2022-08-29 11:13:39
代码如下所示:
import os
import requests
import datetime
from Crypto.Cipher import AES
def decode_key_file(key_file_name):
with open(key_file_name,"r") as f:
data=f.read()
return data
def decode_m_file(m_file_name):
with open(m_file_name,"r") as f:
data=f.read()
return data.split("\n")[6::2]
def decode_video(filename):
data_list=[[],[],[]]
file_list=os.listdir(filename)
for one_file_name in file_list:
if "m3u8" in one_file_name:
data_list[0].append(filename+"/"+one_file_name)
elif "key" in one_file_name:
data_list[1].append(filename + "/" + one_file_name)
else:
data_list[2].append(filename + "/" + one_file_name)
key=decode_key_file(data_list[1][0])
name_list=decode_m_file(data_list[0][0])
for one_name in name_list:
for path_name in data_list[2]:
if one_name in path_name:
with open(path_name,"rb") as f:
data_name=f.read()
if len(key): # AES 解密
cryptor = AES.new(key, AES.MODE_CBC, key)
with open(key + ".mp4", 'ab') as f:
f.write(cryptor.decrypt(data_name))
if __name__ == '__main__':
decode_video("./data")
视频下载下来如下图所示
https://ngmov.me/?channelCode=ngdl50
这个应用下载下来的视频就是这样的解法
改进版本
def decode_key_file(key_file_name):
with open(key_file_name, "r") as f:
data = f.read()
return data
def decode_m_file(m_file_name):
with open(m_file_name, "r") as f:
data = f.read()
return data.split("\n")[6::2]
def decode_video(filename):
data_list = [[], [], []]
file_list = os.listdir(filename)
for one_file_name in file_list:
if "m3u8" in one_file_name:
data_list[0].append(filename + "/" + one_file_name)
elif "key" in one_file_name:
data_list[1].append(filename + "/" + one_file_name)
else:
data_list[2].append(filename + "/" + one_file_name)
try:
key = decode_key_file(data_list[1][0])
except:
key = one_file_name
name_list = decode_m_file(data_list[0][0])
for one_name in name_list:
for path_name in data_list[2]:
if one_name in path_name:
with open(path_name, "rb") as f:
data_name = f.read()
if len(key): # AES 解密
cryptor = AES.new(key, AES.MODE_CBC, key)
with open(key + ".mp4", 'ab') as f:
f.write(cryptor.decrypt(data_name))
else:
with open(key + ".mp4", 'ab') as f:
f.write(data_name)
f.flush()
if __name__ == '__main__':
data_list = os.listdir("/home/dfy/Videos/Download")
index=0
for path_one in data_list:
index+=1
if index<18:
continue
decode_video("/home/dfy/Videos/Download" + "/" + path_one)
来源:https://blog.csdn.net/weixin_32759777/article/details/105588190
标签:python,m3u8,视频,MP4
0
投稿
猜你喜欢
ThinkPHP CURD方法之limit方法详解
2023-11-15 03:58:00
JavaScript利用Canvas实现粒子动画倒计时
2024-04-16 09:37:52
一款Python工具制作的动态条形图(强烈推荐!)
2021-07-21 17:38:18
python_tkinter弹出对话框创建2
2023-10-17 06:27:29
Oracle11g完全卸载的详细步骤(超管用)
2024-01-25 17:04:43
Oracle如何设置表空间数据文件大小
2024-01-13 01:00:03
html风格tooltip效果的实现
2010-04-08 13:00:00
使用BULK INSERT大批量导入数据 SQLSERVER
2012-01-05 19:26:36
Javascript 每日测试 - 第五期 callee及function
2008-07-10 13:22:00
Python基础篇之字符串方法总结
2022-11-25 11:42:36
tensorflow入门之训练简单的神经网络方法
2021-07-16 14:21:51
js数组的基本用法及数组根据下标(数值或字符)移除元素
2024-04-10 10:40:26
pytorch中F.avg_pool1d()和F.avg_pool2d()的使用操作
2023-11-30 01:20:41
Python的几种主动结束程序方式
2022-10-12 14:39:25
基于Python Numpy的数组array和矩阵matrix详解
2022-04-22 10:52:28
JavaScript Alert通用美化类
2024-04-27 15:20:50
PHP常用函数和常见疑难问题解答
2023-11-08 19:28:17
Oracle 外连接实现代码
2009-08-08 23:08:00
CSS框架/命名/规则 注意要点
2008-06-03 13:07:00
Python通过Pygame绘制移动的矩形实例代码
2023-09-05 13:28:31