OpenCV(programmed in C++) stdout piped to ffmpeg(in bashscript)?
I am using the following bash script to pipe the output of OpenCV which is an out.avi file converting that file to a stdout using the tail command and piping the output to ffmpeg
./OpenCV &
tail -n +0 -f out.avi | ffmpeg -i pipe:0 -hls_time 1 -hls_list_size 0 -hls_wrap 10 -hls_segment_filename '%03d.ts' stream.m3u8
This works but causes latency issues.
I have tried to change the C++ code so that the frame gets written to a stdout using cout << frame;
here is some of the code.
imshow( window_name, frame );//show frame
video.write(frame);//rec to avi
cout << frame;
From the above code I get the window to display the processed image, the frame is saved in avi format and from the last line i get printed to the command line the pixel values of the frames.
Now I have tried the following Bash script to pipe this stdout to ffmpeg with no success.
./OpenCV | ffmpeg -i pipe:0 -hls_time 1 -hls_list_size 0 -hls_wrap 10 -hls_segment_filename '%03d.ts' stream.m3u8
I also tried
./OpenCV | ffmpeg -i pipe:0 -f rawvideo -pix_fmt bgr24 -s 480x320 -hls_time 1 -hls_list_size 0 -hls_wrap 10 -hls_segment_filename '%03d.ts' stream.m3u8
Anybody got any ideas?
the issue is that cout<< frame;
is printing pixel values of the Mat
structure defined by openCV. What do you need is the actual binary data of the frame which you can retrieve using imencode
for example
std::vector<uchar> buff;
cv::imencode(".jpg", frame, buff);
for (auto i = buff.begin(); i != buff.end(); ++i)
std::cout << *i ;
if you want to test it, you can write the buffer to a file and see the output like this
std::vector<uchar> buff;
cv::imencode(".jpg", frame, buff);
std::ofstream myfile;
myfile.open("example.jpg", std::ios::binary);
for (auto i = buff.begin(); i != buff.end(); ++i)
{
//std::cout << *i;
myfile << *i;
}
myfile.close();
链接地址: http://www.djcxy.com/p/81086.html
上一篇: 如何在Linux上的虚拟摄像头上写字?