Skip to content

Android:Example:Png2Mp4

assets/0.pngassets/1.png 두 개의 png이미지를 mp4로 변환하는 샘플 프로그램.

Source code

private void testVideo2(Context mContext) {
    try {
        int fps = 24;
        RgbToYuv420 transform = new RgbToYuv420(0, 0);

        // FileChannelWrapper ch = NIOUtils.writableFileChannel(SDPathToFile("", "out.mp4"));
        FileChannelWrapper ch = NIOUtils.writableFileChannel("/storage/sdcard0/DCIM/Camera/1.jpg");

        final MP4Muxer muxer = new MP4Muxer(ch, Brand.MP4);

        // Add a video track
        FramesMP4MuxerTrack outTrack = muxer.addTrackForCompressed(TrackType.VIDEO, (int) fps);

        // Create H.264 encoder
        H264Encoder encoder = new H264Encoder(); // not we could use a rate
                                                    // control in the
                                                    // constructor

        // Allocate a buffer that would hold an encoded frame
        ByteBuffer _out = ByteBuffer.allocate(640 * 480 * 6); // Not sur
                                                                // about RGB

        // Allocate storage for SPS/PPS, they need to be stored separately
        // in a special place of MP4 file
        ArrayList<ByteBuffer> spsList = new ArrayList<ByteBuffer>();
        ArrayList<ByteBuffer> ppsList = new ArrayList<ByteBuffer>();

        final int numberOfimage = 2;

        String path = "me";
        int num = 0;
        Picture rgb = null;

        Picture yuv = null;
        // for (File file : directory.listFiles()) {

        int[] packed = null;

        for (int i = 0; i < numberOfimage; i++) {
            System.out.println("Num" + num);
            Bitmap bitmap = null;
            try {

                AssetManager assetMgr = mContext.getAssets();
                // InputStream istr = assetMgr.open(path + "/" + String.format("%04d", i + 1) + ".png");
                InputStream istr = null;
                // istr = assetMgr.open("/storage/sdcard0/DCIM/Camera/" + i +".png");
                istr = assetMgr.open(i +".png");
                bitmap = BitmapFactory.decodeStream(istr);
                // bitmap=bitmap.copy(Bitmap.Config.ARGB_8888,true);
            } catch (Exception e) {
                e.printStackTrace();
                // System.out.println("Could not read image "+file);
            }
            if (rgb == null) {
                rgb = Picture.create((int) bitmap.getWidth(), (int) bitmap.getHeight(), ColorSpace.RGB);// YUV420);//RGB);
            }

            int[] dstData = rgb.getPlaneData(0);
            if (packed == null) {
                packed = new int[bitmap.getWidth() * bitmap.getHeight()];
            }

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

            for (int iu = 0, srcOff = 0, dstOff = 0; iu < bitmap.getHeight(); iu++) {
                for (int j = 0; j < bitmap.getWidth(); j++, srcOff++, dstOff += 3) {
                    int rgbo = packed[srcOff];

                    dstData[dstOff] = (rgbo >> 16) & 0xff;
                    dstData[dstOff + 1] = (rgbo >> 8) & 0xff;
                    dstData[dstOff + 2] = rgbo & 0xff;
                }
            }

            bitmap.recycle();

            if (yuv == null) {
                yuv = Picture.create(rgb.getWidth(), rgb.getHeight(), ColorSpace.YUV420);
            }
            transform.transform(rgb, yuv);

            // rgb = null;

            ByteBuffer result = encoder.encodeFrame(_out, yuv); // toEncode
            _out.clear();
            // yuv = null;

            // ByteBuffer result = encoder.encodeFrame(_out, yuv);
            // //toEncode

            // yuv = null;
            spsList.clear();
            ppsList.clear();

            H264Utils.encodeMOVPacket(result, spsList, ppsList);
            outTrack.addFrame(new MP4Packet(result, num, (int) fps, 1, num, true, null, num, 0));
            result = null;
            System.gc();

            num++;
        }

        yuv = null;
        packed = null;
        System.gc();
        outTrack.addSampleEntry(H264Utils.createMOVSampleEntry(spsList, ppsList));
        // Write MP4 header and finalize recording
        muxer.writeHeader();
        NIOUtils.closeQuietly(ch);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

See also

Favorite site