首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jcodec -有人看过这个库的文档吗?

jcodec -有人看过这个库的文档吗?
EN

Stack Overflow用户
提问于 2012-06-10 22:18:29
回答 1查看 6K关注 0票数 3

我正在尝试使用jcodec将缓冲区图像编码为h264电影。只是我在任何地方都找不到示例代码。

过去有没有人用过这个库?我没有看到任何文档,甚至连这个库附带的javadoc都没有包含任何用法信息。如果你已经看到了例子,或者可以提供见解,请让我知道。

http://jcodec.org/

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-03 01:22:29

JCodec不支持H.264编码。但是,您可以将JNA与x264库或ffmpeg一起使用。如果你选择这条路,这个线程可能会帮助你开始:How does one encode a series of images into H264 using the x264 C API?

更新从0.1.0版本开始JCodec支持H.264编码,下面是一个简单的类,您可以使用它在MP4容器中将您的图像序列转换为H.264视频:

代码语言:javascript
复制
public class SequenceEncoder {
    private SeekableByteChannel ch;
    private Picture toEncode;
    private RgbToYuv420 transform;
    private H264Encoder encoder;
    private ArrayList<ByteBuffer> spsList;
    private ArrayList<ByteBuffer> ppsList;
    private CompressedTrack outTrack;
    private ByteBuffer _out;
    private int frameNo;
    private MP4Muxer muxer;

    public SequenceEncoder(File out) throws IOException {
        this.ch = NIOUtils.writableFileChannel(out);

        // Transform to convert between RGB and YUV
        transform = new RgbToYuv420(0, 0);

        // Muxer that will store the encoded frames
        muxer = new MP4Muxer(ch, Brand.MP4);

        // Add video track to muxer
        outTrack = muxer.addTrackForCompressed(TrackType.VIDEO, 25);

        // Allocate a buffer big enough to hold output frames
        _out = ByteBuffer.allocate(1920 * 1080 * 6);

        // Create an instance of encoder
        encoder = new H264Encoder();

        // Encoder extra data ( SPS, PPS ) to be stored in a special place of
        // MP4
        spsList = new ArrayList<ByteBuffer>();
        ppsList = new ArrayList<ByteBuffer>();

    }

    public void encodeImage(BufferedImage bi) throws IOException {
        if (toEncode == null) {
            toEncode = Picture.create(bi.getWidth(), bi.getHeight(), ColorSpace.YUV420);
        }

        // Perform conversion
        for (int i = 0; i < 3; i++)
            Arrays.fill(toEncode.getData()[i], 0);
        transform.transform(AWTUtil.fromBufferedImage(bi), toEncode);

        // Encode image into H.264 frame, the result is stored in '_out' buffer
        _out.clear();
        ByteBuffer result = encoder.encodeFrame(_out, toEncode);

        // Based on the frame above form correct MP4 packet
        spsList.clear();
        ppsList.clear();
        H264Utils.encodeMOVPacket(result, spsList, ppsList);

        // Add packet to video track
        outTrack.addFrame(new MP4Packet(result, frameNo, 25, 1, frameNo, true, null, frameNo, 0));

        frameNo++;
    }

    public void finish() throws IOException {
        // Push saved SPS/PPS to a special storage in MP4
        outTrack.addSampleEntry(H264Utils.createMOVSampleEntry(spsList, ppsList));

        // Write MP4 header and finalize recording
        muxer.writeHeader();
        NIOUtils.closeQuietly(ch);
    }

    public static void main(String[] args) throws IOException {
        SequenceEncoder encoder = new SequenceEncoder(new File("video.mp4"));
        for (int i = 1; i < 100; i++) {
            BufferedImage bi = ImageIO.read(new File(String.format("folder/img%08d.png", i)));
            encoder.encodeImage(bi);
        }
        encoder.finish();
    }
}

UPDATE 1使用此代码将交错YUV4:2:0字节数组转换为JCodec图片:

代码语言:javascript
复制
byte[] input = ...
Picture output = Picture.create(width, height, ColorSpace.YUV420);
int[] d0 = output.getData()[0], d1 = output.getData()[1], d2 = output.getData()[2];

for(int i = 0, j0 = 0, j1 = 0, j2 = 0; i < input.length; i += 6, j0 += 4, ++j1 , ++j2) {
    d0[j0    ] = input[i    ] & 0xff;
    d0[j0 + 1] = input[i + 1] & 0xff;
    d0[j0 + 2] = input[i + 2] & 0xff;
    d0[j0 + 3] = input[i + 3] & 0xff;

    d1[j1    ] = input[i + 4] & 0xff;
    d2[j2    ] = input[i + 5] & 0xff;
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10969423

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档