Linux下JavaCv使用GPU加速(Nvidia显卡)

1.环境配置,安装显卡驱动,cuda,cudnn

​​​​​​linux上安装NVIDIA显卡驱动以及深度学习需要的cudn、cudnn、pytorch_宜城有少年的博客-CSDN博客_linux安装nvidia显卡驱动

2.安装FFmpeg视频转码所需要的包

(1)安装依赖

yum install autoconf automake bzip2 cmake freetype-devel gcc gcc-c++ git libtool make mercurial pkgconfig zlib-devel

(2)安装包

wget http://www.nasm.us/pub/nasm/releasebuilds/2.13.02/nasm-2.13.02.tar.bz2
tar xjvf nasm-2.13.02.tar.bz2
cd nasm-2.13.02
./autogen.sh
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"
make
make install
wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar xzvf yasm-1.3.0.tar.gz
cd yasm-1.3.0
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"
make
make install
git clone git clone https://code.videolan.org/videolan/x264.git
cd x264
PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-static
make
make install

git clone https://bitbucket.org/multicoreware/x265_git.git
cd x265/build/linux
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/ffmpeg_build" -DENABLE_SHARED:bool=off ../../source
make
make install
git clone --depth 1 --branch v0.1.6 https://github.com/mstorsjo/fdk-aac.git
cd fdk-aac
autoreconf -fiv
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
make install
wget http://downloads.sourceforge.net/project/lame/lame/3.100/lame-3.100.tar.gz
tar xzvf lame-3.100.tar.gz
cd lame-3.100
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --disable-shared --enable-nasm
make
make install
wget https://archive.mozilla.org/pub/opus/opus-1.2.1.tar.gz
tar xzvf opus-1.2.1.tar.gz
cd opus-1.2.1
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
make install
wget http://downloads.xiph.org/releases/ogg/libogg-1.3.3.tar.gz
tar xzvf libogg-1.3.3.tar.gz
cd libogg-1.3.3
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
make install
wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.5.tar.gz
tar xzvf libvorbis-1.3.5.tar.gz
cd libvorbis-1.3.5
./configure --prefix="$HOME/ffmpeg_build" --with-ogg="$HOME/ffmpeg_build" --disable-shared
make
make install
git clone --depth 1 https://github.com/webmproject/libvpx.git
cd libvpx
./configure --prefix="$HOME/ffmpeg_build" --disable-examples --disable-unit-tests --enable-vp9-highbitdepth --as=yasm
make
make install
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers
make PREFIX="$HOME/ffmpeg_build" BINDDIR="$HOME/bin"
make install PREFIX="$HOME/ffmpeg_build" BINDDIR="$HOME/bin" 

安装ffmpeg

wget http://ffmpeg.org/releases/ffmpeg-4.4.tar.bz2
tar xjvf ffmpeg-4.4.tar.bz2
cd ffmpeg-4.4
PATH="$HOME/bin:$PATH" PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure \
  --prefix="$HOME/ffmpeg_build" \
  --pkg-config-flags="--static" \
  --extra-cflags="-I$HOME/ffmpeg_build/include -I/usr/local/cuda/include" \
  --extra-ldflags="-L$HOME/ffmpeg_build/lib -L/usr/local/cuda/lib64" \
  --extra-libs=-lpthread \
  --extra-libs=-lm \
  --bindir="$HOME/bin" \
  --enable-gpl \
  --enable-libfdk_aac \
  --enable-libmp3lame \
  --enable-libopus \
  --enable-libvorbis \
  --enable-libvpx \
  --enable-libx264 \
  --enable-libx265 \
  --enable-nonfree \
  --enable-cuda \
  --enable-cuvid \
  --enable-nvenc \
  --enable-libnpp
make
make install
hash -r

查看版本ffmpeg -h

查看编码器 ffmpeg -hwaccels

查看编码视频是否成功

ffmpeg -hwaccel cuvid -c:v h264_cuvid -i /home/rtsp/test.mp4 -c:v h264_nvenc -b:v 2048k -vf scale_npp=1280:-1 -y /home/2.mp4

3.使用GPU加速JavaCV

    //解析视频时
    public FFmpegFrameGrabber getGrabberByUrl(String url) throws  Exception{
        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(url);
        grabber.setFormat("rtsp");
        grabber.setVideoCodecName("hevc_nvenc");//上图安装的解码器,GPU加速的关键
        grabber.setPixelFormat(avutil.AV_PIX_FMT_NV12);//像素格式
        grabber.setImageMode(FrameGrabber.ImageMode.COLOR);
        String byte2Base64= RSAUtil.getMsgByRsa("****");
        grabber.setOption("rtsp_transport", "tcp");//tcp传输协议
        grabber.setOption("appkey", "****");//海康视频 appkey
        grabber.setOption("secret", byte2Base64);//海康视频 secret
        grabber.setOption("playMode", "0");//初始播放模式:0-预览,1-回放
        grabber.setOption("port", "446");//综合安防管理平台端口,若启用HTTPS协议,默认443
        grabber.setOption("enableHTTPS", "1"); //是否启用HTTPS协议与综合安防管理平台交互,这里总是填1
        grabber.setOption("rtsp_flags", "prefer_tcp");
        // 设置缓存大小,提高画质、减少卡顿花屏
        grabber.setOption("buffer_size", "1024000");
        grabber.setOption("stimeout", "3000000");
        grabber.start();
        return grabber;
    }
//推流时
FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(url,grabber.getImageWidth(),grabber.getImageHeight(),1);
                recorder.setVideoCodecName("h264_nvenc");//编码器,GPU加速关键
                recorder.setPixelFormat(avutil.AV_PIX_FMT_NV12);//像素格式
                recorder.setFrameRate(grabber.getFrameRate());
               recorder.setInterleaved(true);
                // 降低编码延时
             /*   recorder.setVideoOption("tune", "zerolatency");
                // 提升编码速度
                recorder.setVideoOption("preset", "ultrafast");*/
                // 视频质量参数(详见 https://trac.ffmpeg.org/wiki/Encode/H.264)
                recorder.setVideoOption("crf", "28");
                // 分辨率
                recorder.setVideoBitrate(2000000);
                // 视频编码格式
                recorder.setVideoCodec(grabber.getVideoCodec());
                // 视频格式
                recorder.setFormat("flv");
                // 视频帧率
                recorder.setFrameRate(grabber.getFrameRate());
                recorder.setGopSize(60);
                //recorder.setAudioOption("crf", "0");
                recorder.setAudioQuality(0);
                recorder.setAudioBitrate(192000);
                recorder.setSampleRate(44100);
                // 建议从grabber获取AudioChannels
               // recorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC);
                recorder.start();

   for(;(frame=grabber.grabImage())!=null;) {
        recorder.record(frame);
    }

使用nvidia-smi查看GPU使用情况

Linux下JavaCv使用GPU加速(Nvidia显卡)_第1张图片

你可能感兴趣的:(javacv,GPU加速,视频流)