h.264 java_Java – h.264视频编码

您可以使用JCodec(

http://jcodec.org).

解码视频序列去:

int frameNumber = 10000;

FileChannelWrapper ch = null;

try {

ch = NIOUtils.readableFileChannel(new File(path to mp4));

FrameGrab frameGrab = new FrameGrab(ch);

frameGrab.seek(frameNumber);

Picture frame;

for (int i = 0; (frame = frameGrab.getNativeFrame()) != null && i < 200; i++) {

// Do something

}

} finally {

NIOUtils.closeQuietly(ch);

}

编码序列:

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(Picture bi) throws IOException {

if (toEncode == null) {

toEncode = Picture.create(bi.getWidth(), bi.getHeight(), ColorSpace.YUV420);

}

// Perform conversion ( RGB -> YUV )

transform.transform(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);

}

}

最后转换TO和FROM Android图像使用:

public static Picture fromBitmap(Bitmap src) {

Picture dst = Picture.create((int)src.getWidth(), (int)src.getHeight(), RGB);

fromBitmap(src, dst);

return dst;

}

public static void fromBitmap(Bitmap src, Picture dst) {

int[] dstData = dst.getPlaneData(0);

int[] packed = new int[src.getWidth() * src.getHeight()];

src.getPixels(packed, 0, src.getWidth(), 0, 0, src.getWidth(), src.getHeight());

for (int i = 0, srcOff = 0, dstOff = 0; i < src.getHeight(); i++) {

for (int j = 0; j < src.getWidth(); j++, srcOff++, dstOff += 3) {

int rgb = packed[srcOff];

dstData[dstOff] = (rgb >> 16) & 0xff;

dstData[dstOff + 1] = (rgb >> 8) & 0xff;

dstData[dstOff + 2] = rgb & 0xff;

}

}

}

public static Bitmap toBitmap(Picture src) {

Bitmap dst = Bitmap.create(pic.getWidth(), pic.getHeight(), ARGB_8888);

toBitmap(src, dst);

return dst;

}

public static void toBitmap(Picture src, Bitmap dst) {

int[] srcData = src.getPlaneData(0);

int[] packed = new int[src.getWidth() * src.getHeight()];

for (int i = 0, dstOff = 0, srcOff = 0; i < src.getHeight(); i++) {

for (int j = 0; j < src.getWidth(); j++, dstOff++, srcOff += 3) {

packed[dstOff] = (srcData[srcOff] << 16) | (srcData[srcOff + 1] << 8) | srcData[srcOff + 2];

}

}

dst.setPixels(packed, 0, src.getWidth(), 0, 0, src.getWidth(), src.getHeight());

}

最后解码你会得到YUV帧,为了将它转换为RGB帧去:

Transform transform = ColorUtil.getTransform(pic.getColor(), ColorSpace.RGB);

Picture rgb = Picture.create(pic.getWidth(), pic.getHeight(), ColorSpace.RGB);

transform.transform(pic, rgb);

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值