3

I'm new here. I'm trying to stream some images processed with opencv on a LAN using ffmpeg. I saw this: Pipe raw OpenCV images to FFmpeg but it doesn't work for me, it creates only noise. I think the data I'm sending are not in the right format. I also have a look to this: How to send opencv video's to ffmpeg but (looking at the last answer) the option -f jpeg_pipe give me error.

What I do now:

  • I have a RGB Mat called "composedImage"
  • I send in output with: std::cout << composedImage; The output are the pixels values separated by comma
  • then I call:

./example | ffmpeg -f rawvideo -pixel_format bgr24 -video_size 160x120 -framerate 20 -i - -f udp://192.168.1.79:1234

  • I try to read using VLC (it didn't work) and with ffplay:

ffplay -f rawvideo -pixel_format gray -video_size 160x120 -framerate 30 udp://192.168.1.79:1234

Here it seems so easy: http://ffmpeg.org/ffmpeg-formats.html#rawvideo

I have also tried to write the image and sent it, but I have errors. Probably it tries to send before the image is complete.

Thank you for your help.

cristian b
  • 61
  • 1
  • 6

1 Answers1

3

I managed to stream, albeit with potato quality, maybe some ffmpeg guru can help out with the ffmpeg commands.

#include <iostream>                                                          
#include <opencv2/opencv.hpp>                                                
#include <vector>                                      

using namespace cv;                                                          

int main() {                                                                 
    VideoCapture cap("Your video or stream goes here");
    Mat frame;                                                               
    std::ios::sync_with_stdio(false);                                        

    while (cap.read(frame)) {                                                
        for (size_t i = 0; i < frame.dataend - frame.datastart; i++) 
           std::cout << frame.data[i];                                   
    }                                                                        

    return 0;                                                                
}                                                                            

And then pipe it to ffmpeg like

./test | ffmpeg -f rawvideo -pixel_format bgr24 -video_size 1912x796 -re -framerate 20  -i - -f mpegts -preset ultrafast udp://127.0.0.1:1234

And play it with ffplay mpv or whatever

ffplay udp://127.0.0.1:1234
aram
  • 1,415
  • 13
  • 27
  • Thank you for the answer, this almost goes even if with some changes. I hope I can let it go properly – cristian b Mar 02 '18 at 11:30
  • So, the printed values in output are ok, but when sending the streaming I have problems, it seems that the first block of the image is shown at the end of the frame instead of the beginning. At this point I think there are problems in the streaming. – cristian b Mar 02 '18 at 16:26
  • 1
    @cristianb did you change the resolution? – aram Mar 02 '18 at 16:37
  • yes, I've changed the resolution to mine, but the problem is not that. It seems the data are not well encoded, or at least their order. Thanks. – cristian b Mar 02 '18 at 20:47
  • So, finally, I find that adding an offset in the for cycle (the number is found empirically) it works. Not very elegant, but for now is ok. – cristian b Mar 05 '18 at 09:47