順德營銷型網站建設查關鍵詞的排名工具
最近要完成一個功能,就是把四個視頻合成左右上下分布的一個視頻。嘗試很多方法,最終使用opencv來實現該功能。(通過opencv實現的視頻好像沒有聲音。)研究的步驟,首先在Ubuntu環(huán)境測試,該功能是否實現。然后再將生成的庫文件放到AS中,使用jni的方法調用,或者將源碼放到AS中利用jni技術。在實現過程中遇到很多問題,下面記錄。
一、在ubuntu linux環(huán)境使用opencv。
1、下載opencv安裝包。
下載地址:官網:https://opencv.org/releases/
git地址:https://github.com/opencv/opencv/releases
2、將下載的安裝包放到虛擬機根目錄(新建software目錄)。
3、安裝包解壓縮。
unzip opencv-4.8.0.zip
4、下載相關軟件。
進入 opencv-4.8.0 文件夾。
1)更新軟件
sudo apt-get update
2)安裝cmake
sudo apt-get install cmake
3) 安裝依賴庫
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg.dev libtiff5.dev libswscale-dev libjasper-dev
4)在 opencv-4.8.0 文件夾下新建build文件夾。
mkdir build
5)進入build文件夾,使用命令修改參數
sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
6)使用make編譯
sudo make
7)安裝
sudo make install
8)配置環(huán)境
sudo gedit /etc/ld.so.conf.d/opencv.conf
在新建的文檔中添加:
/usr/local/lib
sudo ldconfig
配置路徑生效。
9)配置bash
sudo gedit /etc/bash.bashrc
在最末尾添加
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
執(zhí)行該步驟時,發(fā)現/usr/local/lib/路徑下沒有pkgconfig文件夾。并且執(zhí)行命令
pkg-config --cflags openc
報一下錯誤。
解決:首先創(chuàng)建opencv.pc文件,這里要注意它的路徑信息:
cd /usr/local/lib
sudo mkdir pkgconfig
cd pkgconfig
sudo touch opencv.pc
然后在opencv.pc中添加以下信息,注意這些信息需要與自己安裝opencv時的庫路徑對應:(一下是我自己的內容)
prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include/opencv4Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.8.0
Libs: -L${exec_prefix}/lib -lopencv_highgui -lopencv_shape -lopencv_objdetect -lopencv_ml -lopencv_superres -lopencv_dnn -lopencv_stitching -lopencv_videostab -lopencv_calib3d -lopencv_videoio -lopencv_imgcodecs -lopencv_features2d -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core
Libs.private: -ldl -lm -lpthread -lrt
Cflags: -I${includedir}
保存退出,添加環(huán)境:
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
執(zhí)行.cpp編譯命令:
g++ videotest.cpp -o videotest `pkg-config --cflags --libs opencv`
報錯:
再執(zhí)行編譯命令:
g++ videotest.cpp -o videotest `pkg-config --cflags --libs opencv` -std=gnu++11
報錯:
查看/usr/local/lib/lib文件下沒有l(wèi)ibopencv_shape.so等這三個庫,再次打開/usr/local/lib/pkgconfig/opencv.pc文件,將-lopencv_shape 等三個路徑刪除掉。保存退出。執(zhí)行命令:
g++ videotest.cpp -o videotest `pkg-config --cflags --libs opencv` -std=gnu++11
編譯成功。
最后的opencv.pc文件:
# Package Information for pkg-configprefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include/opencv4Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.8.0
Libs: -L${exec_prefix}/lib -lopencv_highgui -lopencv_objdetect -lopencv_ml -lopencv_dnn -lopencv_stitching -lopencv_calib3d -lopencv_videoio -lopencv_imgcodecs -lopencv_features2d -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core
Libs.private: -ldl -lm -lpthread -lrt
Cflags: -I${includedir}
注意:1、該opencv源碼編譯的對應so庫文件只是linux系統(tǒng)平臺文件(我的是x86_64),如果想將該庫編譯為其他平臺,例如arm64需要交叉編譯。(我沒整明白)
注意:2、上述配置bash后,使用以下命令使得配置文件生效。若沒有生效,重啟電腦試試。(如果沒有生效,執(zhí)行 編譯生成的可執(zhí)行文件,報錯 找不到opencv庫)
source /etc/bash.bashrc
sudo updatedb
注意:3 我這里編譯的是opencv4.8.0版本,編譯過程中使用python3.x版本,opencv.pc需要自己創(chuàng)建。鏈接http://www.taodudu.cc/news/show-3639538.html?action=onClick#google_vignette 編譯的opencv3.4.1版本,使用python2.x版本,opencv.pc自動生成。按照鏈接中配置,就可以成功編譯安裝opencv。
最后是測試代碼,功能將代碼中的video.mp4合成上下左右分布的一個視頻。生成視頻為mergevideo.avi 或mergevideo.mkv
#include <iostream>
#include <opencv2/opencv.hpp>
#include <unistd.h>
#include <error.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <pthread.h>
#include <linux/videodev2.h>
#include <sys/mman.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <iomanip>
#include <string>using namespace std;
using namespace cv;
int main(int argc, char ** argv)
{std::string videoFile = "video.mp4";//視頻的路徑std::string videoFile1 = "video.mp4";//視頻的路徑std::string videoFile2 = "video.mp4";//視頻的路徑std::string videoFile3 = "video.mp4";//視頻的路徑/** 打開第一個視頻文件 */VideoCapture cap; //視頻句柄變量cap.open(videoFile);//打開視頻if(!cap.isOpened()) //判斷是否打開了{ printf("1cap.isOpened is error\n");return -1; } /** 打開第二個視頻文件 */VideoCapture cap1; //視頻句柄變量cap1.open(videoFile1);//打開視頻if(!cap1.isOpened()) //判斷是否打開了{ printf("2cap.isOpened is error\n");return -1; } /** 打開第三個視頻文件 */VideoCapture cap2; //視頻句柄變量cap2.open(videoFile2);//打開視頻if(!cap2.isOpened()) //判斷是否打開了{ printf("3cap.isOpened is error\n");return -1; } /** 打開第四個視頻文件 */VideoCapture cap3; //視頻句柄變量cap3.open(videoFile3);//打開視頻if(!cap3.isOpened()) //判斷是否打開了{ printf("4cap.isOpened is error\n");return -1; } /** 打開第一個視頻文件的幀數 */int frame_num = cap.get(cv::CAP_PROP_FRAME_COUNT);std::cout << "videoFile total frame number is: " << frame_num << std::endl;/** 打開第二個視頻文件的幀數 */int frame_num1 = cap1.get(cv::CAP_PROP_FRAME_COUNT);std::cout << "videoFile1 total frame number is: " << frame_num1 << std::endl;/** 打開第三個視頻文件的幀數 */int frame_num2 = cap2.get(cv::CAP_PROP_FRAME_COUNT);std::cout << "videoFile2 total frame number is: " << frame_num2 << std::endl;/** 打開第四個視頻文件的幀數 */int frame_num3 = cap3.get(cv::CAP_PROP_FRAME_COUNT);std::cout << "videoFile3 total frame number is: " << frame_num3 << std::endl;/** 打開第一個視頻文件的幀率 */int fps = cap.get(cv::CAP_PROP_FPS);std::cout << "videoFile fps: " << fps << std::endl;/** 打開第二個視頻文件的幀率 */int fps1 = cap1.get(cv::CAP_PROP_FPS);std::cout << "videoFile1 fps1: " << fps1 << std::endl;/** 打開第三個視頻文件的幀率 */int fps2 = cap2.get(cv::CAP_PROP_FPS);std::cout << "videoFile fps2: " << fps2 << std::endl;/** 打開第四個視頻文件的幀率 */int fps3 = cap3.get(cv::CAP_PROP_FPS);std::cout << "videoFile1 fps3: " << fps3 << std::endl;/** 打開第一個視頻文件的寬度 */int image_width = cap.get(cv::CAP_PROP_FRAME_WIDTH);std::cout << "videoFile image width is: " << image_width << std::endl;/** 打開第二個視頻文件的寬度 */int image_width1 = cap1.get(cv::CAP_PROP_FRAME_WIDTH);std::cout << "videoFile1 image width is: " << image_width1 << std::endl;/** 打開第三個視頻文件的寬度 */int image_width2 = cap2.get(cv::CAP_PROP_FRAME_WIDTH);std::cout << "videoFile2 image width is: " << image_width2 << std::endl;/** 打開第四個視頻文件的寬度 */int image_width3 = cap3.get(cv::CAP_PROP_FRAME_WIDTH);std::cout << "videoFile3 image width is: " << image_width3 << std::endl;/** 打開第一個視頻文件的高度 */int image_height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);std::cout << "videoFile image height: " << image_height << std::endl;/** 打開第二個視頻文件的高度 */int image_height1 = cap1.get(cv::CAP_PROP_FRAME_HEIGHT);std::cout << "videoFile1 image height: " << image_height1 << std::endl;/** 打開第三個視頻文件的高度 */int image_height2 = cap2.get(cv::CAP_PROP_FRAME_HEIGHT);std::cout << "videoFile2 image height: " << image_height2 << std::endl;/** 打開第四個視頻文件的高度 */int image_height3 = cap3.get(cv::CAP_PROP_FRAME_HEIGHT);std::cout << "videoFile3 image height: " << image_height3 << std::endl;/** 打開第一個視頻文件的矩陣對象的格式*/int frame_format = cap.get(cv::CAP_PROP_FORMAT);std::cout << "videoFile frame format: " << frame_format << std::endl;/** 打開第二個視頻文件的矩陣對象的格式 */int frame_format1 = cap1.get(cv::CAP_PROP_FORMAT);std::cout << "videoFile1 frame format: " &