`

CentOS 下通过 JavaCPP 调用 FFMPEG

 
阅读更多

1. Java 与 FFMPEG

   FFMPEG 是一个广泛用于媒体处理的库,在Java的世界里,处理视频的能力相当弱,于是有很大需求需要Java 调用 FFMPEG。

  Java 调用C 的方式有很多,可以用最原始的JNI方式,也可以JNA方式,还可以是命令行。

   采用命令行的方式比较简单,不过有很大局限性,尤其是涉及到 视频的处理和分析的时候,比如要取出某个packet,然后进行处理。

 
    这里介绍的是用JavaCPP 调用 ffmpeg 库的方式,而不是命令行模式。

     JavaCPP的源码在这里:https://github.com/bytedeco/javacpp 

 

  基于JavaCPP的项目   

1)JavaCV,是做图形图像的,有人脸识别、增强现实AR 等开源算法,非常不错

    项目主页是:https://github.com/bytedeco/javacv 
  
 2)JavaAV 封装了FFMPEG的java 接口

    项目主页:https://github.com/hoary/JavaAV

 

2.  JavaCPP Presets


  为了方便使用,JavaCPP下面有个presets项目,主页是 https://github.com/bytedeco/javacpp-presets,将一些常用的项目都编译好了以方便使用。
    集成的项目包括:
• OpenCV 2.4.9 http://opencv.org/downloads.html
• FFmpeg 2.3.x http://ffmpeg.org/download.html
• FlyCapture 2.6.x http://ww2.ptgrey.com/sdk/flycap
• libdc1394 2.1.x or 2.2.x http://sourceforge.net/projects/libdc1394/files/
• libfreenect 0.5 https://github.com/OpenKinect/libfreenect
• videoInput 0.200 https://github.com/ofTheo/videoInput/tree/update2013
• ARToolKitPlus 2.3.0 https://launchpad.net/artoolkitplus
• flandmark 1.07 http://cmp.felk.cvut.cz/~uricamic/flandmark/#download
• FFTW 3.3.4 http://www.fftw.org/download.html
• GSL 1.16 http://www.gnu.org/software/gsl/#downloading
• LLVM 3.4.2 http://llvm.org/releases/download.html
• Leptonica 1.71 http://www.leptonica.org/download.html
• Tesseract 3.03-rc1 https://code.google.com/p/tesseract-ocr/

    包含的平台有:   android-arm, android-x86, linux-x86, linux-x86_64, macosx-x86_64, windows-x86, windows-x86_64,

  但需要注意:这里的 linux-x86_64 是 基于Fedora 平台的,无法在 CentOS下使用。

 

3. 在CentOS下编译
  1)安装JDK
  2)安装 apache-maven 2/3

  3) 安装 gcc,gcc+,gcc-c++,yasm

     yum -y install yasm gcc+ gcc-c++


  4)  获取源码

     git clone http://github.com/bytedeco/javacpp-presets
  5)  编译 ffmpeg 


     cd javacpp-presets/
    ./cppbuild.sh -platform linux-x86_64 install ffmpeg 


  6) 编译 jni 和 相关jar


      mvn install --projects ffmpeg

 
   7) 编译完成后,会在 ffmpeg 的lib上生成相关 .so ,javacpp-presets/targets 下生成相关的jar

 

4. 测试
   1) 将相关的 .so 复制到 /lib64 目录下,或者通过 -Djava.library.path 指定到.so 所在目录
   2) 拷贝一个实例用来测试,  

public class Tutorial01 {  
  
    static void SaveFrame(AVFrame pFrame, int width, int height, int iFrame)  
            throws IOException {  
        // Open file  
        OutputStream stream = new FileOutputStream("frame" + iFrame + ".ppm");  
  
        // Write header  
        stream.write(("P6\n" + width + " " + height + "\n255\n").getBytes());  
  
        // Write pixel data  
        BytePointer data = pFrame.data(0);  
        byte[] bytes = new byte[width * 3];  
        int l = pFrame.linesize(0);  
        for (int y = 0; y < height; y++) {  
            data.position(y * l).get(bytes);  
            stream.write(bytes);  
        }  
  
        // Close file  
        stream.close();  
    }  
  
    public static void main(String[] args) throws IOException {  
        AVFormatContext pFormatCtx = new AVFormatContext(null);  
        int i, videoStream;  
        AVCodecContext pCodecCtx = null;  
        AVCodec pCodec = null;  
        AVFrame pFrame = null;  
        AVFrame pFrameRGB = null;  
        AVPacket packet = new AVPacket();  
        int[] frameFinished = new int[1];  
        int numBytes;  
        BytePointer buffer = null;  
  
        AVDictionary optionsDict = null;  
        SwsContext sws_ctx = null;  
  
        if (args.length < 1) {  
            args = new String[] { "/root/test.ts" };  
            // System.out.println("Please provide a movie file");  
            // System.exit(-1);  
        }  
        // Register all formats and codecs  
        av_register_all();  
  
        // Open video file  
        if (avformat_open_input(pFormatCtx, args[0], null, null) != 0) {  
            System.exit(-1); // Couldn't open file  
        }  
  
        // Retrieve stream information  
        if (avformat_find_stream_info(pFormatCtx, (PointerPointer) null) < 0) {  
            System.exit(-1); // Couldn't find stream information  
        }  
  
        // Dump information about file onto standard error  
        av_dump_format(pFormatCtx, 0, args[0], 0);  
  
        // Find the first video stream  
        videoStream = -1;  
        for (i = 0; i < pFormatCtx.nb_streams(); i++) {  
            if (pFormatCtx.streams(i).codec().codec_type() == AVMEDIA_TYPE_VIDEO) {  
                videoStream = i;  
                break;  
            }  
        }  
        if (videoStream == -1) {  
            System.exit(-1); // Didn't find a video stream  
        }  
  
        // Get a pointer to the codec context for the video stream  
        pCodecCtx = pFormatCtx.streams(videoStream).codec();  
  
        // Find the decoder for the video stream  
        pCodec = avcodec_find_decoder(pCodecCtx.codec_id());  
        if (pCodec == null) {  
            System.err.println("Unsupported codec!");  
            System.exit(-1); // Codec not found  
        }  
        // Open codec  
        if (avcodec_open2(pCodecCtx, pCodec, optionsDict) < 0) {  
            System.exit(-1); // Could not open codec  
        }  
  
        // Allocate video frame  
        pFrame = av_frame_alloc();  
  
        // Allocate an AVFrame structure  
        pFrameRGB = av_frame_alloc();  
        if (pFrameRGB == null) {  
            System.exit(-1);  
        }  
  
        // Determine required buffer size and allocate buffer  
        numBytes = avpicture_get_size(AV_PIX_FMT_RGB24, pCodecCtx.width(),  
                pCodecCtx.height());  
        buffer = new BytePointer(av_malloc(numBytes));  
  
        sws_ctx = sws_getContext(pCodecCtx.width(), pCodecCtx.height(),  
                pCodecCtx.pix_fmt(), pCodecCtx.width(), pCodecCtx.height(),  
                AV_PIX_FMT_RGB24, SWS_BILINEAR, null, null,  
                (DoublePointer) null);  
  
        // Assign appropriate parts of buffer to image planes in pFrameRGB  
        // Note that pFrameRGB is an AVFrame, but AVFrame is a superset  
        // of AVPicture  
        avpicture_fill(new AVPicture(pFrameRGB), buffer, AV_PIX_FMT_RGB24,  
                pCodecCtx.width(), pCodecCtx.height());  
  
        // Read frames and save first five frames to disk  
        i = 0;  
        while (av_read_frame(pFormatCtx, packet) >= 0) {  
            // Is this a packet from the video stream?  
            if (packet.stream_index() == videoStream) {  
                // Decode video frame  
                avcodec_decode_video2(pCodecCtx, pFrame, frameFinished, packet);  
  
                // Did we get a video frame?  
                if (frameFinished[0] != 0) {  
                    // Convert the image from its native format to RGB  
                    sws_scale(sws_ctx, pFrame.data(), pFrame.linesize(), 0,  
                            pCodecCtx.height(), pFrameRGB.data(),  
                            pFrameRGB.linesize());  
  
                    // Save the frame to disk  
                    if (++i <= 5) {  
                        SaveFrame(pFrameRGB, pCodecCtx.width(),  
                                pCodecCtx.height(), i);  
                    }  
                }  
            }  
  
            // Free the packet that was allocated by av_read_frame  
            av_free_packet(packet);  
        }  
  
        // Free the RGB image  
        av_free(buffer);  
        av_free(pFrameRGB);  
  
        // Free the YUV frame  
        av_free(pFrame);  
  
        // Close the codec  
        avcodec_close(pCodecCtx);  
  
        // Close the video file  
        avformat_close_input(pFormatCtx);  
  
        System.exit(0);  
    }  
}  

 4)运行实例,得到输出结果:

   
Input #0, mpegts, from '/root/test.ts':
  Duration: 00:03:20.02, start: 0.056778, bitrate: 1455 kb/s
  Program 1
    Metadata:
      service_name    : jVideo
      service_provider: jTeam
    Stream #0:0[0x100]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p, 960x540 [SAR 1:1 DAR 16:9], 25 fps, 25 tbr, 90k tbn, 50 tbc
    Stream #0:1[0x101]: Audio: aac ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp, 66 kb/s

原文:http://blog.csdn.net/maoxiang/article/details/38388387

分享到:
评论

相关推荐

    java调用ffmpeg详细步骤

    使用Java语言调用终端开启关闭Nginx服务器,然后执行ffmpeg命令,开启转流推流。以及其它指令,来学习吧

    linux下Java视频转码、截图开发包:ffmepg3-opencv-javacpp-javacv-centos6&7

    JAVA视频操作的最常用工具包的linux开发库,支持在linux下java调用ffmpeg进行视频操作、转码、截图等操作。如果你自己编译、寻找库费了很长时间而不成功,那么,希望这个资源能够帮到你。 里面的jar及linux动态库,...

    centos6.5下安装ffmpeg的详细步骤

    ### CentOS 6.5 下安装 FFMPEG 的详细步骤及注意事项 #### 一、环境准备与背景介绍 FFMPEG 是一款强大的多媒体处理工具,它能够进行音视频编解码、格式转换、流媒体抓取等多种操作。对于需要处理大量音视频数据的...

    centos7自动安装ffmpeg脚本.sh

    centos7下自动化部署ffmpeg,

    centos6安装ffmpeg

    OS:CentOS release 6 Kernel: 2.6.32-754.35.1.el6.x86_64 ffmpeg: 2.6.8 虽然下载速度慢点,但成功安装了多台机器

    java通过ssh调用centos7指令

    java通过ssh调用centos7指令,获取磁盘空间使用率、磁盘空间使用情况、网络使用情况,CPU使用率,内存使用率

    docker-centos-ffmpeg:用于构建 FFmpeg 的 Docker 镜像

    CentOS 6 的 FFmpeg 构建 此 Docker 映像为 CentOS 6 构建静态 ffmpeg 库(和二进制文件)。其主要目标是提供将被编译为minidlna的静态库。 rpmforge 上 ffmpeg 的 RPM 非常过时,安装其中几个 RPM 最终会增加很多...

    Centos6.*一键安装ffmpeg(含赖包)

    通过这个过程,用户可以方便地在旧版CentOS系统上部署和使用FFmpeg,进行多媒体处理任务。文件"install_ffmpeg"可能包含了整个安装过程的脚本,帮助自动化安装流程。在实际操作时,可依据这个文件的内容执行安装。

    ffmpeg_installer:用于在CentOS中编译,安装和卸载FFmpegLinux Shell脚本

    用于在CentOS中编译,安装和卸载FFmpegLinux Shell脚本(已在7.0上测试)。 该脚本基于。 安装 cd ~ sudo yum -y install git git clone https://github.com/jnaqsh/ffmpeg_installer/ cd ffmpeg_installer chmod +x...

    FFMPEG4.1 CentOS7 64位运行版

    用户可以通过命令行调用 ffmpeg 命令来执行各种操作,如 `ffmpeg -i input.mp4 output.mp4` 进行视频转换。 综上所述,"FFMPEG4.1 CentOS7 64位运行版"是一个针对 CentOS 7 系统优化的 FFmpeg 实现,适合用于多媒体...

    centos 6.5 安装 FFmpeg及转码插件(内附安装方法【自编】)

    FFmpeg 是一个强大的开源命令行工具,用于处理多媒体文件...通过以上步骤,你已经在 CentOS 6.5 上成功安装了 FFmpeg 及其转码插件。记得,FFmpeg 的使用非常灵活,你可以根据需求调整各种参数,以实现特定的转码效果。

    ffmpeg版本的rpm,适用环境ky10、centos

    2、执行安装 rpm -ivh ffmpeg-6.0-1.el7.centos.x86_64.rpm --nodeps --force 3、安装完成后,目录在/usr/local/ffmpeg /usr/local/ffmpeg/bin/ffmpeg [root@localhost /usr/local/ffmpeg/bin]# ./ffmpeg ffmpeg ...

    安卓termux下Centos的 ffmpeg.tar.gz

    总之,在Termux环境下安装CentOS和FFmpeg是一项挑战性的任务,但完成之后可以为Android设备带来强大的多媒体处理能力,适合对技术有深入研究和实践的用户。在整个过程中,不仅需要掌握Linux命令行操作、软件编译技巧...

    ffmpeg3.3.3解决花屏现象库文件

    该压缩包包含了FFmpeg 3.3.3 版本的库文件,这是一系列预编译的静态或动态库,可供开发人员在Windows环境下直接集成到他们的应用程序中,以便利用FFmpeg的功能进行音视频处理。这些库文件可能包括如libavcodec(负责...

    Centos6.5一键安装ffmpeg(含依赖包)

    安装完成后,用户可以通过`ffmpeg -version`命令检查FFmpeg是否安装成功并查看其版本信息。 总之,这个一键安装过程简化了在旧版CentOS上安装FFmpeg的复杂性,帮助用户快速获得一个能工作的多媒体处理环境。但请...

    centos 6.X升级ffmpeg包步骤详解

    众所周知FFmpeg是一个开源免费跨平台的视频和音频流方案,属于自由...之前公司已经安装了ffmpeg包,但因为时间较久,所以最近打算升级下,所以这篇文章就记录了在centos 6.X升级ffmpeg包的详细步骤,下面来一起看看吧。

    emcc编译的ffmpeg静态库-编译wasm可用

    Linux下、windows 下均可调用。 与平台无关。 编译方法: emcc wasm_ffmpeg/wasm_ffmpeg.c libavformat.a libavcodec.a libswresample.a libavutil.a -I "/include" -s EXPORTED_FUNCTIONS="['_malloc','_free','...

    ffmpeg_2_8_15_install.tar.gz

    通过以上步骤,你可以在 CentOS 7 环境下离线安装并使用 FFmpeg 2.8.15 版本。如果你需要处理多媒体文件,FFmpeg 是一个不可或缺的工具。记得根据你的具体需求和系统环境来调整安装过程和使用方法。

    Centos 6.5下PHP 5.3安装ffmpeg扩展的步骤详解

    在本篇文章中,我们将详细讲解如何在CentOS 6.5上为PHP 5.3安装ffmpeg扩展,这是一个用于处理多媒体文件的强大的开源工具。 首先,确保系统已经安装了必要的开发工具,包括GCC编译器和相关的库,因为它们是编译和...

    CentOS服务器中安装FFmpeg的完整步骤

    FFmpeg的使用不仅可以提高音视频处理的效率,还可以作为开发中的重要工具,例如在Java项目中调用FFmpeg处理媒体文件。希望这些内容对你在学习和工作中有所帮助,如果有任何疑问,欢迎继续探讨。

Global site tag (gtag.js) - Google Analytics