示例代碼
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/videoio.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
#if 0
//-------------【1】讀取視頻文件-------------
VideoCapture capture;
capture.open("xxxpath/input.h264");
//方式二:VideoCapture capture("xxxpath/input.h264");
//-------------【2】判斷視頻讀取是否正確-------------
if (!capture.isOpened())
{
cout << "打開視頻文件失敗,請重新輸入正確路徑!" << endl;
system("pause");
return -1;
}
//-------------【3】獲取視頻相關信息-------------
//獲取視頻相關信息——幀數
long nTotalFrame = capture.get(CV_CAP_PROP_FRAME_COUNT);
cout << "幀數 = " << nTotalFrame << endl;
//獲取視頻相關信息——幀像素寬/高
int frameHeight = capture.get(CV_CAP_PROP_FRAME_HEIGHT);
int frameWidth = capture.get(CV_CAP_PROP_FRAME_WIDTH);
cout << "幀像素高 = " << frameHeight << endl;
cout << "幀像素寬 = " << frameWidth << endl;
//獲取視頻相關信息——幀率
double FrameRate = capture.get(CV_CAP_PROP_FPS);
cout << "幀率 = " << FrameRate << endl;
//--------------【4】循環顯示每一幀---------------
long nCount = 1; //計數,當前幀號
bool flag = true;
while (flag)
{
//輸出當前幀號
cout << "當前幀號: " << nCount << endl;
Mat frameImg;//定義一個Mat變量,用於存儲每一幀的圖像
capture >> frameImg;
//判斷當前讀取文件
if (frameImg.empty())
{
break;
}
imshow("讀取視頻", frameImg); //顯示當前幀
//按下鍵盤上Q或q鍵退出
if (char(waitKey(40)) == 'q' || char(waitKey(40)) == 'Q') //每幀畫面存在40ms,即1秒25幀
{
break;
}
nCount++;
flag = false;
}
//視頻釋放
capture.release();
#endif
std::string path = "xxxpath";
cv::VideoCapture * m_Cap = new cv::VideoCapture(path);
if (m_Cap) {
std::cout << "video: " << path << " is opened !" << std::endl;
}
else {
std::cout << "video: " << path << " opened failed !" << std::endl;
}
if( !m_Cap->isOpened() )
{
std::cout << "***Could not initialize capturing...***\n";
}
cv::Mat frame;
bool flag = true;
while (m_Cap->read(frame) && flag) {
//flag = false;
}
m_Cap->release();
return 0;
}
以上程序在ubuntu電腦上能夠跑通,正常讀取視頻,但是在開發板上不能讀取,isOpened()函數返回false
查看原因是由於電腦上的libopencv_videoio庫編譯時攜帶了ffmpeg,而板子上沒有,具體查看下圖
電腦上的
板子上的
利用ffmpeg和opencv進行視頻的解碼播放
參考
https://www.jianshu.com/p/6ef3c18d61b0
問題參考
https://bbs.huaweicloud.com/forum/thread-99217-1-1.html