问题描述:
我这边用的是rk3588
的核心板,刷的固件里面有opencv 4.5.5
, 系统是ubuntu 20.04 LTS
版本, 使用cv::VideoCapture
无法读取mp4
或其他视频,每次读取后isOpened()
都是 false;
硬件是rk3588
对应的开发板
以下编译安装直接在开发板上进行,不是在宿主机用交叉编译链编译安装的。
opencv
安装下载 opencv4.5.5.zip
, 解压后,在源代码目录新建 build
目录,进入 build
目录;
确保安装了相关依赖
确保安装了相关依赖
确保安装了相关依赖
sudo apt-get update
sudo apt-get upgrade
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
sudo apt-get install -f ffmpeg # show video
如果 libjasper
安装失败,请往下翻
cd opencv4.5.5
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=~/install/opencv4.5.5 -DBUILD_IPP_IW=OFF -DBUILD_WITH_DYNAMIC_IPP=OFF -DBUILD_JPEG=ON -DBUILD_PNG=ON -DBUILD_TIFF=ON -DBUILD_ZLIB=ON -DBUILD_SHARED_LIBS=ON -DBUILD_opencv_java_bindings_generator=OFF -DBUILD_opencv_python_bindings_generator=OFF -DWITH_MATLAB=OFF -DWITH_LAPACK=OFF -DWITH_FFMPEG=ON -DWITH_TBB=ON -DWITH_V4L=ON -DWITH_GTK=ON -DWITH_OPENGL=ON -DWITH_IPP=OFF ..
make -j$(nproc)
make install
或者新建一个一键编译安装的脚本
build_ubuntu2004_arm64_opencv4.5.5.sh
#!/usr/bin/bash
# ###############################################
# # 确保安装了相关依赖
# sudo apt-get update
# sudo apt-get upgrade
# 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
# sudo apt-get install -f ffmpeg # show video
# ###################################################
# opencv4.5.5
SRC_NAME_VER=opencv4.5.5
# 压缩包
SRC_ZIP=~/depends/${SRC_NAME_VER}.zip
# 源码和编译目录
SRC_DIR=~/depends/${SRC_NAME_VER}
BUILD_DIR=${SRC_DIR}/build
# 安装目录
INSTALL_ROOT=~/install
INSTALL_DIR=${INSTALL_ROOT}/${SRC_NAME_VER}
# 清空源码目录、编译目录以及对应的安装目录
rm -rf ${BUILD_DIR}
rm -rf ${SRC_DIR}
rm -rf ${INSTALL_DIR}
# 新建源码目录,编译目录和安装目录
mkdir -p ${SRC_DIR}
mkdir -p ${BUILD_DIR}
mkdir -p ${INSTALL_DIR}
# 解压安装包
unzip -d $SRC_DIR $SRC_ZIP
# 进入对应目录进行编译
pushd ${BUILD_DIR}
cmake -DCMAKE_BUILD_TYPE=RELEASE \
-DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} \
-DBUILD_IPP_IW=OFF \
-DBUILD_WITH_DYNAMIC_IPP=OFF \
-DBUILD_JPEG=ON \
-DBUILD_PNG=ON \
-DBUILD_TIFF=ON \
-DBUILD_ZLIB=ON \
-DBUILD_SHARED_LIBS=ON \
-DBUILD_JAVA=OFF \
-DBUILD_opencv_java_bindings_generator=OFF \
-DBUILD_opencv_python_bindings_generator=OFF \
-DBUILD_opencv_python3=OFF \
-DBUILD_opencv_python_tests=OFF \
-DWITH_MATLAB=OFF \
-DWITH_LAPACK=OFF \
-DWITH_FFMPEG=ON \
-DFFMPEG_DIR=/usr/local/ffmpeg \
-DWITH_GSTREAMER=ON \
-DWITH_TBB=ON \
-DWITH_V4L=ON \
-DWITH_GTK=ON \
-DWITH_OPENCL=ON \
-DWITH_OPENGL=ON \
-DWITH_IPP=OFF ..
make -j$(nproc)
make install
popd
libjasper
或许确认这个就可以了大意了,一开始安装的时候,没有太在意这个库,所以一直重新编都没有管这个库;
所以在安装 opencv 的时候一定要按照步骤来,否则要找很久的问题。
如果上述opencv
重新编译安装还是出现无法读取视频的问题,请确保每个依赖库都已经成功安装。
我相信很多人还是无法读取视频,那么就确认这个库吧。
sudo apt install libjasper1 libjasper-dev
执行上述命令会报错。那怎么办呢?修改 /etc/apt/source.list
sudo cp /etc/apt/source.list /etc/apt/source.list.bak
sudo gedit /etc/apt/source.list
deb http://mirrors.aliyun.com/ubuntu-ports/ focal main restricted
deb http://mirrors.aliyun.com/ubuntu-ports/ focal-updates main restricted
deb http://mirrors.aliyun.com/ubuntu-ports/ focal universe
deb http://mirrors.aliyun.com/ubuntu-ports/ focal-updates universe
deb http://mirrors.aliyun.com/ubuntu-ports/ focal multiverse
deb http://mirrors.aliyun.com/ubuntu-ports/ focal-updates multiverse
deb http://mirrors.aliyun.com/ubuntu-ports/ focal-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu-ports/ focal-security main restricted
deb http://mirrors.aliyun.com/ubuntu-ports/ focal-security universe
deb http://mirrors.aliyun.com/ubuntu-ports/ focal-security multiverse
# 新增
deb http://ports.ubuntu.com/ubuntu-ports xenial-security main
sudo apt update
sudo apt upgrade
sudo apt install libjasper1 libjasper-dev
NOTE: 以下过程是试错过程,不一定是必要过程,如果上述过程能解决问题,下面就不用管了。
这里为什么要重新安装 ffmpeg
? 翻阅了很多博客,说要重新安装 ffmpeg
.
但是这里也不一定需要重新安装,重新安装肯定没问题。ffmpeg 版本不要安装太高,因为接口变换较大,可能 opencv 4.5.5
不兼容,我这里试过好几个版本( ffmpeg 6.0
, ffmpeg 5.0.3
, ffmpeg 4.4.4
),在重新编译opencv时都编译失败。 ffmpeg 4.2.9
可以成功。
Download FFmpeg
ffmpeg-4.2.9
./configure --enable-shared --prefix=/usr/local/ffmpeg
make -j4
sudo make install
INSTALL libavdevice/libavdevice.a
INSTALL libavdevice/libavdevice.so
STRIP install-libavdevice-shared
INSTALL libavfilter/libavfilter.a
INSTALL libavfilter/libavfilter.so
STRIP install-libavfilter-shared
INSTALL libavformat/libavformat.a
INSTALL libavformat/libavformat.so
STRIP install-libavformat-shared
INSTALL libavcodec/libavcodec.a
INSTALL libavcodec/libavcodec.so
STRIP install-libavcodec-shared
INSTALL libswresample/libswresample.a
INSTALL libswresample/libswresample.so
STRIP install-libswresample-shared
INSTALL libswscale/libswscale.a
INSTALL libswscale/libswscale.so
STRIP install-libswscale-shared
INSTALL libavutil/libavutil.a
INSTALL libavutil/libavutil.so
STRIP install-libavutil-shared
INSTALL doc/ffmpeg.1
INSTALL doc/ffplay.1
INSTALL doc/ffprobe.1
INSTALL doc/ffmpeg-all.1
INSTALL doc/ffplay-all.1
INSTALL doc/ffprobe-all.1
INSTALL doc/ffmpeg-utils.1
INSTALL doc/ffmpeg-scaler.1
INSTALL doc/ffmpeg-resampler.1
INSTALL doc/ffmpeg-codecs.1
INSTALL doc/ffmpeg-bitstream-filters.1
INSTALL doc/ffmpeg-formats.1
INSTALL doc/ffmpeg-protocols.1
INSTALL doc/ffmpeg-devices.1
INSTALL doc/ffmpeg-filters.1
INSTALL doc/libavutil.3
INSTALL doc/libswscale.3
INSTALL doc/libswresample.3
INSTALL doc/libavcodec.3
INSTALL doc/libavformat.3
INSTALL doc/libavdevice.3
INSTALL doc/libavfilter.3
INSTALL doc/ffmpeg.1
INSTALL doc/ffplay.1
INSTALL doc/ffprobe.1
INSTALL doc/ffmpeg-all.1
INSTALL doc/ffplay-all.1
INSTALL doc/ffprobe-all.1
INSTALL doc/ffmpeg-utils.1
INSTALL doc/ffmpeg-scaler.1
INSTALL doc/ffmpeg-resampler.1
INSTALL doc/ffmpeg-codecs.1
INSTALL doc/ffmpeg-bitstream-filters.1
INSTALL doc/ffmpeg-formats.1
INSTALL doc/ffmpeg-protocols.1
INSTALL doc/ffmpeg-devices.1
INSTALL doc/ffmpeg-filters.1
INSTALL doc/libavutil.3
INSTALL doc/libswscale.3
INSTALL doc/libswresample.3
INSTALL doc/libavcodec.3
INSTALL doc/libavformat.3
INSTALL doc/libavdevice.3
INSTALL doc/libavfilter.3
INSTALL install-progs-yes
INSTALL ffmpeg
INSTALL ffplay
INSTALL ffprobe
INSTALL presets/libvpx-1080p50_60.ffpreset
INSTALL presets/libvpx-360p.ffpreset
INSTALL presets/libvpx-1080p.ffpreset
INSTALL presets/libvpx-720p50_60.ffpreset
INSTALL presets/libvpx-720p.ffpreset
INSTALL doc/ffprobe.xsd
INSTALL doc/examples/transcoding.c
INSTALL doc/examples/vaapi_transcode.c
INSTALL doc/examples/demuxing_decoding.c
INSTALL doc/examples/filtering_video.c
INSTALL doc/examples/muxing.c
INSTALL doc/examples/avio_dir_cmd.c
INSTALL doc/examples/extract_mvs.c
INSTALL doc/examples/encode_audio.c
INSTALL doc/examples/hw_decode.c
INSTALL doc/examples/filter_audio.c
INSTALL doc/examples/resampling_audio.c
INSTALL doc/examples/decode_video.c
INSTALL doc/examples/metadata.c
INSTALL doc/examples/remuxing.c
INSTALL doc/examples/filtering_audio.c
INSTALL doc/examples/http_multiclient.c
INSTALL doc/examples/avio_reading.c
INSTALL doc/examples/qsvdec.c
INSTALL doc/examples/vaapi_encode.c
INSTALL doc/examples/decode_audio.c
INSTALL doc/examples/transcode_aac.c
INSTALL doc/examples/encode_video.c
INSTALL doc/examples/scaling_video.c
INSTALL doc/examples/README
INSTALL doc/examples/Makefile
INSTALL doc/examples/transcoding.c
INSTALL doc/examples/vaapi_transcode.c
INSTALL doc/examples/demuxing_decoding.c
INSTALL doc/examples/filtering_video.c
INSTALL doc/examples/muxing.c
INSTALL doc/examples/avio_dir_cmd.c
INSTALL doc/examples/extract_mvs.c
INSTALL doc/examples/encode_audio.c
INSTALL doc/examples/hw_decode.c
INSTALL doc/examples/filter_audio.c
INSTALL doc/examples/resampling_audio.c
INSTALL doc/examples/decode_video.c
INSTALL doc/examples/metadata.c
INSTALL doc/examples/remuxing.c
INSTALL doc/examples/filtering_audio.c
INSTALL doc/examples/http_multiclient.c
INSTALL doc/examples/avio_reading.c
INSTALL doc/examples/qsvdec.c
INSTALL doc/examples/vaapi_encode.c
INSTALL doc/examples/decode_audio.c
INSTALL doc/examples/transcode_aac.c
INSTALL doc/examples/encode_video.c
INSTALL doc/examples/scaling_video.c
INSTALL doc/examples/README
INSTALL doc/examples/Makefile
INSTALL libavdevice/avdevice.h
INSTALL libavdevice/version.h
INSTALL libavdevice/libavdevice.pc
INSTALL libavfilter/avfilter.h
INSTALL libavfilter/buffersink.h
INSTALL libavfilter/buffersrc.h
INSTALL libavfilter/version.h
INSTALL libavfilter/libavfilter.pc
INSTALL libavformat/avformat.h
INSTALL libavformat/avio.h
INSTALL libavformat/version.h
INSTALL libavformat/libavformat.pc
INSTALL libavcodec/ac3_parser.h
INSTALL libavcodec/adts_parser.h
INSTALL libavcodec/avcodec.h
INSTALL libavcodec/avdct.h
INSTALL libavcodec/avfft.h
INSTALL libavcodec/d3d11va.h
INSTALL libavcodec/dirac.h
INSTALL libavcodec/dv_profile.h
INSTALL libavcodec/dxva2.h
INSTALL libavcodec/jni.h
INSTALL libavcodec/mediacodec.h
INSTALL libavcodec/qsv.h
INSTALL libavcodec/vaapi.h
INSTALL libavcodec/vdpau.h
INSTALL libavcodec/version.h
INSTALL libavcodec/videotoolbox.h
INSTALL libavcodec/vorbis_parser.h
INSTALL libavcodec/xvmc.h
INSTALL libavcodec/libavcodec.pc
INSTALL libswresample/swresample.h
INSTALL libswresample/version.h
INSTALL libswresample/libswresample.pc
INSTALL libswscale/swscale.h
INSTALL libswscale/version.h
INSTALL libswscale/libswscale.pc
INSTALL libavutil/adler32.h
INSTALL libavutil/aes.h
INSTALL libavutil/aes_ctr.h
INSTALL libavutil/attributes.h
INSTALL libavutil/audio_fifo.h
INSTALL libavutil/avassert.h
INSTALL libavutil/avstring.h
INSTALL libavutil/avutil.h
INSTALL libavutil/base64.h
INSTALL libavutil/blowfish.h
INSTALL libavutil/bprint.h
INSTALL libavutil/bswap.h
INSTALL libavutil/buffer.h
INSTALL libavutil/cast5.h
INSTALL libavutil/camellia.h
INSTALL libavutil/channel_layout.h
INSTALL libavutil/common.h
INSTALL libavutil/cpu.h
INSTALL libavutil/crc.h
INSTALL libavutil/des.h
INSTALL libavutil/dict.h
INSTALL libavutil/display.h
INSTALL libavutil/downmix_info.h
INSTALL libavutil/encryption_info.h
INSTALL libavutil/error.h
INSTALL libavutil/eval.h
INSTALL libavutil/fifo.h
INSTALL libavutil/file.h
INSTALL libavutil/frame.h
INSTALL libavutil/hash.h
INSTALL libavutil/hdr_dynamic_metadata.h
INSTALL libavutil/hmac.h
INSTALL libavutil/hwcontext.h
INSTALL libavutil/hwcontext_cuda.h
INSTALL libavutil/hwcontext_d3d11va.h
INSTALL libavutil/hwcontext_drm.h
INSTALL libavutil/hwcontext_dxva2.h
INSTALL libavutil/hwcontext_qsv.h
INSTALL libavutil/hwcontext_mediacodec.h
INSTALL libavutil/hwcontext_vaapi.h
INSTALL libavutil/hwcontext_videotoolbox.h
INSTALL libavutil/hwcontext_vdpau.h
INSTALL libavutil/imgutils.h
INSTALL libavutil/intfloat.h
INSTALL libavutil/intreadwrite.h
INSTALL libavutil/lfg.h
INSTALL libavutil/log.h
INSTALL libavutil/macros.h
INSTALL libavutil/mathematics.h
INSTALL libavutil/mastering_display_metadata.h
INSTALL libavutil/md5.h
INSTALL libavutil/mem.h
INSTALL libavutil/motion_vector.h
INSTALL libavutil/murmur3.h
INSTALL libavutil/opt.h
INSTALL libavutil/parseutils.h
INSTALL libavutil/pixdesc.h
INSTALL libavutil/pixelutils.h
INSTALL libavutil/pixfmt.h
INSTALL libavutil/random_seed.h
INSTALL libavutil/rc4.h
INSTALL libavutil/rational.h
INSTALL libavutil/replaygain.h
INSTALL libavutil/ripemd.h
INSTALL libavutil/samplefmt.h
INSTALL libavutil/sha.h
INSTALL libavutil/sha512.h
INSTALL libavutil/spherical.h
INSTALL libavutil/stereo3d.h
INSTALL libavutil/threadmessage.h
INSTALL libavutil/time.h
INSTALL libavutil/timecode.h
INSTALL libavutil/timestamp.h
INSTALL libavutil/tree.h
INSTALL libavutil/twofish.h
INSTALL libavutil/version.h
INSTALL libavutil/xtea.h
INSTALL libavutil/tea.h
INSTALL libavutil/tx.h
INSTALL libavutil/lzo.h
INSTALL libavutil/avconfig.h
INSTALL libavutil/ffversion.h
INSTALL libavutil/libavutil.pc
编译安装完成后,在 /usr/local/ffmpeg
下新建 ffmpeg-config.cmake
, 文件内容如下:
ffmpeg-config.cmake
set(ffmpeg_path "${CMAKE_CURRENT_LIST_DIR}")
message("ffmpeg_path: ${ffmpeg_path}")
set(FFMPEG_EXEC_DIR "${ffmpeg_path}/bin")
set(FFMPEG_LIBDIR "${ffmpeg_path}/lib")
set(FFMPEG_INCLUDE_DIRS "${ffmpeg_path}/include")
# library names
set(FFMPEG_LIBRARIES
${FFMPEG_LIBDIR}/libavformat.so
${FFMPEG_LIBDIR}/libavdevice.so
${FFMPEG_LIBDIR}/libavcodec.so
${FFMPEG_LIBDIR}/libavutil.so
${FFMPEG_LIBDIR}/libswscale.so
${FFMPEG_LIBDIR}/libswresample.so
${FFMPEG_LIBDIR}/libavfilter.so
)
# found status
set(FFMPEG_libavformat_FOUND TRUE)
set(FFMPEG_libavdevice_FOUND TRUE)
set(FFMPEG_libavcodec_FOUND TRUE)
set(FFMPEG_libavutil_FOUND TRUE)
set(FFMPEG_libswscale_FOUND TRUE)
set(FFMPEG_libswresample_FOUND TRUE)
set(FFMPEG_libavfilter_FOUND TRUE)
# library versions, 注意这几个变量,一定要设置为全局CACHE变量
set(FFMPEG_libavutil_VERSION 56.31.100 CACHE INTERNAL "FFMPEG_libavutil_VERSION") # info
set(FFMPEG_libavcodec_VERSION 58.54.100 CACHE INTERNAL "FFMPEG_libavcodec_VERSION") # info
set(FFMPEG_libavformat_VERSION 58.29.100 CACHE INTERNAL "FFMPEG_libavformat_VERSION") # info
set(FFMPEG_libavdevice_VERSION 58.8.100 CACHE INTERNAL "FFMPEG_libavdevice_VERSION") # info
set(FFMPEG_libavfilter_VERSION 7.57.100 CACHE INTERNAL "FFMPEG_libavfilter_VERSION") # info
set(FFMPEG_libswscale_VERSION 5.5.100 CACHE INTERNAL "FFMPEG_libswscale_VERSION") # info
set(FFMPEG_libswresample_VERSION 3.5.100 CACHE INTERNAL "FFMPEG_libswresample_VERSION") # info
set(FFMPEG_FOUND TRUE)
set(FFMPEG_LIBS ${FFMPEG_LIBRARIES})
status(" #################################### FFMPEG:" FFMPEG_FOUND THEN "YES (find_package)" ELSE "NO (find_package)")
status(" avcodec:" FFMPEG_libavcodec_VERSION THEN "YES (${FFMPEG_libavcodec_VERSION})" ELSE NO)
status(" avformat:" FFMPEG_libavformat_VERSION THEN "YES (${FFMPEG_libavformat_VERSION})" ELSE NO)
status(" avutil:" FFMPEG_libavutil_VERSION THEN "YES (${FFMPEG_libavutil_VERSION})" ELSE NO)
status(" swscale:" FFMPEG_libswscale_VERSION THEN "YES (${FFMPEG_libswscale_VERSION})" ELSE NO)
status(" avresample:" FFMPEG_libavresample_VERSION THEN "YES (${FFMPEG_libavresample_VERSION})" ELSE NO)
以上还不能解决问题的话,删除所有 opencv 的安装,全部重装。
安装顺序如下:
sudo apt install xxx
)ffmpeg
源码编译安装opencv
debug-ubuntu下opencv无法读取视频且capture.isOpened()测试一直为false-可参考
opencv+ffmpeg编译打包全解指南-可参考
【环境配置】Windows 11 的 WSL(Ubuntu2204) 安装OpenCV 4.5.4 (亲测有效)
linux下 opencv 和 ffmpeg安装
RK3588移植-opencv交叉编译aarch64
瑞芯微-交叉编译环境搭建-opencv环境搭建(基于RV1109-1126)-(基于ubuntu18.04)
RK3588编译Opencv最新版
树莓派4B(aarch64,arm64)bullseye无法定位libjasper-dev