1

I m trying to send the processed opencv Mat as video to ffmpeg. I m encoding the frame and writing it to std output and then piping it to ffmpeg. Here is my code.

C++:

if(!cap.isOpened()) {
    cout << "Video not accessible" << endl;
    return -1;
} else {
    cout << "Video is accessible" << endl;
}

while (true) {

    cap >> frame;

    //some processing

    cv::imencode(".jpg", frame, buff);
    for (i = buff.begin(); i != buff.end(); ++i)
        std::cout << *i ;
}

My input video resolution is 640x418. I do not alter the video size

After building, I use following command to execute.

./a.out | ffmpeg -f rawvideo -pix_fmt bgr24 -s 640x418 -r 30 -i - -an -f mpegts udp://0.0.0.0:8182

and also this

./a.out | ffmpeg -i pipe:0 -f rawvideo -pix_fmt bgr24 -s 640x418 -r 30 -i - -an -f mpegts udp://0.0.0.0:8182

However none of this seems to work.

Kindly help.

  • Why don't exploit OpenCV VideoWriter class? It uses ffmpeg libraries if they are available – MBo May 20 '16 at 05:29
  • This may help : http://stackoverflow.com/questions/12999674/ffmpeg-which-file-formats-support-stdin-usage – Pierre May 20 '16 at 08:30

2 Answers2

4

Not sure if Jahan is still interested in using pipe, but here's a belated answer:

  1. What you write to stdout are images, not videos, so the input format should be -f jpeg_pipe and not rawvideo.
  2. The input source should be -i pipe: (colon included)
  3. No encoding is needed in the pipe; rather, encoding is specified in ffserver config. For image frames I find that using stream type mjpg is straight forward.
  4. A simpler write in opencv can be: fwrite (buff.data(), buff.size(), 1, stdout)
GeorgDangl
  • 2,146
  • 1
  • 29
  • 37
KYLum
  • 41
  • 2
  • Would you mind providing the command line you would use? I've been trying to figure this one out and could not get it to work properly. Thanks! – Simon Garnier Feb 28 '18 at 03:03
1

Same question ... same answer... I hope this answer will help you.

In this program I make a screen copy(RGB data) at 20 fps and send image to ffmpeg. i don't use pipe but socket. I use this command line :

ffmpeg -f rawvideo -pixel_format rgb24  -video_size 640x480 -i  "tcp://127.0.0.1:2345" -codec:v libx264 -pix_fmt yuv420p Video.mp4

to run ffmpeg and then send data to port 2345 using socket

sock->Write(b.GetData(), nb);

I don't encode frame it is raw data

LBerger
  • 593
  • 2
  • 12