gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:22:03 25 4
gpt4 key购买 nike

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

过去有人用过这个库吗?我没有看到任何文档,甚至库附带的 javadoc 也没有包含任何使用信息。如果您看过示例或可以提供见解,请告诉我。

http://jcodec.org/

提前致谢

最佳答案

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 视频:

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] 使用此代码将交错的 YUV 4:2:0 字节数组转换为 JCodec 图片:

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;
}

关于java - jcodec - 有人看过这个库的文档吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10969423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com