0

I'm trying to create a HLS stream using OpenCV and Gstreamer in Linux (Ubuntu 20.10). The OpenCv was successfully installed with GStreamer support. I have created a simple application with the help of these two tutorials: http://4youngpadawans.com/stream-live-video-to-browser-using-gstreamer/

How to use Opencv VideoWriter with GStreamer?

The code is the following:

#include <string>
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio/videoio_c.h>

using namespace std;
using namespace cv;
int main()
{
    VideoCapture cap;
    if(!cap.open(0, CAP_V4L2))
        return 0;

 VideoWriter writer(
 "appsrc ! videoconvert ! videoscale ! video/x-raw,width=640,height=480 ! x264enc ! mpegtsmux ! hlssink playlist-root=http://192.168.1.42:8080 location=/home/sem/hls/segment_%05d.ts target-duration=5 max-files=5 playlist-location=/home/sem/hls/playlist.m3u8 ",
  0,
  20,
  Size(800, 600),
  true);

    if (!writer.isOpened()) {
        std::cout <<"VideoWriter not opened"<<endl;
        exit(-1);
    }
    for(;;)
    {
          Mat frame;
          cap >> frame;
          

          if( frame.empty() ) break; // end of video stream


          writer.write(frame);

          imshow("this is you, smile! :)", frame);
          if( waitKey(10) == 27 ) break; // stop capturing by pressing ESC 
    }
}

The HTTP served was started using python command

python3 -m http.server 8080

At first look everything is fine. Streamer creates all needed files (playlist and xxx.ts files) Folder with the HTTP Server content

Server Response on requests

But if I try to play the stream it does not work:

Open Stream in browser

The playing using VLC-Player does not work also (green screen)

Could someone give me a hint, what I'm doing wrong? Thanks in advance!

Yunus Temurlenk
  • 4,085
  • 4
  • 18
  • 39
Semiory
  • 3
  • 2

2 Answers2

0

Check what stream format is created. Check what color format you push into the pipeline. If its RGB chances are you create a non 4:2:0 stream which has very limited decoder support.

Florian Zwoch
  • 6,764
  • 2
  • 12
  • 21
0

Thanks Florian, I tried to change the format but it was not a problem. First, what shall be performed is to take a real frame rate from capture device

 int fps = cap.get(CV_CAP_PROP_FPS);

 VideoWriter writer(
 "appsrc ! videoconvert ! videoscale ! video/x-raw, width=640, height=480 ! x264enc !  mpegtsmux ! hlssink playlist-root=http://192.168.1.42:8080 location=/home/sem/hls/segment_%05d.ts target-duration=5 max-files=5 playlist-location=/home/sem/hls/playlist.m3u8 ",
  0,
  fps,
  Size(640, 480),
  true);

Second, the frame size shall be the same in all places it is mentioned. The frame, which is captured, shall be also resized:

  resize(frame, frame, Size(640,480));
  writer.write(frame);

After this changes the chunks, generated by gstreamer, can be opened in a local player and the video works. Unfortunately the remote access still failing. :(

Semiory
  • 3
  • 2