相关疑难解决方法(0)

使用Android MediaCodec从摄像头编码H.264

我试图让它在Android 4.1上运行(使用升级的Asus Transformer平板电脑).感谢Alex对我之前的问题的回答,我已经能够将一些原始的H.264数据写入文件,但是这个文件只能播放ffplay -f h264,而且似乎丢失了有关帧速率的所有信息(极快的播放).此外,色彩空间看起来不正确(atm使用摄像机默认的编码器侧).

public class AvcEncoder {

private MediaCodec mediaCodec;
private BufferedOutputStream outputStream;

public AvcEncoder() { 
    File f = new File(Environment.getExternalStorageDirectory(), "Download/video_encoded.264");
    touch (f);
    try {
        outputStream = new BufferedOutputStream(new FileOutputStream(f));
        Log.i("AvcEncoder", "outputStream initialized");
    } catch (Exception e){ 
        e.printStackTrace();
    }

    mediaCodec = MediaCodec.createEncoderByType("video/avc");
    MediaFormat mediaFormat = MediaFormat.createVideoFormat("video/avc", 320, 240);
    mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, 125000);
    mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 15);
    mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar);
    mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 5);
    mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    mediaCodec.start();
}

public void close() {
    try {
        mediaCodec.stop();
        mediaCodec.release();
        outputStream.flush();
        outputStream.close();
    } …
Run Code Online (Sandbox Code Playgroud)

android video-encoding video-streaming h.264 android-camera

30
推荐指数
3
解决办法
5万
查看次数