首页 > 编程语言 >python 下载m3u8格式的视频方法

python 下载m3u8格式的视频方法

时间:2024-11-08 15:58:26浏览次数:4  
标签:m3u8 python ts filename url path 格式 os

import requests
import re
import json
import m3u8
import os
from concurrent.futures import ThreadPoolExecutor
from time import sleep
import shutil
import subprocess

headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
    'Cookie':'_did=web_1930955391DE64DF; _did=web_1930955391DE64DF; csrfToken=B3HzIa_gotdrEZWr2wOODE9z; webp_supported=%7B%22lossy%22%3Atrue%2C%22lossless%22%3Atrue%2C%22alpha%22%3Atrue%2C%22animation%22%3Atrue%7D; Hm_lvt_2af69bc2b378fb58ae04ed2a04257ed1=1730789391; HMACCOUNT=EAB764C050B1F83E; lsv_js_player_v2_main=ca85g8; uuid=71dac937b0a505074f4e5e94fa8d1234; auth_key=76212252; ac_userimg=https%3A%2F%2Fimgs.aixifan.com%2Fstyle%2Fimage%2FdefaultAvatar.jpg; stochastic=eWF6MjduaWRvZw%3D%3D; acPasstoken=ChVpbmZyYS5hY2Z1bi5wYXNzdG9rZW4ScGbcuUJMOlbw849_IMXUeM1D9Pnj6LNEtj0f15emmerMDycQxBDcT_CZHQLrhk-XgwxJctLTNpifSja1P7U-Vj2rAquxGMEdpRoqg2dF7Vvz2XvTTOMjZH0JmWGOuvGlGltsL5nV4iEaeATIEtRnGn8aEmdOSJkx2ly3WrEb6jSqdMEq6SIgOlkU3ZpDjDwLFeEP8MTMeUiOIRnLpDfBbTREE09kiSIoBTAB; ac_username=yangzhenyu9406; acPostHint=3eb71c4a51d5a6884aa31105719cdecf872a; safety_id=AAJROucDlbVzhbY2UsGaqvRY; cur_req_id=713498187499F148_self_0528e0128300a06862bd18195cc26e1d; cur_group_id=713498187499F148_self_0528e0128300a06862bd18195cc26e1d_0; Hm_lpvt_2af69bc2b378fb58ae04ed2a04257ed1=1730789919'
}

output_dir = r'C:\Users\38461\Desktop\dongman\data'


# 单个TS文件的下载函数,带重试机制
def download_segment(ts_url, ts_filename, max_retries):
    attempt = 0
    while attempt < max_retries:
        try:
            print(f"Attempting to download {ts_url} to {ts_filename}... (Attempt {attempt + 1})")
            response = requests.get(ts_url, headers=headers, stream=True, timeout=10)
            response.raise_for_status()  # 检查请求是否成功
            with open(ts_filename, "wb") as ts_file:
                for chunk in response.iter_content(chunk_size=1024):
                    if chunk:
                        ts_file.write(chunk)
            print(f"Downloaded {ts_filename}")
            return  # 成功下载则退出函数
        except requests.exceptions.RequestException as e:
            print(f"Failed to download {ts_url}: {e}")
            attempt += 1
            sleep(2)  # 等待2秒钟后重试
    print(f"Failed to download {ts_url} after {max_retries} attempts.")


# 下载所有TS文件的多线程处理函数
def down_load_ts(m3u8_url,m3u8_full_path):
    m3u8_obj = m3u8.load(m3u8_url)
    # 使用线程池加速下载
    with ThreadPoolExecutor(max_workers=10) as executor:
        futures = []
        for i, segment in enumerate(m3u8_obj.segments):
            ts_url = 'https://tx-safety-video.acfun.cn/mediacloud/acfun/acfun_video/hls/' + segment.uri
            ts_filename = os.path.join(m3u8_full_path, f"segment_{i}.ts")
            futures.append(executor.submit(download_segment, ts_url, ts_filename,3))
        # 等待所有任务完成
        for future in futures:
            future.result()
    print("All TS files downloaded successfully.")
    return m3u8_obj


def merge_ts_files(m3u8_obj, output_dir, full_path):
    # 打开目标输出文件以写入模式
    output_file = os.path.join(full_path,'output_video.ts')
    with open(output_file, "wb") as merged:
        for i in range(len(m3u8_obj.segments)):
            ts_filename = os.path.join(output_dir, f"segment_{i}.ts")
            # 检查TS文件是否存在
            if not os.path.exists(ts_filename):
                print(f"Warning: {ts_filename} does not exist. Skipping this segment.")
                continue

            # 打开并读取TS文件,将其内容写入到合并文件中
            with open(ts_filename, "rb") as ts_file:
                merged.write(ts_file.read())
            print(f"Merged {ts_filename}")
    print(f"All segments merged into {output_file}.")
    return output_file

def ts_to_mp4(ts_file,output_file):
    # 使用 ffmpeg 将 .ts 文件转换为 .mp4
    subprocess.run(
        ['ffmpeg', '-i', ts_file, '-c', 'copy', output_file],
        check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
    )



# 获取M3U8的URL
def start_url():
    url = 'https://www.acfun.cn/bangumi/aa5024869'
    response = requests.get(url=url, headers=headers)
    data = json.loads(re.findall(r'window.bangumiData = (.*?)};', response.text)[0] + '}')
    info = data['currentVideoInfo']['ksPlayJsonHevc']
    name = data['bangumiTitle']
    m3u8_url = re.findall(r'{"id":1,"url":"(.*?)",', info)[0]

    m3u8_full_path = os.path.join(output_dir, name + '_' + 'ts')
    full_dir = os.path.join(output_dir,name)
    if not os.path.exists(m3u8_full_path):
        os.makedirs(m3u8_full_path, exist_ok=True)
    m3u8_obj = down_load_ts(m3u8_url, m3u8_full_path)
    if not os.path.exists(full_dir):
        os.makedirs(full_dir, exist_ok=True)
    ts_filename = merge_ts_files(m3u8_obj,m3u8_full_path,full_dir)
    ts_to_mp4(ts_filename,os.path.join(os.path.dirname(ts_filename),os.path.basename(ts_filename).replace('.ts','.mp4')))
    os.remove(ts_filename)
    shutil.rmtree(m3u8_full_path)



if __name__ == '__main__':
    start_url()

 

标签:m3u8,python,ts,filename,url,path,格式,os
From: https://www.cnblogs.com/lvye001/p/18535255

相关文章

  • 虚假新闻检测:CSV格式数据集的预处理与模型选择
    ✅作者简介:2022年博客新星第八。热爱国学的Java后端开发者,修心和技术同步精进。......
  • Python介绍和基础运用
    python之基本介绍(1)什么是python?python是一门编程语言python是一门面向对象,解释型的动态类型的编程语言,guidovanrossunm(吉多*范罗苏姆),在1989年发明,第一个公开发行版本与1991年;guidovanrossunm(吉多*范罗苏姆)是荷兰计算机程序员(2)什么是面向对象?python中一切皆......
  • Python从0到100(七十):Python OpenCV-Opencv实现人像迁移
    前言:零基础学Python:Python从0到100最新最全教程。想做这件事情很久了,这次我更新了自己所写过的所有博客,汇集成了Python从0到100,共一百节课,帮助大家一个月时间里从零基础到学习Python基础语法、Python爬虫、Web开发、计算机视觉、机器学习、神经网络以及人工智能相关知......
  • python3.5-IDLE中斐波那契数列程序实现
    斐波那契数列F(n)定义:F(0)=0,F(1)=1,……,F(n)=F(n-2)+F(n-1),其中n≥2(简单总结,从第3个数起,斐波那契数列中每个数都是前两个数之和)代码实现:1)采用迭代方式实现:deffibonacci_iterative(n):ifn<=0:return0elifn==1:return1......
  • nodejs 18.20.4 juice 库将html中的全局CSS格式, 转化为每个元素内联的CSS格式
    执行下面命令,安装nodejs和npmubuntu@ubuntu22:~$sudoaptupdateubuntu@ubuntu22:~$sudoaptinstallnpm#npm是Node.js的包管理工具ubuntu@ubuntu22:~$sudoaptinstall-ycurlubuntu@ubuntu22:~$curl-fsSLhttps://deb.nodesource.com/setup_18.x|sudo-Eb......
  • Python数据分析NumPy和pandas(二十六、数据整理--连接、合并和重塑 之三:重塑和透视)
    对表格数据的重新排列操作,称为reshape或pivot。有很多种方法对表格数据进行重塑。一、使用分层索引进行reshape分层索引提供了一种在DataFrame中重新排列数据的方法。主要有两个函数方法:stack:将数据中的列旋转或透视到行。unstack:从行转为列。还是用代码示例来学习......
  • 洛谷P1157 组合的输出(Python)
    伤痕,是男子汉的勋章。——圣斗士星矢一、题目P1157组合的输出https://www.luogu.com.cn/problem/P1157二、代码defpri(L):foriinrange(len(L)):ifL[i]==True:print("{:3d}".format(i),end='')defdfs(n,r,cur,count):#n,r为题......
  • python球探数据分析
    Python球探数据分析入门指南作为一名刚入行的小白,你可能会对如何使用Python进行球探数据分析感到困惑。不用担心,这篇文章将为你提供一份详细的入门指南,帮助你逐步掌握这项技能。流程概览首先,让我们通过一个表格来了解整个数据分析的流程:步骤描述1获取数据2数据......
  • 最新毕设-SpringBoot-求职推荐系统-55000(免费领项目)可做计算机毕业设计JAVA、PHP、爬
    摘 要当前社会竞争激烈,求职市场信息众多,但信息不对称、筛选困难的问题依然存在。因此,设计开发一款求职推荐系统是顺应时代发展的必然选择。该求职推荐系统利用Java编程语言,使用springboot技术框架,采取MySQL数据库实现系统的各项功能,具有便捷高效、安全友好的特点,促进求职招聘......
  • python中多线程和多进程的应用
    选择指南总结I/O密集型任务:使用多线程(ThreadPoolExecutor)。例如,爬虫抓取数据、从数据库或API获取信息、读取/写入文件等。计算密集型任务:使用多进程(multiprocessing.Pool)。例如,进行大量数据计算、数值分析等。混合型任务:可以结合多线程和多进程。例如,先用线程处理I/O......