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()
相关推荐
Aision_10 小时前
从工具调用到 MCP、Skill完整学习记录
java·python·gpt·学习·langchain·prompt·agi
zc.z14 小时前
JAVA实现:纯PCM格式音频转换成BASE64
java·音视频·pcm
2301_8092047016 小时前
JavaScript中严格模式use-strict对引擎解析的辅助.txt
jvm·数据库·python
zjy2777716 小时前
mysql如何选择合适的索引类型_mysql索引设计实战
jvm·数据库·python
Aaswk16 小时前
Java Lambda 表达式与流处理
java·开发语言·python
万邦科技Lafite16 小时前
京东item_get接口实战案例:实时商品价格监控全流程解析
java·开发语言·数据库·python·开放api·淘宝开放平台
Cyber4K17 小时前
【Python专项】进阶语法-系统资源监控与数据采集(1)
开发语言·python·php
苍煜19 小时前
Java开发IO零基础吃透:BIO、NIO、同步异步、阻塞非阻塞
java·python·nio
一苇以航3219 小时前
LE Audio低功耗蓝牙音频详解 (三)
音视频·蓝牙·ble·le audio
AllData公司负责人19 小时前
通过Postgresql同步到Doris,全视角演示AllData数据中台核心功能效果,涵盖:数据入湖仓,数据同步,数据处理,数据服务,BI可视化驾驶舱
java·大数据·数据库·数据仓库·人工智能·python·postgresql