将Android Mediarecorder输出文件转换为H264

时间:2018-07-19 12:16:57

标签: java android h.264 android-mediarecorder mp4parser

我有一个MediaRecorder,它给了我File作为输出。但是我需要从录音中获取H264并将其与mp4parser库一起使用,该库可以在这里找到:mp4parser

Mp4parser允许创建H264 object like this

H264TrackImpl h264Track = new H264TrackImpl(new FileDataSourceImpl("video.h264"));

现在,H264停止时,我需要完全相同的MediaRecorder

private void setUpMediaRecorder() throws IOException {
    if (null == parentactivity) {
        return;
    }

    mMediaRecorder = new UMediaRecorder();
    mMediaRecorder.setStateListener(new UMediaRecorder.UMediaRecorderStateListener() {
        @Override
        public void onStop() {
            tempVideo = new File(mNextVideoAbsolutePath);

            try {
            // It gives the error here
            H264TrackImpl h264Track = new H264TrackImpl(new FileDataSourceImpl(mNextVideoAbsolutePath));
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
    });
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

    mNextVideoAbsolutePath = getVideoFilePath();
    mMediaRecorder.setOutputFile(mNextVideoAbsolutePath);

    mMediaRecorder.setVideoFrameRate(30);
    mMediaRecorder.setVideoEncodingBitRate(500000); //Recomennded 500k

    mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);

    int rotation = parentactivity.getWindowManager().getDefaultDisplay().getRotation();

    switch (mSensorOrientation) {
        case SENSOR_ORIENTATION_DEFAULT_DEGREES:
            mMediaRecorder.setOrientationHint(DEFAULT_ORIENTATIONS.get(rotation));
            break;
        case SENSOR_ORIENTATION_INVERSE_DEGREES:
            mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation));
            break;
    }

    try {
        mMediaRecorder.prepare();
    } catch (IllegalStateException ex) {
        ex.printStackTrace();
    }
}

尝试使用H264tempVideo制作mNextVideoAbsolutePath时,给我错误:

两者都给出相同的错误:

W/System.err: java.lang.IndexOutOfBoundsException: index=0 out of bounds (limit=0)
W/System.err:     at java.nio.Buffer.checkIndex(Buffer.java:528)
                  at java.nio.DirectByteBuffer.get(DirectByteBuffer.java:204)
                  at com.googlecode.mp4parser.authoring.tracks.h264.H264TrackImpl.getNalUnitHeader(H264TrackImpl.java:96)
W/System.err:     at com.googlecode.mp4parser.authoring.tracks.h264.H264TrackImpl.readSamples(H264TrackImpl.java:285)
                  at com.googlecode.mp4parser.authoring.tracks.h264.H264TrackImpl.parse(H264TrackImpl.java:107)
                  at com.googlecode.mp4parser.authoring.tracks.h264.H264TrackImpl.<init>(H264TrackImpl.java:83)
                  at com.googlecode.mp4parser.authoring.tracks.h264.H264TrackImpl.<init>(H264TrackImpl.java:87)
                  at com.googlecode.mp4parser.authoring.tracks.h264.H264TrackImpl.<init>(H264TrackImpl.java:91)

0 个答案:

没有答案