bytedeco/javacv

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

Open

#2,335 创建于 2025年5月7日

在 GitHub 查看
 (2 评论) (0 反应) (0 负责人)Java (6,985 star) (1,583 fork)batch import
help wantedquestion

描述

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

贡献者指南

The transparency information in the generated APNG file has not been processed correctly · bytedeco/javacv#2335 | Good First Issue