gpt4 book ai didi

python - 将原始 OpenCV 图像通过管道传输到 FFmpeg

转载 作者:IT老高 更新时间:2023-10-28 22:22:05 42 4
gpt4 key购买 nike

这是一个使用 OpenCV 的 python 绑定(bind)读取网络摄像头的相当简单的示例:

'''capture.py'''
import cv, sys
cap = cv.CaptureFromCAM(0) # 0 is for /dev/video0
while True :
if not cv.GrabFrame(cap) : break
frame = cv.RetrieveFrame(cap)
sys.stdout.write( frame.tostring() )

现在我想将输出通过管道传输到 ffmpeg,如下所示:

$ python capture.py | ffmpeg -f image2pipe -pix_fmt bgr8 -i - -s 640x480 foo.avi

遗憾的是,我无法完全正确地使用 ffmpeg 魔法咒语,并且失败了

  libavutil     50.15. 1 / 50.15. 1  libavcodec    52.72. 2 / 52.72. 2  libavformat   52.64. 2 / 52.64. 2  libavdevice   52. 2. 0 / 52. 2. 0  libavfilter    1.19. 0 /  1.19. 0  libswscale     0.11. 0 /  0.11. 0  libpostproc   51. 2. 0 / 51. 2. 0Output #0, avi, to 'out.avi':    Stream #0.0: Video: flv, yuv420p, 640x480, q=2-31, 19660 kb/s, 90k tbn, 30 tbc[image2pipe @ 0x1508640]max_analyze_duration reached[image2pipe @ 0x1508640]Estimating duration from bitrate, this may be inaccurateInput #0, image2pipe, from 'pipe:':  Duration: N/A, bitrate: N/A    Stream #0.0: Video: 0x0000, bgr8, 25 fps, 25 tbr, 25 tbn, 25 tbcswScaler: 0x0 -> 640x480 is invalid scaling dimension
  • 捕获的帧肯定是 640x480。
  • 我很确定 OpenCV 图像类型 (IplImage) 的像素顺序是 GBR,每个 channel 一个字节。至少,这似乎是从相机中出来的。

我不是 ffmpeg 大师。有没有人成功做到这一点?

最佳答案

费了一番功夫,但我用 FFmpeg rawvideo demuxer 弄明白了:

python capture.py | ffmpeg -f rawvideo -pixel_format bgr24 -video_size 640x480 -framerate 30 -i - foo.avi

由于原始视频中没有 header 指定假定的视频参数,因此用户必须指定它们才能正确解码数据:

  • -framerate 设置输入视频帧率。默认值为 25。
  • -pixel_format 设置输入视频像素格式。默认值为 yuv420p。
  • -video_size 设置输入视频大小。没有默认值,因此必须明确指定此值。

对于高级用户来说,这里有一些额外的东西。同样的事情,但使用 VLC 将实时输出流式传输到网络,Flash 格式:

python capture.py | cvlc --demux=rawvideo --rawvid-fps=30 --rawvid-width=320 --rawvid-height=240  --rawvid-chroma=RV24 - --sout "#transcode{vcodec=h264,vb=200,fps=30,width=320,height=240}:std{access=http{mime=video/x-flv},mux=ffmpeg{mux=flv},dst=:8081/stream.flv}"

编辑:使用 ffmpeg 和 ffserver 创建 webm 流

python capture.py | ffmpeg -f rawvideo -pixel_format rgb24 -video_size 640x480 -framerate 25 -i - http://localhost:8090/feed1.ffm

关于python - 将原始 OpenCV 图像通过管道传输到 FFmpeg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5825173/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com