[英]Python: How to use ffmpeg to read videos(video file or rtsp) and display them using OpenCV?
I know that if we want to read video file or USB camera or rtsp stream, we can use cv2.VideoCapture
. 我知道,如果我们想读取视频文件或USB摄像机或rtsp流,可以使用
cv2.VideoCapture
。
But for some reason, I want to use ffmpeg to do the thing and process obtained images using opencv. 但是由于某种原因,我想使用ffmpeg来做事情,并使用opencv处理获得的图像。 So the pseudocode would be:
因此,伪代码为:
import cv2
import ffmpeg
videoFile = 'XXX.mp4'
video = ffmpeg.XXX(videoFile)
while True:
frame = video.read() # this read() function is made up by me
frameCV = ffmpeg2cv(frame) # this ffmpeg2cv() function is made up by me
cv2.imshow('frame', frameCV)
I have searched a something like https://github.com/kkroening/ffmpeg-python . 我搜索了类似https://github.com/kkroening/ffmpeg-python的内容 。 But I still not know how to do this.
但是我仍然不知道该怎么做。 I think ffmpeg is too hard and there are few online docs unlike opencv.
我认为ffmpeg太难了,很少有在线文档不像opencv。 So please help.
所以请帮忙。
This is what I found on the topic: https://www.bugcodemaster.com/article/extract-images-frame-frame-video-file-using-ffmpeg They propose a call extracting one image each second called: fps=1 Below they suggest how to extract each frame like: -vf select=eq(pict_type\\,I) 这是我在以下主题中发现的: https : //www.bugcodemaster.com/article/extract-images-frame-frame-video-file-using-ffmpeg他们提出了一个呼叫,每秒提取一个图像,称为:fps = 1下面他们建议如何提取每个帧,例如:-vf select = eq(pict_type \\,I)
So I would suggest using subprocess and try something like this: 因此,我建议使用子流程并尝试如下操作:
import subprocess
time = 200 # of your movie
frame_rate = 50
pathToFfmpeg = "SOME\\PATH"
pathToInput = "ANOTHER\\PATH"
pathToOuput = "YET\\ANOTHER\\PATH"
proc = subprocess.run([pathToFfmeg + "\\ffmpeg.exe",
"-framerate", str(frame_rate),
"-loglevel", "panic", # Suppress Log
"-i", pathToInput + "\\SomeName.avi",
# Now copying their code
"-vf", "select=eq(pict_type\,I)",
"-vsync", "vfr", pathToOuput + "\\Name.jpg",
"-hide_banner"]) # Specify Output
And then you can read the outout images with opencv. 然后,您可以使用opencv读取输出图像。
Note: You need all the "" for subprocess 注意:您需要所有“”用于子流程
Choose an Output name that contains a counter/iterator. 选择一个包含计数器/迭代器的输出名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.