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

Mel*_*Mel 3 video android

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

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

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

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

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

相关链接......

Android屏幕捕获或从图像制作视频

如何像iPhone中的Talking Tomcat应用程序一样录制屏幕视频?

Ale*_*x I 8

使用MediaCodec API将CONFIGURE_FLAG_ENCODE其设置为编码器.不需要ffmpeg :)

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

编辑:这个样本代码很难找到,但在这里,它是对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
    }
}
Run Code Online (Sandbox Code Playgroud)

还有一些未解决的问题.

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