我正在尝试将OpenCV图像传输到ffmpeg。我已经用ffmpeg处理来自OpenCV的原始帧,但是输出是断断续续的,并且只显示每个帧的一部分。我的代码如下:
#include "opencv2/opencv.hpp"
#include <iostream>
#include <unistd.h>
#include <string.h>
using namespace cv;
using namespace std;
VideoCapture vcap(0);
int main(int argc, char** argv) {
try {
if (!vcap.isOpened()) {
cout << "Cannot open video stream. Exiting." << endl;
return -1;
} else {
vcap.set(CV_CAP_PROP_FPS, 25);
}
string stream_window = "Current Output Stream";
namedWindow(stream_window, CV_WINDOW_AUTOSIZE);
Mat frame;
while (1) {
vcap >> frame;
vector<uchar> buf;
/// Showing the result
buf.assign(frame.datastart, frame.dataend);
for (auto itr = buf.begin(); itr != buf.end(); ++itr) {
cout << *itr;
}
cout << endl;
imshow(stream_window, frame);
if(waitKey(4) == 27) break;
}
} catch (exception& e) {
cout << "Exception: " << endl << e.what();
}
return 0;
}
使用
将结果通过管道传递给ffmpeg./stream.out | ffmpeg -f rawvideo -pixel_format bgr24 -video_size 640x480 -r 25 -i pipe: vid.mp4
ffmpeg命令是否有问题,还是有更好的方法将数据传输到ffmpeg?