python之使用ffmpeg下载直播推流视频rtmp、m3u8协议实时获取时间进度

一、ffmpeg下载

录制函数

python 复制代码
    def download_video_by_live_url():
		playUrl = "rtmp://....."    #推流url:rtmp、m3u8等
        print(f"已开始录制: playUrl:{playUrl}")

        now_time = datetime.now().strftime('%Y_%m_%d__%H_%M_%S')
        output_file =  now_time + ".mp4"
        output_file = re.sub(r'[\\/:*?"<>|]', '', output_file)
        ffmpeg_command = [
            "ffmpeg",
            "-i", playUrl,
            "-c", "copy",
            "-f", "mp4",
            output_file
        ]

        process = subprocess.Popen(
            ffmpeg_command,
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            universal_newlines=True,  # decode bytes to str
            encoding="utf-8",
            bufsize=1
        )

        time_pattern = re.compile(r'time=(\d+:\d+:\d+\.\d+)')
        start_time = time.time()
        try:
            for line in process.stderr:
                line = line.strip()
                if 'time=' in line:
                    match = time_pattern.search(line)
                    if match and time.time() - start_time > 10:
                        start_time = time.time()
                        current_time = match.group(1)
                        print(f" Progress: {current_time}", flush=True)
                if 'No such stream' in line:
                    print(f"直播结束,结束录制!")
                    break


        except KeyboardInterrupt:
            print(f"检测到手动中断,正在优雅停止 ffmpeg...")
            process.stdin.write('q'.encode("GBK"))
            process.communicate()
            process.kill()
        except Exception as e:
            print(f"[{current_name}][{nickname}]录制出错: {e}")

        process.wait()
相关推荐
AntBlack2 分钟前
不当韭菜 : 好像真有点效果 ,想藏起来自己用了
前端·后端·python
百锦再36 分钟前
破茧成蝶:全方位解析Java学习难点与征服之路
java·python·学习·struts·kafka·maven·intellij-idea
可触的未来,发芽的智生1 小时前
触摸未来2025-10-25:蓝图绘制
javascript·python·神经网络·程序人生·自然语言处理
新手村领路人1 小时前
python opencv gpu加速 cmake msvc cuda编译问题和设置
开发语言·python·opencv
暴风鱼划水2 小时前
卡码网语言基础课(Python) | 19.洗盘子
python·算法
Gitpchy2 小时前
Day 23 机器学习管道 pipeline
python·机器学习
程序员小远2 小时前
使用Jmeter进行http接口测试
自动化测试·软件测试·python·测试工具·jmeter·http·接口测试
mit6.8243 小时前
[sam2图像分割] 视频追踪API | VideoPredictor | `inference_state`记忆
人工智能·计算机视觉·音视频
B站_计算机毕业设计之家3 小时前
spark实战:python股票数据分析可视化系统 Flask框架 金融数据分析 Echarts可视化 大数据技术 ✅
大数据·爬虫·python·金融·数据分析·spark·股票
碧海银沙音频科技研究院4 小时前
DiVE长尾识别的虚拟实例蒸馏方法
arm开发·人工智能·深度学习·算法·音视频