You can use JCodec ( http://jcodec.org ).
To decode a video sequence, go to:
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++) {
To encode a sequence:
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);
And finally, to convert TO and OT images:
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()); }
FINALLY, when decoding, you will get a YUV frame to convert it to an RGB frame:
Transform transform = ColorUtil.getTransform(pic.getColor(), ColorSpace.RGB); Picture rgb = Picture.create(pic.getWidth(), pic.getHeight(), ColorSpace.RGB); transform.transform(pic, rgb);
And make sure you download the JAR with all the folders: http://jcodec.org/downloads/jcodec-0.1.3-uberjar.jar