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

版权声明:本文博主原创文章,博客,未经同意不得转载。

CentOS 由 JavaCPP 转让 FFMPEG的更多相关文章

  1. <亲测>CentOS中yum安装ffmpeg

    CentOS中yum安装ffmpeg 1.升级系统 sudo yum install epel-release -y sudo yum update -y sudo shutdown -r now 2 ...

  2. CentOS下yum安装FFmpeg

    一.yum安装FFmpeg 1.    最偷懒的方式就是yum安装了,自动解决依赖.不过CentOS系统默认无FFmpeg源,企业版 Linux 附加软件包EPEL源也不包含,需要手动添加yum源配置 ...

  3. CentOS 6.5 安装 ffmpeg

    CentOS 6.5 安装 ffmpeg 满满的坑   http://download.videolan.org/pub/videolan/x264/snapshots/     安装ffmpeg   ...

  4. CentOS 6/7安装ffmpeg

    环境 CentOS 6/7 安装 导入GPG key rpm --import http://packages.atrpms.net/RPM-GPG-KEY.atrpms 安装ATRPMS Repo ...

  5. CentOS中yum安装ffmpeg

    1.升级系统 sudo yum install epel-release -y sudo yum update -y sudo shutdown -r now 2.安装Nux Dextop Yum 源 ...

  6. CentOS 6&7安装ffmpeg

    CentOS 6和7安装方法是不一样的,下面分别说明: 安装前都需要先安装epel扩展源 yum -y install epel-release CentOS 6比较简单,安装yum源之后直接安装即可 ...

  7. linux(centos)下安装ffmpeg

    [备忘]windows环境下20行php代码搞定音频裁剪 上次我的这篇文章将了windows下web中如何操作ffmpeg的文章,这里则记录下linux(centos)下的安装 首先:我花了中午大概1 ...

  8. How to install ffmpeg,mp4box,mplayer,mencoder,flvtool2,ffmpeg-php on centos

    1. Enable RPM Fusion yum repository The CentOS rpm packages of ffmpeg, mplayer, mencoder and MP4Box ...

  9. ffmpeg centos yum安装

    CentOS 6&7安装ffmpeg   CentOS 6和7安装方法是不一样的,下面分别说明: 安装前都需要先安装epel扩展源 yum -y install epel-release ce ...

随机推荐

  1. 使用wepy开发微信小程序商城第二篇:路由配置和页面结构

    使用wepy开发微信小程序商城 第二篇:路由配置和页面结构 前言: 最近公司在做一个微信小程序的项目,用的是类似于vue的wepy框架.我也借此机会学习和实践一下. 小程序官方文档:https://d ...

  2. Android系统开发(7)——标准I/O与文件锁

    一.常用函数 fopen: FILE *fopen(const char *filename, const char *mode); fread: size_t  fread(void *ptz, s ...

  3. 26、从零写UVC驱动之分析描述符

    指令:lsusb 可以查看usb设备的描述符信息,当然lsusb指令要带一些参数 一个usb设备有多个config配置+设备描述符,一个config有多个接口和association.config描述 ...

  4. “locktype”enum type 类型重定义问题的解决

    作者:朱金灿 来源:http://blog.csdn.net/clever101 使用ado来连接数据库,结果出现这样一些编译错误: 1>f:\c++pro\iocptser\debug\msa ...

  5. (四)RabbitMQ消息队列-服务详细配置与日常监控管理

    原文:(四)RabbitMQ消息队列-服务详细配置与日常监控管理 RabbitMQ服务管理 启动服务:rabbitmq-server -detached[ /usr/local/rabbitmq/sb ...

  6. Java中的${pageContext.request.contextPath}

    之前在drp项目中就接触了${pageContext.request.contextPath}.当时没有注意.这次在java版高校云平台ITOO4.0中再次与之相遇,真是无巧不成书啊.再次遇到.我再置 ...

  7. wdcp忘记mysql的root密码

    复制别人的 不管出于何种原因,应该有不少的朋友在自己的VPS/服务器上采用WDCP管理面板的时候有忘记MYSQL ROOT账户管理密码在寻找解决方法,甚至有忘记WDCP后台管理登录密码的.这些问题都比 ...

  8. 【35.43%】【hdu 4347】The Closest M Points

    Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others) Total Submissio ...

  9. 为什么说 C/C++ 不适合做 Web 开发?(成本高,容易出错,apache等工具分担了大部分工作)

    因为大家在讨论用C#.Java,做出来的项目的时候,用C++的人们还在讨论语言特性 每种语言都有特定适用范围,对应着某类问题.web开发的重头戏不是计算,而是与用户交互和发送sql语句,当然以脚本语言 ...

  10. thinkphp将excel导入到数据库中

    首先下载phpexcel插件 http://pan.baidu.com/s/1hq56dFm 我用的是thinkphp框架的3.1版本,下载好压缩包,框架中的extend中的vendor文件夹中新建一 ...