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()
相关推荐
我材不敲代码3 小时前
Python实现打包贪吃蛇游戏
开发语言·python·游戏
0思必得05 小时前
[Web自动化] Selenium处理动态网页
前端·爬虫·python·selenium·自动化
韩立学长5 小时前
【开题答辩实录分享】以《基于Python的大学超市仓储信息管理系统的设计与实现》为例进行选题答辩实录分享
开发语言·python
qq_192779875 小时前
高级爬虫技巧:处理JavaScript渲染(Selenium)
jvm·数据库·python
u0109272715 小时前
使用Plotly创建交互式图表
jvm·数据库·python
爱学习的阿磊5 小时前
Python GUI开发:Tkinter入门教程
jvm·数据库·python
Imm7776 小时前
中国知名的车膜品牌推荐几家
人工智能·python
tudficdew6 小时前
实战:用Python分析某电商销售数据
jvm·数据库·python
sjjhd6527 小时前
Python日志记录(Logging)最佳实践
jvm·数据库·python
2301_821369617 小时前
用Python生成艺术:分形与算法绘图
jvm·数据库·python