javacv icon indicating copy to clipboard operation
javacv copied to clipboard

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

Open chenhao523 opened this issue 8 months ago • 0 comments

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]);?

chenhao523 avatar Dec 13 '23 03:12 chenhao523