码农网

网站首页> 后端开发> python

python将下载到本地m3u8视频合成MP4的代码详解

众衡网络科技

代码如下所示:

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")

视频下载下来如下图所示

python将下载到本地m3u8视频合成MP4的代码详解

https://ngmov.me/?channelCode=ngdl50
这个应用下载下来的视频就是这样的解法

python将下载到本地m3u8视频合成MP4的代码详解

改进版本

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)

到此这篇关于python将下载到本地m3u8视频合成MP4的代码详解的文章就介绍到这了。

Python m3u8视频合成MP4

本文地址:https://m.manongw.com/article/340.html

文章来源:转载于CSDN,转载网址为https://blog.csdn.net/weixin_32759777/article/details/105588190

版权申明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 ezhongheng@126.com 举报,一经查实,本站将立刻删除。

最近更新
热门素材
html5卡通章鱼素材,几何图形抽象设计

html5卡通章鱼素材,几何图形抽象设计

图片素材

html文字动画特效,文字虚线边框

html文字动画特效,文字虚线边框

文字特效

Bootstrap点击左侧垂直导航菜单全屏网页切换特效

Bootstrap点击左侧垂直导航菜单全屏网页切换特效

导航菜单

js+css3透明渐变风格导航菜单特效

js+css3透明渐变风格导航菜单特效

导航菜单

8款经典的css网站顶部导航栏样式

8款经典的css网站顶部导航栏样式

图片素材

js+css3网站顶部自适应导航栏菜单特效

js+css3网站顶部自适应导航栏菜单特效

图片素材

jQuery自定义添加删除表格行内容特效

jQuery自定义添加删除表格行内容特效

图片素材

jQuery+CSS3漂亮的下拉菜单选择框美化特效

jQuery+CSS3漂亮的下拉菜单选择框美化特效

css3实例

jQuery文字公告无限滚动轮播特效

jQuery文字公告无限滚动轮播特效

css3实例

jQuery+Layui省市区城市三级联动菜单选择特效

jQuery+Layui省市区城市三级联动菜单选择特效

css3实例