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

视频下载下来如下图所示

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)

到此这篇关于python将下载到本地m3u8视频合成MP4的代码详解的文章就介绍到这了,更多相关python把m3u8视频合成MP4内容请搜索呐喊教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持呐喊教程!

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。