org.bytedeco.javacpp.avutil.AVFrame Java Examples
The following examples show how to use
org.bytedeco.javacpp.avutil.AVFrame.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FFmpegVideoImageGrabber.java From easyCV with Apache License 2.0 | 6 votes |
@Override protected ByteBuffer saveFrame(AVFrame pFrame, int width, int height) { BytePointer data = pFrame.data(0); int size = width * height * 3; ByteBuffer buf = data.position(0).limit(size).asBuffer(); if(viwer==null) { viwer=new ImageViewer(width,height, BufferedImage.TYPE_3BYTE_BGR); }else { if(index==0) { index++; viwer.setSize(width, height); } } viwer.show(buf,width,height); return buf; }
Example #2
Source File: FFmpegFrameRecorderPlus.java From easyCV with Apache License 2.0 | 5 votes |
boolean record(AVFrame frame) throws Exception { int ret; av_init_packet(audio_pkt); audio_pkt.data(audio_outbuf); audio_pkt.size(audio_outbuf_size); if ((ret = avcodec_encode_audio2(audio_c, audio_pkt, frame, got_audio_packet)) < 0) { throw new Exception("avcodec_encode_audio2() error " + ret + ": Could not encode audio packet."); } if (frame != null) { frame.pts(frame.pts() + frame.nb_samples()); // magic required by libvorbis and webm } if (got_audio_packet[0] != 0) { if (audio_pkt.pts() != AV_NOPTS_VALUE) { audio_pkt.pts(av_rescale_q(audio_pkt.pts(), audio_c.time_base(), audio_st.time_base())); } if (audio_pkt.dts() != AV_NOPTS_VALUE) { audio_pkt.dts(av_rescale_q(audio_pkt.dts(), audio_c.time_base(), audio_st.time_base())); } audio_pkt.flags(audio_pkt.flags() | AV_PKT_FLAG_KEY); audio_pkt.stream_index(audio_st.index()); } else { return false; } /* write the compressed frame in the media file */ writePacket(AVMEDIA_TYPE_AUDIO, audio_pkt); return true; }
Example #3
Source File: FFmpeg4VideoImageGrabber.java From easyCV with Apache License 2.0 | 5 votes |
@Override public byte[] saveFrame(AVFrame frameRGB, int width, int height) { BytePointer data = frameRGB.data(0); int size = width * height * 3; //复制虚拟机外内存数据到java虚拟机中,因为这个方法之后会清理内存 byte[] bytes=new byte[size]; data.position(0).limit(size).get(bytes,0,size); return bytes; }
Example #4
Source File: FFmpegVideoImageGrabber.java From easyCV with Apache License 2.0 | 5 votes |
@Override protected ByteBuffer saveFrame(AVFrame pFrame, int width, int height){ BytePointer data = pFrame.data(0); int size = width * height * 3; ByteBuffer buf = data.position(0).limit(size).asByteBuffer(); return buf; }
Example #5
Source File: TestFFmpeg.java From easyCV with Apache License 2.0 | 5 votes |
/** * 保存帧 * @param pFrame * @param width * @param height * @return * @throws IOException */ private static ByteBuffer saveFrame(AVFrame pFrame, int width, int height) throws IOException { // byte[] arr=new byte[width*height*3]; // BytePointer data = pFrame.data(0).get(arr); // JavaImgConverter.viewBGR(width, height,ByteBuffer.wrap(arr)); BytePointer data=pFrame.data(0); int size=width*height*3; ByteBuffer buf=data.position(0).limit(size).asBuffer(); return buf; }
Example #6
Source File: GrabberTmplate.java From easyCV with Apache License 2.0 | 4 votes |
/** * 抓取视频帧(默认跳过音频帧和空帧) * @param url * @param fmt - 像素格式,比如AV_PIX_FMT_BGR24 * @return * @throws IOException */ public ByteBuffer grabVideoFrame(String url,int fmt) throws IOException { // Open video file AVFormatContext pFormatCtx=openInput(url); // Retrieve stream information pFormatCtx=findStreamInfo(pFormatCtx); // Dump information about file onto standard error //av_dump_format(pFormatCtx, 0, url, 0); //Find a video stream int videoStream=findVideoStreamIndex(pFormatCtx); AVCodecContext pCodecCtx =findVideoStream(pFormatCtx,videoStream); // Find the decoder for the video stream pCodecCtx= findAndOpenCodec(pCodecCtx); // Allocate video frame AVFrame pFrame = av_frame_alloc(); //Allocate an AVFrame structure AVFrame pFrameRGB = av_frame_alloc(); width = pCodecCtx.width(); height = pCodecCtx.height(); pFrameRGB.width(width); pFrameRGB.height(height); pFrameRGB.format(fmt); // Determine required buffer size and allocate buffer int numBytes = avpicture_get_size(fmt, width, height); SwsContext sws_ctx = sws_getContext(width, height, pCodecCtx.pix_fmt(), width, height,fmt, SWS_BILINEAR, null, null, (DoublePointer) null); BytePointer buffer = new BytePointer(av_malloc(numBytes)); // 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, fmt, width, height); AVPacket packet = new AVPacket(); int[] frameFinished = new int[1]; try { // Read frames and save first five frames to disk 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 != null&&frameFinished[0] > 0) { // Convert the image from its native format to BGR sws_scale(sws_ctx, pFrame.data(), pFrame.linesize(), 0, height, pFrameRGB.data(),pFrameRGB.linesize()); //Convert BGR to ByteBuffer saveFrame(pFrameRGB, width, height); } } // Free the packet that was allocated by av_read_frame av_free_packet(packet); } return null; }finally { //Don't free buffer // av_free(buffer); av_free(pFrameRGB);// Free the RGB image av_free(pFrame);// Free the YUV frame sws_freeContext(sws_ctx);//Free SwsContext avcodec_close(pCodecCtx);// Close the codec avformat_close_input(pFormatCtx);// Close the video file } }
Example #7
Source File: FFmpegRecorder.java From easyCV with Apache License 2.0 | 4 votes |
/** * 抓取视频帧(默认跳过音频帧和空帧) * @param url * @param fmt - 像素格式,比如AV_PIX_FMT_BGR24 * @return * @throws IOException */ public ByteBuffer grabVideoFrame(String url,int fmt) throws IOException { // Open video file AVFormatContext pFormatCtx=openInput(url); // if(url.indexOf("rtmp")>=0) { //解决rtmp检索时间过长问题 //限制最大读取缓存 pFormatCtx.probesize(PROBESIZE);//设置500k能保证高清视频也能读取到关键帧 //限制avformat_find_stream_info最大持续时长,设置成3秒 pFormatCtx.max_analyze_duration(MAX_ANALYZE_DURATION); // } // Retrieve stream information pFormatCtx=findStreamInfo(pFormatCtx); // Dump information about file onto standard error //av_dump_format(pFormatCtx, 0, url, 0); //Find a video stream int videoStream=findVideoStreamIndex(pFormatCtx); AVCodecContext pCodecCtx =findVideoStream(pFormatCtx,videoStream); // Find the decoder for the video stream pCodecCtx= findAndOpenCodec(pCodecCtx); // Allocate video frame AVFrame pFrame = av_frame_alloc(); AVPacket packet = new AVPacket(); int[] frameFinished = new int[1]; // Read frames and save first five frames to disk 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 != null&&frameFinished[0] != 0) { } } // Free the packet that was allocated by av_read_frame av_free_packet(packet); } //ge av_free(pFrame);// Free the YUV frame avcodec_close(pCodecCtx);// Close the codec avformat_close_input(pFormatCtx);// Close the video file return null; }
Example #8
Source File: TestFFmpeg.java From easyCV with Apache License 2.0 | 4 votes |
public static BufferedImage frame2BufferImage(AVFrame pFrame, int width, int height) { BytePointer data=pFrame.data(0); int size=width*height*3; ByteBuffer buf=data.position(0).limit(size).asBuffer(); return JavaImgConverter.BGR2BufferedImage(buf, width, height); }
Example #9
Source File: TestFFmpeg.java From easyCV with Apache License 2.0 | 4 votes |
/** * 把YUVJ420P数据编码保存成jpg图片 * @param pFrame -YUVJ420P数据 * @param index -序号 * @return */ private static int saveImg(AVFrame pFrame, int index,String out_file) { int width= pFrame.width(), height= pFrame.height(); // 分配AVFormatContext对象 AVFormatContext pFormatCtx = avformat_alloc_context(); // 设置输出文件格式 pFormatCtx.oformat(av_guess_format("PNG", null, null)); if (pFormatCtx.oformat() == null) { return -1; } // 创建并初始化一个和该url相关的AVIOContext AVIOContext pb = new AVIOContext(); if (avio_open(pb, out_file, AVIO_FLAG_READ_WRITE) < 0) { System.err.println("Couldn't open output file."); return -1; } pFormatCtx.pb(pb); // 构建一个新stream AVCodec codec = null; AVStream pAVStream = avformat_new_stream(pFormatCtx, codec); if (pAVStream == null) { return -1; } // 设置该stream的信息 AVCodecContext pCodecCtx = pAVStream.codec(); pCodecCtx.codec_id(pFormatCtx.oformat().video_codec()); pCodecCtx.codec_type(AVMEDIA_TYPE_VIDEO); pCodecCtx.pix_fmt(pFrame.format()); pCodecCtx.width(width); pCodecCtx.height(height); pCodecCtx.time_base().num(1); pCodecCtx.time_base().den(25); // Begin Output some information av_dump_format(pFormatCtx, 0, out_file, 1); // End Output some information // 查找解码器 AVCodec pCodec = avcodec_find_encoder(pCodecCtx.codec_id()); if (pCodec == null) { System.err.println("Codec not found."); return -1; } // 设置pCodecCtx的解码器为pCodec if (avcodec_open2(pCodecCtx, pCodec, (PointerPointer) null) < 0) { System.err.println("Could not open codec."); return -1; } // Write Header avformat_write_header(pFormatCtx, (PointerPointer) null); int y_size = width * height; // 给AVPacket分配足够大的空间 AVPacket pkt = new AVPacket(); av_new_packet(pkt, y_size * 3); // int[] got_picture_arr = { 0 }; // IntPointer got_picture = new IntPointer(got_picture_arr); int ret = avcodec_encode_video2(pCodecCtx, pkt, pFrame, got_picture_arr); if (ret < 0) { System.err.println("Encode Error.\n"); return -1; } if (pkt != null && !pkt.isNull()) { // pkt.stream_index = pAVStream->index; ret = av_write_frame(pFormatCtx, pkt); } // Write Trailer if (av_write_trailer(pFormatCtx) >= 0) { System.err.println("Encode Successful."); } av_free_packet(pkt); if (pAVStream != null) { avcodec_close(pAVStream.codec()); } if (pFormatCtx != null) { avio_close(pFormatCtx.pb()); avformat_free_context(pFormatCtx); } return 0; }
Example #10
Source File: FFmpegVideoImageGrabber.java From easyCV with Apache License 2.0 | 3 votes |
/** * bgr图像帧转BufferedImage图片 * * @param pFrame * -bgr图像帧 * @param width * -宽度 * @param height * -高度 * @return */ protected BufferedImage frame2BufferImage(AVFrame pFrame, int width, int height) { BytePointer data = pFrame.data(0); int size = width * height * 3; ByteBuffer buf = data.position(0).limit(size).asBuffer(); return JavaImgConverter.BGR2BufferedImage(buf, width, height); }
Example #11
Source File: GrabberTmplate.java From easyCV with Apache License 2.0 | 2 votes |
/** * BGR图像帧转字节缓冲区(BGR结构) * * @param pFrame * -bgr图像帧 * @param width * -宽度 * @param height * -高度 * @return * @throws IOException */ abstract ByteBuffer saveFrame(AVFrame pFrameRGB, int width, int height);
Example #12
Source File: Grabber.java From easyCV with Apache License 2.0 | 2 votes |
/** * 保存RGB像素帧 * @param pFrameRGB -默认rgb像素 * @param width * @param height * @return */ byte[] saveFrame(AVFrame pFrameRGB, int width, int height);