将图像序列转换为视频文件android java [英] Convert image sequence to video file android java

查看:951
本文介绍了将图像序列转换为视频文件android java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个老问题,我已经研究了很多,但似乎没有适当的解决方案。有一些方法可以这样做,我已经看过如下:

I know this is an old question and I have researched a lot through this but seems like there are no proper solutions. There are some ways to do so which I have already seen such as:


  • 使用FFmpeg(但它取决于本机代码所以不是我的选择)

  • Using FFmpeg (but it depends on the native code so not an option for me)

Xuggler API(与上述相同的理由)

Xuggler API (same reason as above)

我正在寻找可以将多个图像(可绘制或位图或其他任何东西)转换成可以在Android手机中播放的视频文件的解决方案甚至建议。我已经坚持了3天,并急于寻找解决方案。

Im looking for solutions or even suggestions which can convert multiple images (drawables or bitmaps or whatever it is) into to a video file which can be playback in an android phone. Im stuck at this for 3 days already and rushing to find a solution.

我也正在为我的问题寻找替代解决方案,而不是将图像转换为视频,是否有可能捕获并记录Android手机屏幕,实际上是分开的屏幕在我的应用程序? (请不要使用USB线插入计算机或类似的东西。我研究过的时候看到了很多这些,这不是我想要的)。

Im also looking for an alternative solution for my problem which is instead of convert images into video, is it possible to capture and record the android phone screen, actually apart of the screen in my application? ( please, not using the USB cable to plug into the computer or something like that. I have seen alot of those when I researched, that's not what I'm looking for).

任何帮助都会对我的英语感激和抱歉,这不是我原来的。

Any helps would be appreciate and sorry about my English, it is not my native one.

推荐答案

您可以使用名为JCodec的纯java开源库( http://jcodec.org ),下面的课程演示使用JCodec低级API将图像序列转换为MP4片段(H.264):

You can use a pure java open source library called JCodec ( http://jcodec.org ), the class below demonstrates converting as sequence of images into MP4 clip ( H.264 ) using JCodec low level API:

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 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<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
        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);
    }
}

然后你可以像这样使用它:

Then you can use it like this:

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("img%08d.png", i)));
        encoder.encodeImage(bi);
    }
    encoder.finish();
}

这篇关于将图像序列转换为视频文件android java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆