java - jcodec - has anyone seen documentation on this library? -
i'm trying use jcodec encode bufferedimages h264 movie. haven't been able find example code anywhere.
has used library in past? haven't seen documentation, javadoc comes library contains no usage information. if have seen examples, or can provide insight please let me know.
thanks in advance
jcodec not support encoding of h.264. can use jna x264 library or ffmpeg. if choose route thread may going: how 1 encode series of images h264 using x264 c api? .
[update] of version 0.1.0 jcodec supports h.264 encoding, here's simple class can use turn sequence of images h.264 video in mp4 container:
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 convert between rgb , yuv transform = new rgbtoyuv420(0, 0); // muxer store encoded frames muxer = new mp4muxer(ch, brand.mp4); // add video track muxer outtrack = muxer.addtrackforcompressed(tracktype.video, 25); // allocate buffer big enough hold output frames _out = bytebuffer.allocate(1920 * 1080 * 6); // create instance of encoder encoder = new h264encoder(); // encoder data ( sps, pps ) stored in 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 (int = 0; < 3; i++) arrays.fill(toencode.getdata()[i], 0); transform.transform(awtutil.frombufferedimage(bi), toencode); // encode image h.264 frame, result stored in '_out' buffer _out.clear(); bytebuffer result = encoder.encodeframe(_out, toencode); // based on frame above form correct mp4 packet spslist.clear(); ppslist.clear(); h264utils.encodemovpacket(result, spslist, ppslist); // add packet 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 special storage in mp4 outtrack.addsampleentry(h264utils.createmovsampleentry(spslist, ppslist)); // write mp4 header , finalize recording muxer.writeheader(); nioutils.closequietly(ch); } public static void main(string[] args) throws ioexception { sequenceencoder encoder = new sequenceencoder(new file("video.mp4")); (int = 1; < 100; i++) { bufferedimage bi = imageio.read(new file(string.format("folder/img%08d.png", i))); encoder.encodeimage(bi); } encoder.finish(); } }
[update 1] use code convert interleaved yuv 4:2:0 byte array jcodec picture:
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 = 0, j0 = 0, j1 = 0, j2 = 0; < input.length; += 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; }
Comments
Post a Comment