bytedeco/javacv

The transparency information in the generated APNG file has not been processed correctly

Open

#2335 aperta il 7 mag 2025

Vedi su GitHub
 (2 commenti) (0 reazioni) (0 assegnatari)Java (1583 fork)batch import
help wantedquestion

Metriche repository

Star
 (6985 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

After converting multiple PNG images into APNG using JavaCV, the transparency display of the generated APNG images is abnormal, and the objects in the images have black borders,Here is the code display:

    // 初始化FFmpeg录制器
    try (FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(
            outputPath,
            getFrameWidthHeight(frameFiles[0].getPath())[0],
            getFrameWidthHeight(frameFiles[0].getPath())[1])) {

        // 关键参数配置
        recorder.setFormat("apng");
        recorder.setFrameRate(frameRate);
        recorder.setVideoCodec(avcodec.AV_CODEC_ID_APNG);
        recorder.setPixelFormat(avutil.AV_PIX_FMT_RGBA); // 支持透明通道
        recorder.setOption("plays", String.valueOf(loopCount)); // 循环次数
        recorder.setVideoOption("pred", "1");       // 使用预测模式topleft(通常适合透明背景)
        recorder.setVideoOption("bit_depth", "8"); // 8位颜色深度(默认,但显式设置更安全)
        recorder.setVideoOption("colormap", "0"); // 禁用颜色映射(避免索引色导致透明问题)
        recorder.setVideoOption("lossless", "1");

        // 启动录制器
        recorder.start();

        // 逐帧写入
        for (File frameFile : sortFrameFiles) {
            InputStream frameStream = Files.newInputStream(frameFile.toPath());
            FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(frameStream);
            grabber.setFormat("png_pipe");
            grabber.setPixelFormat(avutil.AV_PIX_FMT_RGBA); // 设置输入像素格式
            grabber.setImageMode(FrameGrabber.ImageMode.RAW);             // 启用图像序列模式
            grabber.setFrameRate(frameRate);
            grabber.start();

            // 逐帧写入
            Frame frame;
            while ((frame = grabber.grab()) != null) {
                recorder.record(frame);
            }
        }

Image

Guida contributor