jcodec_java-jcodec-有人看过该库的文档吗?

本文介绍了如何使用JCodec库进行H.264编码,包括结合JNA与x264或ffmpeg进行编码,并提供了一个自定义的SequenceEncoder类示例,用于将图像序列转换为MP4中的H.264视频。从0.1.0版本开始,JCodec支持H.264编码,并展示了如何将交错的YUV 4:2:0字节数组转换为JCodec图片。
摘要由CSDN通过智能技术生成

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

private ArrayList 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();

ppsList = new ArrayList();

}

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值