bytedeco/javacv

How to convert Frame to AVPacket,or directly convert the image to AVPacket?(javacv1.5.9)

Open

#2,143 创建于 2023年12月13日

在 GitHub 查看
 (0 评论) (0 反应) (0 负责人)Java (1,583 fork)batch import
enhancementhelp wantedquestion

仓库指标

Star
 (6,985 star)
PR 合并指标
 (30 天内没有已合并 PR)

描述

How to convert org.bytedeco.javacv.Frame to org.bytedeco.ffmpeg.avcodec.AVPacket,or directly convert the image to AVPacket? This is my current approach:

"Start -->Get the JavaCV frame

Get Javacv frame -->convert to AVFrame

Convert to AVFrame -->Encode to AvPacket

Encode as AvPacket -->End"

java code:

        recorder.setVideoCodec(AV_CODEC_ID_H264);
        recorder.setPixelFormat(AV_PIX_FMT_YUV420P);
        recorder.setFormat("rtsp");
        recorder.start();

        File imageFolder = new File(inputFolder);
        File[] imageFiles = imageFolder.listFiles();

        Java2DFrameConverter converter = new Java2DFrameConverter();
        if (imageFiles != null) {
            Frame frame = null;
            //Convert image to Frame in loop
            for (int i = startIdx; i < imageFiles.length; i++) {
                File imageFile = imageFiles[i];
                lastProcessedFile = imageFile.getAbsolutePath();
                
                BufferedImage read = ImageIO.read(imageFile);
                frame = converter.convert(read);


                //TODO convert Frame to AVPacket
                recorder.recordPacket(packet);

            }
         }

        

I don't know how to directly convert images to AVPackets.

AVCodec codec = avcodec.avcodec_find_encoder(avcodec.AV_CODEC_ID_H264);
AVCodecContext codecContext = avcodec.avcodec_alloc_context3(codec);

AVFrame avFrame = avutil.av_frame_alloc();
avFrame.width(frame.imageWidth);
avFrame.height(frame.imageHeight);
avFrame.format(frame.imageChannels);

//There is a problem with this. 
//AVFrame only has these two methods: public BytePointer data (int i) and public AVFrame data (int i, BytePointer setter)

avFrame.data(frame.image[0]);

avFrame.linesize(frame.imageStride);

AVPacket avPacket = avcodec.av_packet_alloc();
avcodec.avcodec_send_frame(codecContext, avFrame);
avcodec.avcodec_receive_packet(codecContext, avPacket);

For YUV420P, I need to fill in the data [0], data [1], and data [2] of the avframe separately based on my research. How to modify this code: avFrame. data (frame. image [0]);?

贡献者指南