opencv-cuda编译以及使用包括问题处理


装前温馨提示

如果你要使用Gpu进行rtsp编解码这块需要注意了:
1.Opencv3.*版本在进行rtsp-gpu编解码的时候提到了需要ffmpegvideosource这个接口,无法进行rtsp编解码,但是对于视频的处理还是可以的,并且性能准确。
2.对于上述rtsp无法编解码问题,我的解决方法是4.*版本解决。
3.很多朋友都选择私信,我从第三条博客开始每条博客都有我的微信联系方式,现在学术界认识得人也慢慢变多,所有需要交流的地方请直接加微信吧,不收费,也不用发红包啥的,微信2267348110,私信就是有缘回,微信一定能找到。(帮助你们,我自己也能学到很多)


提示:以下是本篇文章正文内容,下面案例可供参考

一、常规安装流程

1.ffmpeg安装

1.常规依赖项安装

sudo apt-get update sudo apt-get -y install autoconf automake build-essential libass-dev libfreetype6-dev \ libsdl2-dev libtheora-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev \ libxcb-xfixes0-dev pkg-config texinfo zlib1g-dev
apt-get install yasm -y && apt-get install libx264-dev -y && apt-get install libx265-dev -y && \ apt-get install libvpx-dev -y && \ apt-get install libfdk-aac-dev -y && \ apt-get install libmp3lame-dev -y && \ apt-get install libopus-dev -y
sudo apt-get install yasm
sudo apt-get install libx264-dev
sudo apt-get install libx265-dev
sudo apt-get install libvpx-dev
sudo apt-get install libfdk-aac-dev
sudo apt-get install libmp3lame-dev
sudo apt-get install libopus-dev
sudo apt-get -y install glew-utils libglew-dbg libglew-dev libglew1.13 \ libglewmx-dev libglewmx-dbg freeglut3 freeglut3-dev freeglut3-dbg libghc-glut-dev \ libghc-glut-doc libghc-glut-prof libalut-dev libxmu-dev libxmu-headers libxmu6 \ libxmu6-dbg libxmuu-dev libxmuu1 libxmuu1-dbg

2.下载ffmpeg

git clone https://github.com/FFmpeg/FFmpeg ffmpeg -b master

3.安装nv-codec-headers

提示:可以使ffmpeg具备cuda功能 -https://github.com/FFmpeg/nv-codec-headers/tree/sdk/编译安装sdk

4.编译

cd ffmpeg
./configure --prefix=/usr/local/ffmpeg --disable-asm --disable-x86asm \ --enable-cuda --enable-cuvid --enable-nvenc \ --enable-nonfree --enable-libnpp \ --extra-cflags=-I/usr/local/cuda/include \ --extra-cflags=-fPIC --extra-ldflags=-L/usr/local/cuda/lib64 \ --enable-gpl --enable-libx264 --enable-libx265 \ --enable-shared \ --enable-libass \ --enable-libfdk-aac \ --enable-libfreetype \ --enable-libmp3lame \ --enable-libopus \ --enable-libtheora \ --enable-libvorbis make -j8 sudo make -j8 install make -j8 distclean hash -r

5.配置环境变量

sudo vi /etc/ld.so.conf 添加: /usr/local/ffmpeg/lib sudo ldconfig

sudo vi /etc/profile 
加入以下内容 export PATH=/usr/local/ffmpeg/bin:$PATH export FFMPEG_HOME=/usr/local/ffmpeg export PATH=$FFMPEG_HOME/bin:$PATH export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib #添加动态库路径 
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/ffmpeg/lib/pkgconfig:/usr/local/lib
 执行 source /etc/profile

6.ffmpeg说明

ffmpeg的安装暂时不会记录的很细致,ffmpeg的安装还是很方便的,接下来把重心全部放在关于opencv和解决相关问题之上。

2、opencv-cuda编译

1.常规依赖项安装

sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev # 处理图像所需的包
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
sudo apt-get install libxvidcore-dev libx264-dev # 处理视频所需的包
sudo apt-get install libatlas-base-dev gfortran # 优化opencv功能
sudo apt-get install ffmpeg

2.下载opencv所需相关文件及编译方式

https://github.com/opencv/opencv #opencv相关版本包(我用的是4.2.0)
https://github.com/opencv/opencv_contrib #opencv扩展包(我用的也是4.2.0)

将opencv_contrib放到opencv当前路径下即可。


3.开始编译

sudo cmake 
-D CMAKE_INSTALL_PREFIX=/usr/local/opencv-4.2.0 
-D CMAKE_BUILD_TYPE=RELEASE 
-D CMAKE_INSTLL_PREFIX=/usr/local 
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.2.0/modules 
-D BUILD_opencv_hdf=OFF 
-D BUILD_opencv_python3=ON 
-D WITH_CUDA=ON 
-D WITH_OPENGL=ON 
-D WITH_OPENMP=ON 
-D WITH_OPENCL=ON 
-D WITH_VTK=ON 
-D WITH_TBB=ON 
-D WITH_GSTREAMER=ON 
-D WITH_CUDNN=ON 
-D WITH_CUBLAS=ON 
-D WITH_GTK_2_X=ON 
-D BUILD_EXAMPLES=ON 
-D OPENCV_ENABLE_NONFREE=ON 
-D WITH_FFMPEG=ON 
-D OPENCV_GENERATE_PKGCONFIG=ON 
-D WITH_NVCUVID=ON 
-D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-11.1 
-D CUDA_ARCH_BIN=6.0,6.1,7.0,7.5 
-D CUDA_ARCH_PTX=7.5 ..

中间会有安装 ippicv,face_landmark_model,boostdesc_bgm,我建议是直接让他安装一次,不强行去指定路径然后从网上下载,越往上的opencv版本越高,有的github找不到现成的。


4.编译自查是否成功(看config)

-- General configuration for OpenCV 4.2.0 =====================================
--   Version control:               unknown
-- 
--   Extra modules:
--     Location (extra):            /home/dachuan/guojun/opencv-4.2.0/opencv_contrib-4.2.0/modules
--     Version control (extra):     unknown
-- 
--   Platform:
--     Timestamp:                   2022-04-20T11:37:54Z
--     Host:                        Linux 5.4.0-91-generic x86_64
--     CMake:                       3.10.2
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               RELEASE
-- 
--   CPU/HW features:
--     Baseline:                    SSE SSE2 SSE3
--       requested:                 SSE3
--     Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
--       requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
--       SSE4_1 (16 files):         + SSSE3 SSE4_1
--       SSE4_2 (2 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
--       FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
--       AVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
--       AVX2 (29 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
--       AVX512_SKX (6 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
-- 
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++ Compiler:                /usr/bin/c++  (ver 7.5.0)
--     C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -fopenmp -O3 -DNDEBUG  -DNDEBUG
--     C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -fopenmp -g  -O0 -DDEBUG -D_DEBUG
--     C Compiler:                  /usr/bin/cc
--     C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fopenmp -O3 -DNDEBUG  -DNDEBUG
--     C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fopenmp -g  -O0 -DDEBUG -D_DEBUG
--     Linker flags (Release):      -Wl,--gc-sections  
--     Linker flags (Debug):        -Wl,--gc-sections  
--     ccache:                      YES
--     Precompiled headers:         NO
--     Extra dependencies:          dl m pthread rt /usr/lib/x86_64-linux-gnu/libGL.so /usr/lib/x86_64-linux-gnu/libGLU.so cudart nppc nppial nppicc nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cufft -L/usr/local/cuda-11.1/lib64
--     3rdparty dependencies:
-- 
--   OpenCV modules:
--     To be built:                 aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hfs highgui img_hash imgcodecs imgproc line_descriptor ml objdetect optflow phase_unwrapping photo plot quality reg rgbd saliency sfm shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
--     Disabled:                    hdf world
--     Disabled by dependency:      -
--     Unavailable:                 cnn_3dobj cvv java js matlab ovis python2 python3 viz
--     Applications:                tests perf_tests examples apps
--     Documentation:               NO
--     Non-free algorithms:         YES
-- 
--   GUI: 
--     GTK+:                        YES (ver 2.24.32)
--       GThread :                  YES (ver 2.56.4)
--       GtkGlExt:                  YES (ver 1.2.0)
--     OpenGL support:              YES (/usr/lib/x86_64-linux-gnu/libGL.so /usr/lib/x86_64-linux-gnu/libGLU.so)
--     VTK support:                 NO
-- 
--   Media I/O: 
--     ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
--     JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80)
--     WEBP:                        /usr/lib/x86_64-linux-gnu/libwebp.so (ver encoder: 0x020e)
--     PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.34)
--     TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.0.9)
--     JPEG 2000:                   /usr/lib/x86_64-linux-gnu/libjasper.so (ver 1.900.1)
--     OpenEXR:                     build (ver 2.3.0)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
-- 
--   Video I/O:
--     DC1394:                      YES (2.2.5)
--     FFMPEG:                      YES
--       avcodec:                   YES (57.107.100)
--       avformat:                  YES (57.83.100)
--       avutil:                    YES (55.78.100)
--       swscale:                   YES (4.8.100)
--       avresample:                YES (3.7.0)
--     GStreamer:                   YES (1.14.5)
--     v4l/v4l2:                    YES (linux/videodev2.h)
-- 
--   Parallel framework:            TBB (ver 2017.0 interface 9107)
-- 
--   Trace:                         YES (with Intel ITT)
-- 
--   Other third-party libraries:
--     Intel IPP:                   2020.0.0 Gold [2020.0.0]
--            at:                   /home/dachuan/guojun/opencv-4.2.0/build/3rdparty/ippicv/ippicv_lnx/icv
--     Intel IPP IW:                sources (2020.0.0)
--               at:                /home/dachuan/guojun/opencv-4.2.0/build/3rdparty/ippicv/ippicv_lnx/iw
--     Lapack:                      NO
--     Eigen:                       YES (ver 3.4.0)
--     Custom HAL:                  NO
--     Protobuf:                    build (3.5.1)
-- 
--   NVIDIA CUDA:                   YES (ver 11.1, CUFFT CUBLAS NVCUVID)
--     NVIDIA GPU arch:             60 61 70 75
--     NVIDIA PTX archs:            75
-- 
--   cuDNN:                         NO
-- 
--   OpenCL:                        YES (no extra features)
--     Include path:                /home/dachuan/guojun/opencv-4.2.0/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.6.9)
--     Libraries:                   NO
--     numpy:                       NO (Python3 wrappers can not be generated)
--     install path:                -
-- 
--   Python (for build):            /usr/bin/python2.7
--     Pylint:                      /home/dachuan/anaconda3/bin/pylint (ver: 3.9.7, checks: 163)
--     Flake8:                      /home/dachuan/anaconda3/bin/flake8 (ver: 3.9.2)
-- 
--   Java:                          
--     ant:                         NO
--     JNI:                         NO
--     Java wrappers:               NO
--     Java tests:                  NO
-- 
--   Install to:                    /usr/local/opencv-4.2.0
-- -----------------------------------------------------------------


4.1查看ffmpeg是否编译成功
 Video I/O:
--     DC1394:                      YES (2.2.5)
--     FFMPEG:                      YES
--       avcodec:                   YES (57.107.100)
--       avformat:                  YES (57.83.100)
--       avutil:                    YES (55.78.100)
--       swscale:                   YES (4.8.100)
--       avresample:                YES (3.7.0)
--     GStreamer:                   YES (1.14.5)
--     v4l/v4l2:                    YES (linux/videodev2.h)

提示:如果在此处出现了NO不用着急后面我会给出解决方法


4.2查看OPENGL是否安装成功
GUI: 
--     GTK+:                        YES (ver 2.24.32)
--       GThread :                  YES (ver 2.56.4)
--       GtkGlExt:                  YES (ver 1.2.0)
--     OpenGL support:              YES (/usr/lib/x86_64-linux-gnu/libGL.so /usr/lib/x86_64-linux-gnu/libGLU.so)
--     VTK support:                 NO

提示:如果在此处出现了NO不用着急后面我会给出解决方法


4.3查看nvcuid是否安装成功
 NVIDIA CUDA:                   YES (ver 11.1, CUFFT CUBLAS NVCUVID)
--     NVIDIA GPU arch:             60 61 70 75
--     NVIDIA PTX archs:            75

提示:如果在此处出现了NO不用着急后面我会给出解决方法


5.开始安装

sudo make -j64&& sudo make install

6.设置环境变量

sudo /etc/ld.so.conf.d/opencv.conf

/usr/local/opencv-4.2.0/lib

ldconfig #opencv基础变量设置成功

总局环境变量:

配置bash
sudo gedit /etc/bash.bashrc //在末尾添加 PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opencv-4.2.0/lib/pkgconfig #根据opencv的lib设置的路径更改 export PKG_CONFIG_PATH
 
保存退出,然后执行如下命令使得配置生效
source /etc/bash.bashrc //激活配置然后更新database sudo updatedb
 
配置本地bash
打开~/.bashrc
$ gedit ~/.bashrc
 
在文件末尾增加以下内容
export PKG_CONFIG_PATH=/usr/local/opencv-4.2.0/lib/pkgconfig export LD_LIBRARY_PATH=/usr/local/opencv-4.2.0/lib
 
更新~/.bashrc
$ source ~/.bashrc

7.测试gpu代码事例(官方公开代码)

#include <iostream>

#include "opencv2/opencv_modules.hpp"

#if defined(HAVE_OPENCV_CUDACODEC)

#include <string>
#include <vector>
#include <algorithm>
#include <numeric>

#include <opencv2/core.hpp>
#include <opencv2/core/opengl.hpp>
#include <opencv2/cudacodec.hpp>
#include <opencv2/highgui.hpp>

int main(int argc, const char* argv[])
{
    if (argc != 2)
        return -1;

    const std::string fname(argv[1]);

    //cv::namedWindow("CPU", cv::WINDOW_NORMAL);
    cv::namedWindow("GPU", cv::WINDOW_OPENGL);
    cv::cuda::setGlDevice();

    cv::Mat frame;
    cv::VideoCapture reader(fname);

    cv::cuda::GpuMat d_frame;
    cv::Ptr<cv::cudacodec::VideoReader> d_reader = cv::cudacodec::createVideoReader(fname);

    cv::TickMeter tm;
    std::vector<double> cpu_times;
    std::vector<double> gpu_times;

    int gpu_frame_count=0, cpu_frame_count=0;

    /*for (;;)
    {
        tm.reset(); tm.start();
        if (!reader.read(frame))
            break;
        tm.stop();
        cpu_times.push_back(tm.getTimeMilli());
        cpu_frame_count++;

        cv::imshow("CPU", frame);

        if (cv::waitKey(3) > 0)
            break;
    }*/

    for (;;)
    {
        tm.reset(); tm.start();
        if (!d_reader->nextFrame(d_frame))
            break;
        tm.stop();
        gpu_times.push_back(tm.getTimeMilli());
        gpu_frame_count++;

        cv::imshow("GPU", d_frame);

        if (cv::waitKey(30) > 0)
           break;
    }

    if (!cpu_times.empty() && !gpu_times.empty())
    {
        std::cout << std::endl << "Results:" << std::endl;

        std::sort(cpu_times.begin(), cpu_times.end());
        std::sort(gpu_times.begin(), gpu_times.end());

        double cpu_avg = std::accumulate(cpu_times.begin(), cpu_times.end(), 0.0) / cpu_times.size();
        double gpu_avg = std::accumulate(gpu_times.begin(), gpu_times.end(), 0.0) / gpu_times.size();

        std::cout << "CPU : Avg : " << cpu_avg << " ms FPS : " << 1000.0 / cpu_avg << " Frames " << cpu_frame_count << std::endl;
        std::cout << "GPU : Avg : " << gpu_avg << " ms FPS : " << 1000.0 / gpu_avg << " Frames " << gpu_frame_count << std::endl;
    }

    return 0;
}

#else

int main()
{
    std::cout << "OpenCV was built without CUDA Video decoding support\n" << std::endl;
    return 0;
}

#endif

g++ -std=c++11 -o main run.cpp -I /usr/local/opencv-4.2.0/include/opencv4/ -L /usr/local/opencv-4.2.0/lib/ -lopencv_cudacodec -lopencv_videostab -lopencv_highgui -lopencv_videoio -lopencv_cudaarithm -lopencv_core -lopencv_imgcodecs -lopencv_imgproc
编译通过,一跑OK,接下来到了鸡冻人心的解决问题环节。


二.解决问题

1.cmake时 avresample: NO

apt-get install libavresample-dev


2.cmake时swscale:NO

apt-get install libswscale-dev


3.cmake时openGL:NO

sudo apt-get install freeglut3-dev mesa-common-dev libgtkglext1 libgtkglext1-dev


4.cmake时 CUDA_nppicom_LIBRARY(ADVANCED) linked by target…

报错信息

CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
CUDA_nppicom_LIBRARY (ADVANCED)
linked by target "opencv_cudev" in directory ....
linked by target "opencv_cudev" in directory ....
linked by target "opencv_test_cudev" in directory ....
...

cuda11.0以上版本已经将nppicom,所以我们需要将cmake文件对应位置删除
找到OpenCVDetectCUDA.cmake文件,找到下述if(CUDA_FOUND)的位置,在下面加上去掉nppicom的库的指令

.....
if(CUDA_FOUND)
    set(HAVE_CUDA 1)
 
 //添加以下四行  
  if(CUDA_VERSION VERSION_GREATER_EQUAL "11.0")
     ocv_list_filterout(CUDA_nppi_LIBRARY "nppicom")
     ocv_list_filterout(CUDA_npp_LIBRARY "nppicom")
   endif()

  if(WITH_CUFFT)
    set(HAVE_CUFFT 1)
  .....


5.关于一切未定义或者libopencv*.so not open,file not detection问题

这些纯纯的的属于环境变量问题,仔细检查我上面环境变量的写法是否一致。还有问题请联系我。


6.cmake时 CUDA_nppi_LIBRARY(ADVANCED) linked by target…

报错信息

CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
CUDA_nppi_LIBRARY (ADVANCED)
linked by target "opencv_cudev" in directory 。。。。
linked by target "opencv_cudev" in directory 。。。。。。。。
linked by target "opencv_test_cudev" in directory 。。。。
...

6.1找到FindCUDA.cmake文件

find_cuda_helper_libs(nppi) 

换成

find_cuda_helper_libs(nppial)  
find_cuda_helper_libs(nppicc)  
find_cuda_helper_libs(nppicom)  
find_cuda_helper_libs(nppidei)  
find_cuda_helper_libs(nppif)  
find_cuda_helper_libs(nppig)  
find_cuda_helper_libs(nppim)  
find_cuda_helper_libs(nppist)  
find_cuda_helper_libs(nppisu)  
find_cuda_helper_libs(nppitc) 

set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppi_LIBRARY};${CUDA_npps_LIBRARY}")  

换成

set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppial_LIBRARY};${CUDA_nppicc_LIBRARY};${CUDA_nppicom_LIBRARY};${CUDA_nppidei_LIBRARY};${CUDA_nppif_LIBRARY};${CUDA_nppig_LIBRARY};${CUDA_nppim_LIBRARY};${CUDA_nppist_LIBRARY};${CUDA_nppisu_LIBRARY};${CUDA_nppitc_LIBRARY};${CUDA_npps_LIBRARY}")   

unset(CUDA_nppi_LIBRARY CACHE)  

换成

unset(CUDA_nppial_LIBRARY CACHE)  
unset(CUDA_nppicc_LIBRARY CACHE)  
unset(CUDA_nppicom_LIBRARY CACHE)  
unset(CUDA_nppidei_LIBRARY CACHE)  
unset(CUDA_nppif_LIBRARY CACHE)  
unset(CUDA_nppig_LIBRARY CACHE)  
unset(CUDA_nppim_LIBRARY CACHE)  
unset(CUDA_nppist_LIBRARY CACHE)  
unset(CUDA_nppisu_LIBRARY CACHE)  
unset(CUDA_nppitc_LIBRARY CACHE) 

6.2将opencv/modules/cudev/include/opencv2/cudev/common.hpp

添加#include<cuda_fp16.h>


7.编译成功视频没问题,OpenCV(4.2.0) Error: Unsupported format or combination of formats (Unsupported video source) in nextFrame, file /home/dachuan/guojun/opencv-4.2.0/opencv_contrib-4.2.0/modules/cudacodec/src/video_reader.cpp, line 140terminate called after throwing an instance of ‘cv::Exception’ what(): OpenCV(4.2.0) /home/dachuan/guojun/opencv-4.2.0/opencv_contrib-4.2.0/modules/cudacodec/src/video_reader.cpp:140: error: (-210:Unsupported format or combination of formats) Unsupported video source in function ‘nextFrame’

1,排查几个问题,是否是自己推得流如果是请检查编码

1.视频转换:

avi-------------------------->flv

ffmpeg -i  file.avi   output.flv

MPEG-1--------------->MPEG-4

ffmpeg -i inputfile.mpg -f mp4 -acodec libfaac -vcodec mpeg4 -b 256k -ab 64k outputfile.mp4

MP3------------------->MPEG-4

ffmpeg -i inputfile.mp3 -f mp4 -acodec libaac -vn -ab 64k outputfile.mp4

DVD的VOB---------->VideoCD格式的MPEG-1

ffmpeg -i inputfile.vob -f mpeg -acodec mp2 -vcodec mpeg1video -s 352x240 -b 1152k -ab 128k outputfile.mpg

AVI------------------->H.264格式的M4V

ffmpeg -i inputfile.avi -f mp4 -acodec libfaac -vcodec libx264 -b 512k -ab 320k outputfile.m4v

任何------------------->东芝REGZA可辨识的MPEG2

ffmpeg -i inputfile -target ntsc-svcd -ab 128k -aspect 4:3 -s 720x480 outputfile.mpg

2.音频视频合并:ok

   ffmpeg -i input1.avi -i input2.mp3 -vcodec copy -acodec copy out.avi

3.拆分音频视频:ok

提取音频:

ffmpeg -i input.flv -vn -acodec copy out.mp3

提取视频:

ffmpeg -i input.flv  -an -vcodec copy  out.flv ok

ffmpeg -i input.flv out.flv  -an -vcodec copy  error------->注意参数顺序

音视频都提取:

ffmpeg -i input.flv -an out1.flv -vn out2.mp3

4.提取图片:ok

ffmpeg -i input.avi -y -f image2 -ss 8 -t 0.001 -s 350x240 out.jpg      

5.压缩文件:ok

ffmpeg -i input.mp3 -ab 128 out.mp3 

6.截取指定的时间间隔的视频:ok

 ffmpeg -ss 00:00:10 -t 00:01:22 -i 五月天-突然好想你.mp3  out.mp3 

7.屏幕录像:

ffmpeg -f x11grab -s xga -r 10 -i :0.0+0+0 wheer.avi 

ffmpeg -f x11grab  -s 320x240  -r 10 -i :0.0+100+200 wheer.avi

ffmpeg -f x11grab -s xga    -qscale 5    -r 10 -i :0.0+0+0 wheer.avi 

8.屏幕录像,同时录音:

ffmpeg -f oss -i /dev/dsp        -f x11grab -r 30 -s 1024x768 -i :0.0  output.mkv

ffmpeg   -ac 2 -f oss  -i  /dev/dsp   -f x11grab -r 30 -s 1024x768 -i :0.0 -acodec pcm_s16le -vcodec libx264 -vpre lossless_ultrafast -threads 0 output.mkv

9.屏幕录像,指定窗口:

10.视频连接:

 ffmpeg -i input1.avi -i input2.avi output.avi  -vcodec copy -acodec copy

11.分割视频:ok

ffmpeg -ss 01:00:00 -i input.mp4 -vcodec copy -acodec copy -t 00:06:00 out.mp4

 

四.参数:

1.常用参数:

-ab      bitrate      设置音频码率

-acodec  codec        使用codec编解码

-ac      channels     设置通道,缺省为1

-ar      freq         设置音频采样率

-r       fps          设置帧频,缺省25

-b       bitrate      设置比特率,缺省200kb/s  

-qscale  64         使用动态码率来设置

我的解决方法就是h264转mpeg1video即可。


三 总结

本人微信 2267348110 ,从博客收获了一大堆学术界的朋友,如果遇到问题请直接加我微信,私信看到回复,然后也会持续更新,评论区偶尔会回下,但是很少,其他博客也都可以看看,互相学习。
奥利给!

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值