欢送拜访我的GitHub

https://github.com/zq2599/blog_demos

内容:所有原创文章分类汇总及配套源码,波及Java、Docker、Kubernetes、DevOPS等;

FFmpeg、JavaCPP、JavaCV的关系

  • 先简略的梳理一下FFmpeg、JavaCPP、JavaCV的关系:
  • FFmpeg、OpenCV能够了解成C语言版的本地库(Native library),Java利用无奈间接应用
  • JavaCPP将FFmpeg、OpenCV这些罕用库做了包装(wrapper),使得Java利用也能应用这些Native API(JavaCPP的底层实现是JNI)
  • 这些JavaCPP包装后的API,被JavaCV封装成了工具类(utility classes),这些工具类比原生API更简略易用
  • 简略的说如下图所示,JavaCPP是Native API转Java API,JavaCV是Java API封装成工具类,这些工具类更加简略易用:

学习目标

  • 欣宸的指标是学习和把握JavaCV,而深刻JavaCV外部去理解它用到的JavaCPP,就相当于打好根底,今后应用JavaCV的时候,也能看懂其外部的实现原理;
  • 于是乎,通过JavaCPP应用FFmpeg就成了基本功,本文会开发一个java利用,调用JavaCPP的API实现以下工作:
  • 关上指定的流媒体
  • 取一帧解码,失去YUV420P格局的图像
  • 将YUV420P格局的图像转为YUVJ420P格局
  • 将图像用jpg格局保留在指定地位
  • 开释所有关上的资源
  • 可见上述一系列步骤已笼罩编解码和图像处理等常见操作,对咱们理解FFmpeg库有很大帮忙

常识储备

  • 在理论编码前,建议您对FFmpeg的重要数据结构和API做一些理解,这方面最经典的材料莫过于雷神的系列教程了,尤其是解协定、解封装、解码波及到的数据结构(上下文)和API,都应该简略理解一遍
  • 如果您切实太忙没有工夫翻阅这些经典,我这筹备了一份快餐版,对重要知识点做了简略的小结,这里要申明一下:欣宸的快餐版远不如雷神的经典系列...
  • 先看数据结构,次要分为媒体数据和上下文两大类,以及底层指针对应的java类:

  • 接着是罕用API,依照雷神的解协定、解封装、解码思路(还有反过来的编码和封装解决)去分类和了解,很容易将它们梳理分明:

版本信息

本次编码波及的操作系统、软件、库的版本信息如下:

  1. 操作系统:win10 64位
  2. IDE:IDEA 2021.1.3 (Ultimate Edition)
  3. JDK:1.8.0_291
  4. maven:3.8.1
  5. javacpp:1.4.3
  6. ffmpeg:4.0.2(所以ffmpeg-platform库的版本是4.0.2-1.4.3)

源码下载

  • 本篇实战中的残缺源码可在GitHub下载到,地址和链接信息如下表所示(https://github.com/zq2599/blo...):
名称链接备注
我的项目主页https://github.com/zq2599/blo...该我的项目在GitHub上的主页
git仓库地址(https)https://github.com/zq2599/blo...该我的项目源码的仓库地址,https协定
git仓库地址(ssh)git@github.com:zq2599/blog_demos.git该我的项目源码的仓库地址,ssh协定
  • 这个git我的项目中有多个文件夹,本篇的源码在<font color="blue">javacv-tutorials</font>文件夹下,如下图红框所示:

  • <font color="blue">javacv-tutorials</font>文件夹下有多个子工程,本篇的源码在<font color="red">ffmpeg-basic</font>文件夹下,如下图红框:

开始编码

  • 为了对立治理源码和jar依赖,我的项目采纳了maven父子构造,父工程名为<font color="blue">javacv-tutorials</font>,外面有一些jar的版本定义,就不多说了
  • 在<font color="blue">javacv-tutorials</font>上面新建名为<font color="red">ffmpeg-basic</font>的子工程,其pom.xml内容如下,可见仅用了JavaCPP,并未用到JavaCV:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <parent>        <artifactId>javacv-tutorials</artifactId>        <groupId>com.bolingcavalry</groupId>        <version>1.0-SNAPSHOT</version>    </parent>    <modelVersion>4.0.0</modelVersion>    <artifactId>ffmpeg-basic</artifactId>    <dependencies>        <dependency>            <groupId>org.bytedeco</groupId>            <artifactId>javacpp</artifactId>        </dependency>        <dependency>            <groupId>org.bytedeco.javacpp-presets</groupId>            <artifactId>ffmpeg-platform</artifactId>        </dependency>        <dependency>            <groupId>org.projectlombok</groupId>            <artifactId>lombok</artifactId>        </dependency>        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-api</artifactId>        </dependency>        <dependency>            <groupId>ch.qos.logback</groupId>            <artifactId>logback-classic</artifactId>        </dependency>    </dependencies></project>
  • 接下来开始编码,先写一个最简略的外部类,将AVFrame和它对应的数据指针BytePointer都放在这个类中,在调用办法的时候便于传递:
class FrameData {    AVFrame avFrame;    BytePointer buffer;    public FrameData(AVFrame avFrame, BytePointer buffer) {        this.avFrame = avFrame;        this.buffer = buffer;    }}
  • 接下来是整个程序最重要的办法<font color="blue">openMediaAndSaveImage</font>,该办法是整个程序的主体,负责将关上流媒体、解码、转格局、保留、开释等五个步骤串起来,内部只有调用这个办法就能实现整个性能:
/**     * 关上流媒体,取一帧,转为YUVJ420P,再保留为jpg文件     * @param url     * @param out_file     * @throws IOException     */    public void openMediaAndSaveImage(String url,String out_file) throws IOException {        log.info("正在关上流媒体 [{}]", url);        // 关上指定流媒体,进行解封装,失去解封装上下文        AVFormatContext pFormatCtx = getFormatContext(url);        if (null==pFormatCtx) {            log.error("获取解封装上下文失败");            return;        }        // 控制台打印流媒体信息        av_dump_format(pFormatCtx, 0, url, 0);        // 流媒体解封装后有一个保留了所有流的数组,videoStreamIndex示意视频流在数组中的地位        int videoStreamIndex = getVideoStreamIndex(pFormatCtx);        // 找不到视频流就间接返回        if (videoStreamIndex<0) {            log.error("没有找到视频流");            return;        }        log.info("视频流在流数组中的第[{}]个流是视频流(从0开始)", videoStreamIndex);        // 失去解码上下文,曾经实现了初始化        AVCodecContext pCodecCtx = getCodecContext(pFormatCtx, videoStreamIndex);        if (null==pCodecCtx) {            log.error("生成解码上下文失败");            return;        }        // 从视频流中解码一帧        AVFrame pFrame = getSingleFrame(pCodecCtx,pFormatCtx, videoStreamIndex);        if (null==pFrame) {            log.error("从视频流中取帧失败");            return;        }        // 将YUV420P图像转成YUVJ420P        // 转换后的图片的AVFrame,及其对应的数据指针,都放在frameData对象中        FrameData frameData = YUV420PToYUVJ420P(pCodecCtx, pFrame);        if (null==frameData) {            log.info("YUV420P格局转成YUVJ420P格局失败");            return;        }        // 长久化存储        saveImg(frameData.avFrame,out_file);        // 按程序开释        release(true, null, null, pCodecCtx, pFormatCtx, frameData.buffer, frameData.avFrame, pFrame);        log.info("操作胜利");    }
  • 当初整体逻辑曾经分明了,再来看外面openMediaAndSaveImage外面调用的那些办法的源码,先看关上流媒体的<font color="blue">getFormatContext</font>:
    /**     * 生成解封装上下文     * @param url     * @return     */    private AVFormatContext getFormatContext(String url) {        // 解封装上下文        AVFormatContext pFormatCtx = new avformat.AVFormatContext(null);        // 关上流媒体        if (avformat_open_input(pFormatCtx, url, null, null) != 0) {            log.error("关上媒体失败");            return null;        }        // 读取流媒体数据,以取得流的信息        if (avformat_find_stream_info(pFormatCtx, (PointerPointer<Pointer>) null) < 0) {            log.error("取得媒体流信息失败");            return null;        }        return pFormatCtx;    }
  • 流媒体解封装后有一个保留了所有流的数组,<font color="blue">getVideoStreamIndex</font>办法会找到视频流在数组中的地位:
    /**     * 流媒体解封装后失去多个流组成的数组,该办法找到视频流咋数组中的地位     * @param pFormatCtx     * @return     */    private static int getVideoStreamIndex(AVFormatContext pFormatCtx) {        int videoStream = -1;        // 解封装后有多个流,找出视频流是第几个        for (int i = 0; i < pFormatCtx.nb_streams(); i++) {            if (pFormatCtx.streams(i).codec().codec_type() == AVMEDIA_TYPE_VIDEO) {                videoStream = i;                break;            }        }        return videoStream;    }
  • 解封装之后就是解码,<font color="blue">getCodecContext</font>办法失去解码上下文对象:
    /**     * 生成解码上下文     * @param pFormatCtx     * @param videoStreamIndex     * @return     */    private AVCodecContext getCodecContext(AVFormatContext pFormatCtx, int videoStreamIndex) {        //解码器        AVCodec pCodec;        // 失去解码上下文        AVCodecContext pCodecCtx = pFormatCtx.streams(videoStreamIndex).codec();        // 依据解码上下文失去解码器        pCodec = avcodec_find_decoder(pCodecCtx.codec_id());        if (pCodec == null) {            return null;        }        // 用解码器来初始化解码上下文        if (avcodec_open2(pCodecCtx, pCodec, (AVDictionary)null) < 0) {            return null;        }        return pCodecCtx;    }
  • 紧接着从视频流解码取帧解码:
    /**     * 取一帧而后解码     * @param pCodecCtx     * @param pFormatCtx     * @param videoStreamIndex     * @return     */    private AVFrame getSingleFrame(AVCodecContext pCodecCtx, AVFormatContext pFormatCtx, int videoStreamIndex) {        // 调配帧对象        AVFrame pFrame = av_frame_alloc();        // frameFinished用于查看是否有图像        int[] frameFinished = new int[1];        // 是否找到的标记        boolean exists = false;        AVPacket packet = new AVPacket();        try {            // 每一次while循环都会读取一个packet            while (av_read_frame(pFormatCtx, packet) >= 0) {                // 查看packet所属的流是不是视频流                if (packet.stream_index() == videoStreamIndex) {                    // 将AVPacket解码成AVFrame                    avcodec_decode_video2(pCodecCtx, pFrame, frameFinished, packet);// Decode video frame                    // 如果有图像就返回                    if (frameFinished != null && frameFinished[0] != 0 && !pFrame.isNull()) {                        exists = true;                        break;                    }                }            }        } finally {            // 肯定要执行开释操作            av_free_packet(packet);        }        // 找不到就返回空        return exists ?  pFrame : null;    }
  • 解码后的图像是YUV420P格局,咱们将其转成YUVJ420P:
    /**     * 将YUV420P格局的图像转为YUVJ420P格局     * @param pCodecCtx 解码上下文     * @param sourceFrame 源数据     * @return 转换后的帧极其对应的数据指针     */    private static FrameData YUV420PToYUVJ420P(AVCodecContext pCodecCtx, AVFrame sourceFrame) {        // 调配一个帧对象,保留从YUV420P转为YUVJ420P的后果        AVFrame pFrameRGB = av_frame_alloc();        if (pFrameRGB == null) {            return null;        }        int width = pCodecCtx.width(), height = pCodecCtx.height();        // 一些参数设定        pFrameRGB.width(width);        pFrameRGB.height(height);        pFrameRGB.format(AV_PIX_FMT_YUVJ420P);        // 计算转为YUVJ420P之后的图片字节数        int numBytes = avpicture_get_size(AV_PIX_FMT_YUVJ420P, width, height);        // 分配内存        BytePointer buffer = new BytePointer(av_malloc(numBytes));        // 图片解决工具的初始化操作        SwsContext sws_ctx = sws_getContext(width, height, pCodecCtx.pix_fmt(), width, height, AV_PIX_FMT_YUVJ420P, SWS_BICUBIC, null, null, (DoublePointer) null);        // 将pFrameRGB的data指针指向方才调配好的内存(即buffer)        avpicture_fill(new avcodec.AVPicture(pFrameRGB), buffer, AV_PIX_FMT_YUVJ420P, width, height);        // 转换图像格式,将解压进去的YUV420P的图像转换为YUVJ420P的图像        sws_scale(sws_ctx, sourceFrame.data(), sourceFrame.linesize(), 0, height, pFrameRGB.data(), pFrameRGB.linesize());        // 及时开释        sws_freeContext(sws_ctx);        // 将AVFrame和BytePointer打包到FrameData中返回,这两个对象都要做显示的开释操作        return new FrameData(pFrameRGB, buffer);    }
  • 而后就是另一个很重要办法<font color="blue">saveImg</font>,外面是典型的编码和输入流程,咱们后面曾经理解了关上媒体流解封装解码的操作,当初要看看怎么制作媒体流,包含编码、封装和输入:
    /**     * 将传入的帧以图片的模式保留在指定地位     * @param pFrame     * @param out_file     * @return 小于0示意失败     */    private int saveImg(avutil.AVFrame pFrame, String out_file) {        av_log_set_level(AV_LOG_ERROR);//设置FFmpeg日志级别(默认是debug,设置成error能够屏蔽大多数不必要的控制台音讯)        AVPacket pkt = null;        AVStream pAVStream = null;        int width = pFrame.width(), height = pFrame.height();        // 调配AVFormatContext对象        avformat.AVFormatContext pFormatCtx = avformat_alloc_context();        // 设置输入格局(波及到封装和容器)        pFormatCtx.oformat(av_guess_format("mjpeg", null, null));        if (pFormatCtx.oformat() == null) {            log.error("输入媒体流的封装格局设置失败");            return -1;        }        try {            // 创立并初始化一个和该url相干的AVIOContext            avformat.AVIOContext pb = new avformat.AVIOContext();            // 关上输入文件            if (avio_open(pb, out_file, AVIO_FLAG_READ_WRITE) < 0) {                log.info("输入文件关上失败");                return -1;            }            // 封装之上是协定,这里将封装上下文和协定上下文关联            pFormatCtx.pb(pb);            // 构建一个新stream            pAVStream = avformat_new_stream(pFormatCtx, null);            if (pAVStream == null) {                log.error("将新的流放入媒体文件失败");                return -1;            }            int codec_id = pFormatCtx.oformat().video_codec();            // 设置该stream的信息            avcodec.AVCodecContext pCodecCtx = pAVStream.codec();            pCodecCtx.codec_id(codec_id);            pCodecCtx.codec_type(AVMEDIA_TYPE_VIDEO);            pCodecCtx.pix_fmt(AV_PIX_FMT_YUVJ420P);            pCodecCtx.width(width);            pCodecCtx.height(height);            pCodecCtx.time_base().num(1);            pCodecCtx.time_base().den(25);            // 打印媒体信息            av_dump_format(pFormatCtx, 0, out_file, 1);            // 查找解码器            avcodec.AVCodec pCodec = avcodec_find_encoder(codec_id);            if (pCodec == null) {                log.info("获取解码器失败");                return -1;            }            // 用解码器来初始化解码上下文            if (avcodec_open2(pCodecCtx, pCodec, (PointerPointer<Pointer>) null) < 0) {                log.error("解码上下文初始化失败");                return -1;            }            // 输入的Packet            pkt = new avcodec.AVPacket();            // 调配            if (av_new_packet(pkt, width * height * 3) < 0) {                return -1;            }            int[] got_picture = { 0 };            // 把流的头信息写到要输入的媒体文件中            avformat_write_header(pFormatCtx, (PointerPointer<Pointer>) null);            // 把帧的内容进行编码            if (avcodec_encode_video2(pCodecCtx, pkt, pFrame, got_picture)<0) {                log.error("把帧编码为packet失败");                return -1;            }            // 输入一帧            if ((av_write_frame(pFormatCtx, pkt)) < 0) {                log.error("输入一帧失败");                return -1;            }            // 写文件尾            if (av_write_trailer(pFormatCtx) < 0) {                log.error("写文件尾失败");                return -1;            }            return 0;        } finally {            // 资源清理            release(false, pkt, pFormatCtx.pb(), pAVStream.codec(), pFormatCtx);        }    }
  • 最初是开释资源的操作,请留神开释不同对象要用到的API也不同,另外AVFormatContext的场景不同用到的API也不同(输入输出场景),用错了就会crash,另外release办法一共被调用了两次,也就说关上媒体流和输入媒体流用到的资源和对象,最终都须要开释和回收:
   /**     * 开释资源,程序是先开释数据,再开释上下文     * @param pCodecCtx     * @param pFormatCtx     * @param ptrs     */    private void release(boolean isInput, AVPacket pkt, AVIOContext pb, AVCodecContext pCodecCtx, AVFormatContext pFormatCtx, Pointer...ptrs) {        if (null!=pkt) {            av_free_packet(pkt);        }        // 解码后,这是个数组,要遍历解决        if (null!=ptrs) {            Arrays.stream(ptrs).forEach(avutil::av_free);        }        // 解码        if (null!=pCodecCtx) {            avcodec_close(pCodecCtx);        }        // 解协定        if (null!=pb) {            avio_close(pb);        }        // 解封装        if (null!=pFormatCtx) {            if (isInput) {                avformat_close_input(pFormatCtx);            } else {                avformat_free_context(pFormatCtx);            }        }    }
  • 最初写个main办法,调用openMediaAndSaveImage试试,传入媒体流的地址,以及寄存图片的门路:
    public static void main(String[] args) throws Exception {        // CCTV13,1920*1080分辨率,不稳固,关上失败时请多试几次        String url = "http://ivi.bupt.edu.cn/hls/cctv13hd.m3u8";        // 安徽卫视,1024*576分辨率,较为稳固//        String url = "rtmp://58.200.131.2:1935/livetv/ahtv";        // 本地视频文件,请改为您本人的本地文件地址//        String url = "E:\\temp\\202107\\24\\test.mp4";        // 残缺图片寄存门路,留神文件名是以后的年月日时分秒        String localPath = "E:\\temp\\202107\\24\\save\\" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".jpg";        // 开始操作        new Stream2Image().openMediaAndSaveImage(url, localPath);    }
  • 以上所有代码都在子工程<font color="blue">ffmpeg-basic</font>的<font color="red">Stream2Image.java</font>文件中,运行main办法,控制台输入如下,可见流媒体关上胜利,并且输入了具体的媒体信息:
18:28:35.553 [main] INFO com.bolingcavalry.basic.Stream2Image - 正在关上流媒体 [http://ivi.bupt.edu.cn/hls/cctv13hd.m3u8]18:28:37.062 [main] INFO com.bolingcavalry.basic.Stream2Image - 视频流在流数组中的第[0]个流是视频流(从0开始)18:28:37.219 [main] INFO com.bolingcavalry.basic.Stream2Image - 操作胜利[hls,applehttp @ 00000188548ab140] Opening 'http://ivi.bupt.edu.cn/hls/cctv13hd-1627208880000.ts' for reading[hls,applehttp @ 00000188548ab140] Opening 'http://ivi.bupt.edu.cn/hls/cctv13hd-1627208890000.ts' for reading[NULL @ 000001887ba68bc0] non-existing SPS 0 referenced in buffering period[NULL @ 000001887ba68bc0] SPS unavailable in decode_picture_timing[h264 @ 000001887ba6aa80] non-existing SPS 0 referenced in buffering period[h264 @ 000001887ba6aa80] SPS unavailable in decode_picture_timingInput #0, hls,applehttp, from 'http://ivi.bupt.edu.cn/hls/cctv13hd.m3u8':  Duration: N/A, start: 1730.227267, bitrate: N/A  Program 0     Metadata:      variant_bitrate : 0    Stream #0:0: Video: h264 (Main) ([27][0][0][0] / 0x001B), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 25 fps, 25 tbr, 90k tbn, 50 tbc    Metadata:      variant_bitrate : 0    Stream #0:1: Audio: aac (LC) ([15][0][0][0] / 0x000F), 48000 Hz, 5.1, fltp    Metadata:      variant_bitrate : 0[swscaler @ 000001887cb28bc0] deprecated pixel format used, make sure you did set range correctlyProcess finished with exit code 0
  • 去存储图片的目录下查看,图片曾经生成:

  • 至此,Java版流媒体解码存图的实战就实现了,咱们对JavaCPP包装的FFmpeg罕用函数有了根本的理解,晓得了编解码和图像处理的常见套路,前面在应用JavaCV工具类时,也明确了其外部基本原理,在定位问题、性能优化、深入研究等场景领有了更多劣势。

你不孤独,欣宸原创一路相伴

  1. Java系列
  2. Spring系列
  3. Docker系列
  4. kubernetes系列
  5. 数据库+中间件系列
  6. DevOps系列

欢送关注公众号:程序员欣宸

微信搜寻「程序员欣宸」,我是欣宸,期待与您一起畅游Java世界...
https://github.com/zq2599/blog_demos