简体   繁体   English

Python:如何使用ffmpeg读取视频(视频文件或rtsp)并使用OpenCV显示它们?

[英]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.

相关问题 如何从 a.yuv 视频文件中提取某些帧并使用 FFmpeg、OpenCV 和 python 创建新视频? - How to extract certain frames from a .yuv video file and create a new video using FFmpeg, OpenCV and python? 如何在python中使用opencv从sftp位置读取视频文件 - How to read video file from sftp location using opencv in python 如何在使用 openCV python 捕获视频时将视频上传到 aws - how to upload video to aws while capturing videos using openCV python 如何使用 python 中的 opencv 将视频的特定部分保存到单独的视频中? - How to save specific parts of video into separate videos using opencv in python? 如何使用python和Opencv阅读视频文件 - How to read video files using python & Opencv 如何使用opencv在python中读取视频流 - How to read video stream in python using opencv 如何在CPU使用率较低的情况下从OpenCV读取RTSP视频? - How to read RTSP Video from OpenCV with Low CPU Usage? 如何在 Python OpenCV 中显示和读取实时摄像机视频 - How to display and read a live camera video in Python OpenCV 如何使用 Gstreamer 和 Python RTSP 流式传输视频? - How to RTSP stream a video using Gstreamer and Python? 如何使用OpenCV(Python)读取视频文件而无需循环(Mac OS)? - How can I use OpenCV (Python) to read a video file WITHOUT looping (Mac OS)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM