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) 拷贝一个实例用来测试,  

   

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. public class Tutorial01 {  
  2.   
  3.     static void SaveFrame(AVFrame pFrame, int width, int height, int iFrame)  
  4.             throws IOException {  
  5.         // Open file  
  6.         OutputStream stream = new FileOutputStream("frame" + iFrame + ".ppm");  
  7.   
  8.         // Write header  
  9.         stream.write(("P6\n" + width + " " + height + "\n255\n").getBytes());  
  10.   
  11.         // Write pixel data  
  12.         BytePointer data = pFrame.data(0);  
  13.         byte[] bytes = new byte[width * 3];  
  14.         int l = pFrame.linesize(0);  
  15.         for (int y = 0; y < height; y++) {  
  16.             data.position(y * l).get(bytes);  
  17.             stream.write(bytes);  
  18.         }  
  19.   
  20.         // Close file  
  21.         stream.close();  
  22.     }  
  23.   
  24.     public static void main(String[] args) throws IOException {  
  25.         AVFormatContext pFormatCtx = new AVFormatContext(null);  
  26.         int i, videoStream;  
  27.         AVCodecContext pCodecCtx = null;  
  28.         AVCodec pCodec = null;  
  29.         AVFrame pFrame = null;  
  30.         AVFrame pFrameRGB = null;  
  31.         AVPacket packet = new AVPacket();  
  32.         int[] frameFinished = new int[1];  
  33.         int numBytes;  
  34.         BytePointer buffer = null;  
  35.   
  36.         AVDictionary optionsDict = null;  
  37.         SwsContext sws_ctx = null;  
  38.   
  39.         if (args.length < 1) {  
  40.             args = new String[] { "/root/test.ts" };  
  41.             // System.out.println("Please provide a movie file");  
  42.             // System.exit(-1);  
  43.         }  
  44.         // Register all formats and codecs  
  45.         av_register_all();  
  46.   
  47.         // Open video file  
  48.         if (avformat_open_input(pFormatCtx, args[0], nullnull) != 0) {  
  49.             System.exit(-1); // Couldn't open file  
  50.         }  
  51.   
  52.         // Retrieve stream information  
  53.         if (avformat_find_stream_info(pFormatCtx, (PointerPointer) null) < 0) {  
  54.             System.exit(-1); // Couldn't find stream information  
  55.         }  
  56.   
  57.         // Dump information about file onto standard error  
  58.         av_dump_format(pFormatCtx, 0, args[0], 0);  
  59.   
  60.         // Find the first video stream  
  61.         videoStream = -1;  
  62.         for (i = 0; i < pFormatCtx.nb_streams(); i++) {  
  63.             if (pFormatCtx.streams(i).codec().codec_type() == AVMEDIA_TYPE_VIDEO) {  
  64.                 videoStream = i;  
  65.                 break;  
  66.             }  
  67.         }  
  68.         if (videoStream == -1) {  
  69.             System.exit(-1); // Didn't find a video stream  
  70.         }  
  71.   
  72.         // Get a pointer to the codec context for the video stream  
  73.         pCodecCtx = pFormatCtx.streams(videoStream).codec();  
  74.   
  75.         // Find the decoder for the video stream  
  76.         pCodec = avcodec_find_decoder(pCodecCtx.codec_id());  
  77.         if (pCodec == null) {  
  78.             System.err.println("Unsupported codec!");  
  79.             System.exit(-1); // Codec not found  
  80.         }  
  81.         // Open codec  
  82.         if (avcodec_open2(pCodecCtx, pCodec, optionsDict) < 0) {  
  83.             System.exit(-1); // Could not open codec  
  84.         }  
  85.   
  86.         // Allocate video frame  
  87.         pFrame = av_frame_alloc();  
  88.   
  89.         // Allocate an AVFrame structure  
  90.         pFrameRGB = av_frame_alloc();  
  91.         if (pFrameRGB == null) {  
  92.             System.exit(-1);  
  93.         }  
  94.   
  95.         // Determine required buffer size and allocate buffer  
  96.         numBytes = avpicture_get_size(AV_PIX_FMT_RGB24, pCodecCtx.width(),  
  97.                 pCodecCtx.height());  
  98.         buffer = new BytePointer(av_malloc(numBytes));  
  99.   
  100.         sws_ctx = sws_getContext(pCodecCtx.width(), pCodecCtx.height(),  
  101.                 pCodecCtx.pix_fmt(), pCodecCtx.width(), pCodecCtx.height(),  
  102.                 AV_PIX_FMT_RGB24, SWS_BILINEAR, nullnull,  
  103.                 (DoublePointer) null);  
  104.   
  105.         // Assign appropriate parts of buffer to image planes in pFrameRGB  
  106.         // Note that pFrameRGB is an AVFrame, but AVFrame is a superset  
  107.         // of AVPicture  
  108.         avpicture_fill(new AVPicture(pFrameRGB), buffer, AV_PIX_FMT_RGB24,  
  109.                 pCodecCtx.width(), pCodecCtx.height());  
  110.   
  111.         // Read frames and save first five frames to disk  
  112.         i = 0;  
  113.         while (av_read_frame(pFormatCtx, packet) >= 0) {  
  114.             // Is this a packet from the video stream?  
  115.             if (packet.stream_index() == videoStream) {  
  116.                 // Decode video frame  
  117.                 avcodec_decode_video2(pCodecCtx, pFrame, frameFinished, packet);  
  118.   
  119.                 // Did we get a video frame?  
  120.                 if (frameFinished[0] != 0) {  
  121.                     // Convert the image from its native format to RGB  
  122.                     sws_scale(sws_ctx, pFrame.data(), pFrame.linesize(), 0,  
  123.                             pCodecCtx.height(), pFrameRGB.data(),  
  124.                             pFrameRGB.linesize());  
  125.   
  126.                     // Save the frame to disk  
  127.                     if (++i <= 5) {  
  128.                         SaveFrame(pFrameRGB, pCodecCtx.width(),  
  129.                                 pCodecCtx.height(), i);  
  130.                     }  
  131.                 }  
  132.             }  
  133.   
  134.             // Free the packet that was allocated by av_read_frame  
  135.             av_free_packet(packet);  
  136.         }  
  137.   
  138.         // Free the RGB image  
  139.         av_free(buffer);  
  140.         av_free(pFrameRGB);  
  141.   
  142.         // Free the YUV frame  
  143.         av_free(pFrame);  
  144.   
  145.         // Close the codec  
  146.         avcodec_close(pCodecCtx);  
  147.   
  148.         // Close the video file  
  149.         avformat_close_input(pFormatCtx);  
  150.   
  151.         System.exit(0);  
  152.     }  
  153. }  

 

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

猜你喜欢

转载自chriszeng87.iteye.com/blog/2151850