【发布时间】:2018-01-08 09:14:01
【问题描述】:
我想从一个简短的 .mp4 视频中提取所有帧并将它们作为图像保存到文件夹中。但是当我调用函数 cv::VideoCapture::read() 时,它无法提取帧。我的代码有什么问题?
#include <iostream>
#include <string>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/videoio.hpp>
int main(int argc, const char * argv[]) {
cv::VideoCapture cap("test.mp4");
if (!cap.isOpened()) {
std::cout << "Cannot open the video file.\n";
return -1;
}
for (int frame_count = 0; frame_count < cap.get(cv::CAP_PROP_FRAME_COUNT); frame_count++) {
cap.set(cv::CAP_PROP_POS_FRAMES, frame_count);
cv::Mat frame;
if (!cap.read(frame)) {
std::cout << "Failed to extract a frame.\n";
return -1;
}
std::string image_path = "frames/" + std::to_string(frame_count) + ".jpg";
cv::imwrite(image_path, frame);
}
return 0;
}
我的输出总是“提取帧失败”。
【问题讨论】:
-
你试过没有 cap.set 吗?假设在 for 循环之外。
cap.get(cv::CAP_PROP_FRAME_COUNT)返回什么? -
@api55 刚试了下,也提取帧失败。无需调用 cap.set() 并在循环之外。 cap.get(cv::CAP_PROP_FRAME_COUNT) 返回 70
-
尝试分别使用抓取和检索(而不是读取)。您是否尝试过其他视频?也许用另一个编解码器?
-
您的平台和 OpenCV 版本是什么?你是从源代码编译它还是下载二进制文件?
-
@api55 刚刚尝试了另一个视频并且成功了!奇怪的是,它是用同一部手机拍摄的。但我尝试使用的第一个视频是从 iPhone 上传到运行 PHP 服务器脚本的计算机。我假设我以某种方式错误地保存了它(在那个 PHP 脚本中)。因为该视频的编解码器是:hvc1、AAC 和 mebx,而它使用的编解码器是 AAC 和 H.264。我的错,谢谢你的帮助!