CentOS transferred by Javacpp FFMPEG

Source: Internet
Author: User

1. Java and FFMPEG

FFMPEG It is a widely used media processing library, in the Java world, to deal with the weak video capabilities, so there is a very large demand for Java transfer FFMPEG.

There are many ways in which Java transfers C. Can be used in the most primitive jni way, also can JNA way. can also be a command line.

It is easier to use the command line method. There are only very big limitations, especially when it comes to video processing and analysis. For example, to take out a packet and then process it.


Here is the way to call the FFmpeg library with Javacpp. Instead of the command-line mode.

The source code for Javacpp is here: https://github.com/bytedeco/javacpp


Javacpp-based projects

1) JAVACV. is to do graphic image, face recognition, augmented reality AR and other open source algorithms, very good

Project homepage is: HTTPS://GITHUB.COM/BYTEDECO/JAVACV

2) Javaav encapsulates the Java interface of the FFmpeg

Project home: Https://github.com/hoary/JavaAV


2. Javacpp Presets


For ease of use, Javacpp has a presets project below, and the homepage is https://github.com/bytedeco/javacpp-presets. Compile some of the frequently used projects to make them easy to use.
The Integrated project contains:
? 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/

The platforms included are: Android-arm, android-x86, Linux-x86, linux-x86_64, macosx-x86_64, Windows-x86, windows-x86_64,

But note: The linux-x86_64 is based on the Fedora platform and cannot be used under CentOS.


3. Compiling under CentOS
1) Installing the JDK
2) Install Apache-maven 2/3

3) Install Gcc,gcc+,gcc-c++,yasm

Yum-y Install yasm gcc+ gcc-c++


4) Get the source code

git clone http://github.com/bytedeco/javacpp-presets
5) Compiling FFmpeg


CD javacpp-presets/
./cppbuild.sh-platform linux-x86_64 Install FFmpeg


6) compiling JNI and related jars


MVN Install--projects FFmpeg


7) After the compilation is complete. The associated. So is generated on the FFmpeg lib. Generate the associated jar under Javacpp-presets/targets


4. Test
1) Copy the relevant. So to the/lib64 folder, or specify it to the. So folder by-djava.library.path
2) Copy an instance for testing.

public class Tutorial01 {static void Saveframe (Avframe pframe, int width, int height, int iFrame) throws IOException {//Op  En fileoutputstream stream = new FileOutputStream ("frame" + IFrame + ". ppm");//Write Headerstream.write ("p6\n" + width + "+ height +" \n255\n "). GetBytes ());//Write pixel databytepointer 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 filestream.close ();} public static void Main (string[] args) throws IOException {Avformatcontext pformatctx = new Avformatcontext (null); int I, V Ideostream; 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 Codecsav_register_all ();//Open Video Fileif (Avformat_open_input (Pformatctx, args[0], NULL, n ull)! = 0) {system.exit ( -1);//couldn ' t open file}//Retrieve stream informationif (Avformat_find_stream_info (pformatctx  , (pointerpointer) NULL) < 0) {system.exit ( -1);//couldn ' t find stream information}//Dump information about file onto  Standard Errorav_dump_format (pformatctx, 0, Args[0], 0);//Find the first video Streamvideostream = -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 Streampcodecctx = Pformatctx.streams (videostream). Codec ();//Find The decoder for the video Streampcodec = Avcodec_find_d Ecoder (pcodecctx.codec_id ()); if (Pcodec = = null) {System.err.println ("Unsupported codec!"); System.exit (-1); Codec not found}//Open codecif (Avcodec_open2 (Pcodecctx, Pcodec, optionsdict) < 0) {system.exit ( -1);//Could not O  Pen codec}//Allocate Video framepframe = Av_frame_alloc ();//Allocate an avframe Structurepframergb = Av_frame_alloc (); if (Pframergb = = null) {System.exit (-1);} Determine required buffer size and allocate buffernumbytes = 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 this pframergb is a n Avframe, but Avframe is a superset//of Avpictureavpicture_fill (new Avpicture (PFRAMERGB), buffer, av_pix_fmt_rgb24,pcod Ecctx.width (), Pcodecctx.height ());//Read frames and save first five frames to Diski = 0;while (Av_read_frame (PFOrmatctx, packet) >= 0) {//Is this a packet from the video stream?if (packet.stream_index () = = Videostream) {//Decode Video Frameavcodec_decode_video2 (Pcodecctx, Pframe, framefinished, packet);/Did we get a video frame?if (framefinished[ 0]! = 0) {//Convert the image from its native format to Rgbsws_scale (Sws_ctx, Pframe.data (), Pframe.linesize (), 0,PCODECC Tx.height (), Pframergb.data (), pframergb.linesize ());//Save the frame to Diskif (++i <= 5) {saveframe (Pframergb, Pcode Cctx.width (), Pcodecctx.height (), i);}}} Free the packet that is allocated by Av_read_frameav_free_packet (packet); Free the RGB imageav_free (buffer); Av_free (PFRAMERGB);//Free the YUV frameav_free (pframe);//Close the CODECAVCODEC_CL OSE (PCODECCTX);//Close the video fileavformat_close_input (PFORMATCTX); System.exit (0);}}

4) Execute the example, get the output result:

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], fps, TBR, 90k TBN, TBC
Stream #0:1[0x101]: AUDIO:AAC ([15][0][0][0]/0x000f), 44100 Hz, stereo, FLTP, kb/s



Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

CentOS transferred by Javacpp FFMPEG

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.