使用MediaCodec APIS解码视频时出现问题

时间:2015-01-15 04:00:39

标签: android mediacodec

我想使用Android MediaCodec和MediaMuxer APIS对新格式的视频进行编码,并在编码后将结果保存到新的.mp4文件中。

这是我的代码:

final String MIME_TYPE = "video/avc";
final int BIT_RATE = 128000; // 128kbps
final int SAMPLING_RATE = 44100;
final int CODEC_TIMEOUT_IN_MS = 5000;
boolean suc=false;

try {
    File inputFile = new File(filePath);
    FileInputStream fis = new FileInputStream(inputFile);

    File outputFile = new File(dstMediaPath);
    if (outputFile.exists())
        outputFile.delete();

    MediaMuxer mux = new MediaMuxer(outputFile.getAbsolutePath(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);

    MediaFormat outputFormat = MediaFormat.createAudioFormat(MIME_TYPE,SAMPLING_RATE, 1);
    outputFormat = MediaFormat.createVideoFormat("video/avc",
            320, 240);
    outputFormat.setInteger(MediaFormat.KEY_BIT_RATE, 125000);
    outputFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 15);
    outputFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT,
            MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar);
    outputFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 5);

    MediaCodec codec = MediaCodec.createEncoderByType(MIME_TYPE);
    codec.configure(outputFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    codec.start();

    ByteBuffer[] codecInputBuffers = codec.getInputBuffers(); // Note: Array of buffers
    ByteBuffer[] codecOutputBuffers = codec.getOutputBuffers();

    MediaCodec.BufferInfo outBuffInfo = new MediaCodec.BufferInfo();

    byte[] tempBuffer = new byte[4096];
    boolean hasMoreData = true;
    double presentationTimeUs = 0;
    int audioTrackIdx = 0;
    int totalBytesRead = 0;
    int percentComplete;

    do {

        int inputBufIndex = 0;
        while (inputBufIndex != -1 && hasMoreData) {
            inputBufIndex = codec.dequeueInputBuffer(CODEC_TIMEOUT_IN_MS);

            if (inputBufIndex >= 0) {
                ByteBuffer dstBuf = codecInputBuffers[inputBufIndex];
                dstBuf.clear();

                int bytesRead = fis.read(tempBuffer, 0, tempBuffer.length);
                if (bytesRead == -1) { // -1 implies EOS
                    hasMoreData = false;
                    codec.queueInputBuffer(inputBufIndex, 0, 0, (long) presentationTimeUs, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
                } else {
                    totalBytesRead += bytesRead;
                    dstBuf.put(tempBuffer, 0, bytesRead);
                    codec.queueInputBuffer(inputBufIndex, 0, bytesRead, (long) presentationTimeUs, 0);
                    presentationTimeUs = 1000000l * (totalBytesRead / 2) / SAMPLING_RATE;
                }
            }
        }

        int outputBufIndex = 0;
        while (outputBufIndex != MediaCodec.INFO_TRY_AGAIN_LATER) {
            outputBufIndex = codec.dequeueOutputBuffer(outBuffInfo, CODEC_TIMEOUT_IN_MS);
            if (outputBufIndex >= 0) {
                ByteBuffer encodedData = codecOutputBuffers[outputBufIndex];
                encodedData.position(outBuffInfo.offset);
                encodedData.limit(outBuffInfo.offset + outBuffInfo.size);

                if ((outBuffInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0 && outBuffInfo.size != 0) {
                    codec.releaseOutputBuffer(outputBufIndex, false);
                    outBuffInfo.size=0;
                } else {
                    mux.writeSampleData(audioTrackIdx, codecOutputBuffers[outputBufIndex], outBuffInfo);
                    codec.releaseOutputBuffer(outputBufIndex, false);
                }
            } else if (outputBufIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
                outputFormat = codec.getOutputFormat();
                Log.v(TAG, "Output format changed - " + outputFormat);
                audioTrackIdx = mux.addTrack(outputFormat);
                mux.start();
            } else if (outputBufIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
                Log.e(TAG, "Output buffers changed during encode!");
            } else if (outputBufIndex == MediaCodec.INFO_TRY_AGAIN_LATER) {
                // NO OP
            } else {
                Log.e(TAG, "Unknown return code from dequeueOutputBuffer - " + outputBufIndex);
            }
        }
        percentComplete = (int) Math.round(((float) totalBytesRead / (float) inputFile.length()) * 100.0);
    } while (outBuffInfo.flags != MediaCodec.BUFFER_FLAG_END_OF_STREAM);

    fis.close();
    mux.stop();
    mux.release();
    Log.v(TAG, "Compression done ...");
    suc=true;
} catch (FileNotFoundException e) {
    Log.e(TAG, "File not found!", e);
    suc=false;
} catch (IOException e) {
    Log.e(TAG, "IO exception!", e);
    suc=false;
}


return suc;

当打开文件输出时,它可以播放,但它显示错误的结果,其示例如下:

Example of Wrong Output File Results

任何人都可以帮我理解我做错了吗?

0 个答案:

没有答案