博客
关于我
Python subprocess ffmpeg
阅读量:798 次
发布时间:2023-03-06

本文共 1496 字,大约阅读时间需要 4 分钟。

ffmpeg 实时视频流处理系统配置与实现

本文将介绍一个基于 ffmpeg 的实时视频流处理系统,详细阐述系统的配置方法及核心实现逻辑。

系统配置

1. 环境搭建

  • 操作系统:Windows 10/11
  • ffmpeg 版本:2018年发布的 fa0c9d6-win64-static 版本
  • 编程语言:Python 3.x
  • 依赖库
    • numpy
    • cv2
    • subprocess

2. 命令行参数设置

# 查看帮助信息ffmpeg -help# 查看视频流信息ffmpeg -log info input_file# 查看输出格式支持情况ffmpeg -formats

3. 视频流处理配置

  • 输入设置
    ffmpeg -i rtsp://admin:hik12345@192.168.3.175/cam/realmonitor?channel=1&subtype=1
  • 输出设置
    ffmpeg -i -y -f rawvideo -vcodec rawvideo -pix_fmt bgr24 -s 704*576 -f flv rtmp://192.168.3.56:1935/MyRed5/test

视频流处理核心实现

1. 读取视频流

import osimport sysimport numpy as npimport subprocess as sp# ffmpeg 执行器ffmpeg_bin = "E:/ffmpeg-20180227-fa0c9d6-win64-static/bin/ffmpeg.exe"# 命令行参数command_in = [    ffmpeg_bin,    '-i', input_file,    '-f', 'rawvideo',    '-s', f'{width}*{height}',    '-pix_fmt', 'bgr24',    '-']# 创建 ffmpeg 进程pipe_in = sp.Popen(command_in, stdout=sp.PIPE)

2. 视频流处理

def process_image(img):    cv2.imshow('实时视频', img)# 读取视频流while True:    raw_image = pipe_in.stdout.read(width * height * 3)        if len(raw_image) == 0:        break        # 转换为图像数组    image = np.fromstring(raw_image, dtype='uint8')    image = image.reshape((height, width, 3)).copy()        # 处理当前帧    process_image(image)        # 写入输出流    pipe_out.stdin.write(image.tobytes())    pipe_out.stdin.flush()        # 等待键盘输入    k = cv2.waitKey(25)    if k & 0xff == ord('q'):        break

技术实现总结

  • 视频流输入处理:通过 ffmpeg 读取实时视频流,设置参数如分辨率、像素格式等。
  • 图像处理:使用 OpenCV 库显示实时视频帧。
  • 流处理:将处理后的图像输出到指定 rtmp地址。

整个系统通过 ffmpeg 实现视频流的实时读取与处理,兼顾了高效性与实用性。

转载地址:http://ehafk.baihongyu.com/

你可能感兴趣的文章
Python setuptools sdist:仅安装版本化文件
查看>>
Python Shell下使用matplotlib
查看>>
Python Slice How-to,我知道Python Slice,但我怎么才能使用内置的Slice对象呢?
查看>>
python socket分包发送数据
查看>>
python socket模块_Python socket模块实现TCP服务端客户端
查看>>
Python SOCKS5代理客户端HTTPS
查看>>
Python Soc网络分析:通过使用函数迭代列表来计算机会网络
查看>>
python转换已转义的字符串
查看>>
Python Sphinx自动摘要:成员函数的自动列表
查看>>
Python SQL和NoSQL数据库操作实战
查看>>
python stdout flush_sys.stdout.flush()方法的用法
查看>>
python string 运算
查看>>
Python str与bytes之间的转换
查看>>
Python subprocess ffmpeg
查看>>
python subprocess Permission denied Errno 13
查看>>
Python subprocess.call - 将变量添加到 subprocess.call
查看>>
Python Subprocess.Popen 从一个线程
查看>>
Python subprocess.Popen 作为 Windows 上的不同用户
查看>>
Python转换PPT为PDF
查看>>
Python subprocess.Popen() 等待完成
查看>>