0

How to play H.264 camera streams using OpenCV? I was searching for a while but i didn't find an answer. I think OpenCV can encode and decode h.264 videos since it uses ffmpeg and it is the documentation of the class VideoWriter ensures that as shown in this example:

#include <iostream> // for standard I/O
#include <string>   // for strings

#include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat)
#include <opencv2/highgui/highgui.hpp>  // Video write

using namespace std;
using namespace cv;

int main()
{
    VideoWriter outputVideo; // For writing the video

    int width = ...; // Declare width here
    int height = ...; // Declare height here
    Size S = Size(width, height); // Declare Size structure

    // Open up the video for writing
    const string filename = ...; // Declare name of file here

    // Declare FourCC code
    int fourcc = CV_FOURCC('H','2','6','4');

    // Declare FPS here
    int fps = ...;
    outputVideo.open(filename, fourcc, fps, S);

    // Put your processing code here
    // ...

    // Logic to write frames here... see below for more details
    // ...

    return 0;
}

So can OpenCV encode-decode h.264 stream as well? if yes, please let me know how. thanks!!

peter bence
  • 782
  • 3
  • 14
  • 34
  • 1
    you only have shown code to prepare the video WRITER. If I understand your question right you want to CAPTURE, so try a cv::VideoCapture. Typically, openCV confiugurations can read H264 out of the box. – Micka Oct 26 '18 at 10:41
  • yes you are right, i wanna capture however i posted the code to show that opencv is really capable of encoding h.246 videos. I've worked a lot with VideoCaputre in opencv but it only supports a maximum of two argument constructor, nor of them deals with the encoding. also if you keep default configuration VideoCapture doesn't automatically decode h.264 video streams, it just decode it as mpeg (this is already tested) – peter bence Oct 26 '18 at 10:47
  • 1
    As far as I know - you need libx264 (which is not shipped with ffmpeg by default) to be able to encode. – Dmitrii Z. Oct 26 '18 at 11:27
  • 1
    so you want to decode (should work out of the box) or encode (maybe need to compile manually)? – Micka Oct 26 '18 at 12:07
  • i wanna decode an h.246 video stream coming from an ip camera – peter bence Oct 26 '18 at 13:21
  • should be possible with a `cv::VideoCapture` object and the precompiled opencv_ffmpeg*.dll file in windows setting, for example. Just feed the camera url as source string. It will choose the decoder automatically. I've done this with different cameras already and it worked. According to some postings you can give the videoCapture a hint (or is this to parametrize the camera?): `cap.set(CV_CAP_PROP_FOURCC, CV_FOURCC('H', '2', '6', '4'));` – Micka Oct 26 '18 at 20:56

0 Answers0