我在我的项目中使用OpenCV 3.2 Library for Android来保存视频,但它不起作用,没有错误,也没有文件。
这是onCameraFrame
中的代码:
if (isRecord) {
if (mVideoWriter == null) {
mVideoWriter = new VideoWriter(recordfilepath(), VideoWriter.fourcc('M','J','P','G'), 15, mRgba.size());
}
if (!mVideoWriter.isOpened()) {
mVideoWriter.open(recordfilepath(), VideoWriter.fourcc('M','J','P','G'), 15, mRgba.size());
}
if (!mVideoWriter.isOpened()) {
Log.w(TAG, "onCameraFrame: open fail!");
}
mVideoWriter.write(mRgba);
}
似乎mVideoWriter没有打开。 那么,我如何使用Opencv3.2 VideoWriter录制视频?
感谢您的帮助!
更新
我将文件名从.mp4
更改为.avi
,然后就可以了!但是文件没有声音,屏幕是黑色的,我不知道为什么。
UPDATE2: 我成功。这是我的代码:
if (isRecord) {
Log.w(TAG, "onCameraFrame: 录制");
if (mVideoWriter == null) {
Log.w(TAG, "onCameraFrame: 初始化");
//'P','I','M','1'
// 'M','P','E','G'
// 'M','J','P','G'
mVideoWriter = new VideoWriter(recordfilepath(), VideoWriter.fourcc('M', 'J', 'P', 'G'), 25.0D, mRgba.size());
mVideoWriter.open(recordfilepath(), VideoWriter.fourcc('M', 'J', 'P', 'G'), 25.0D, mRgba.size());
Log.i(TAG, "onCameraFrame: recordFilePath" + recordfilepath());
}
if (!mVideoWriter.isOpened()) {
Log.w(TAG, "onCameraFrame: open");
mVideoWriter.open(recordfilepath(), VideoWriter.fourcc('M', 'J', 'P', 'G'), 25.0D, mRgba.size());
}
mVideoWriter.write(mRgba);
Log.w(TAG, "onCameraFrame: 写入中 " + mRgba.toString());
} else {
if (mVideoWriter != null) {
mVideoWriter.release();
}
}
private String recordfilepath() {
// ongetTime();
File sddir = Environment.getExternalStorageDirectory();
File vrdir = new File(sddir, folder_name);
mTimeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File file = new File(vrdir, "KLI_" + mTimeStamp + ".avi");
String filepath = file.getAbsolutePath();
Log.e("debug mediarecorder", filepath);
return filepath;
}
答案 0 :(得分:0)
cv::VideoWriter writer;
writer.open("your_mp4_file_path.mp4", cv::VideoWriter::fourcc('H', '2', '6', '4'),
15, //framerate
cv::Size(720, 1280),
true);
writer << mat_frame;
// remember writer.release() when finish
Android 平台,opencv4.5.2 with ffmpeg + openh264,对我来说效果很好