从android中的屏幕抓取创建视频

时间:2012-12-28 09:29:14

标签: android video

我想在视频中记录用户互动,然后人们可以上传到他们的社交媒体网站。

例如,Talking Tom Cat安卓应用程序有一个小摄像机图标。用户可以按下摄像机图标,然后与应用程序交互,按图标停止录制,然后处理/转换视频以备上载。

我想我可以使用setDrawingCacheEnabled(true)来保存图片,但不知道如何添加音频或制作视频。

更新:进一步阅读后,我想我需要使用NDK和ffmpeg。我不想这样做,但是,如果没有其他选择,有人知道怎么做吗?

有人知道如何在Android中执行此操作吗?

相关链接......

Android Screen capturing or make video from images

how to record screen video as like Talking Tomcat application does in iphone?

1 个答案:

答案 0 :(得分:8)

MediaCodec API与CONFIGURE_FLAG_ENCODE一起使用,将其设置为编码器。不需要ffmpeg:)

您已经找到了如何在您链接的其他问题中抓取屏幕,现在您只需要将每个捕获的帧提供给MediaCodec,设置相应的格式标记,时间戳等。

编辑:此示例代码很难找到,但是here it is,请向MartinStorsjö提示。快速API演练:

MediaFormat inputFormat = MediaFormat.createVideoFormat("video/avc", width, height);
inputFormat.setInteger(MediaFormat.KEY_BIT_RATE, bitRate);
inputFormat.setInteger(MediaFormat.KEY_FRAME_RATE, frameRate);
inputFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, colorFormat);
inputFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 75);
inputFormat.setInteger("stride", stride);
inputFormat.setInteger("slice-height", sliceHeight);

encoder = MediaCodec.createByCodecName("OMX.TI.DUCATI1.VIDEO.H264E"); // need to find name in media codec list, it is chipset-specific

encoder.configure(inputFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
encoder.start();
encoderInputBuffers = encoder.getInputBuffers();
encoderOutputBuffers = encoder.getOutputBuffers();

byte[] inputFrame = new byte[frameSize];

while ( ... have data ... ) {
    int inputBufIndex = encoder.dequeueInputBuffer(timeout);

    if (inputBufIndex >= 0) {
        ByteBuffer inputBuf = encoderInputBuffers[inputBufIndex];
        inputBuf.clear();

        // HERE: fill in input frame in correct color format, taking strides into account
        // This is an example for I420
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                inputFrame[ i * stride + j ] = ...; // Y[i][j]
                inputFrame[ i * stride/2 + j/2 + stride * sliceHeight ] = ...; // U[i][j]
                inputFrame[ i * stride/2 + j/2 + stride * sliceHeight * 5/4 ] = ...; // V[i][j]
            }
        }

        inputBuf.put(inputFrame);

        encoder.queueInputBuffer(
            inputBufIndex,
            0 /* offset */,
            sampleSize,
            presentationTimeUs,
            0);
    }

    int outputBufIndex = encoder.dequeueOutputBuffer(info, timeout);

    if (outputBufIndex >= 0) {
        ByteBuffer outputBuf = encoderOutputBuffers[outputBufIndex];

        // HERE: read get the encoded data

        encoder.releaseOutputBuffer(
            outputBufIndex, 
            false);
    }
    else {
        // Handle change of buffers, format, etc
    }
}

还有一些open issues

编辑:您可以将数据作为字节缓冲区以一种支持的像素格式(例如I420或NV12)提供。遗憾的是,没有完美的方法来确定哪种格式适用于特定设备;但是,通常可以从相机获得与编码器相同的格式。

相关问题