6

我尝试关注这个问题Concatenate multiple mp4 audio files using android´s MediaMuxer and concatenate video files。

但是由于某种原因,当我调用 readSampleData 时,MediaExtractor 会引发 IllegalArgumentException

在官方 API 中,这个函数根本不应该抛出任何异常!这是怎么回事 ?

我发现一个老问题表明 ByteBuffer 的大小可能是负责任的:Android MediaMuxer readSampleData IllegalStateException

我已经尝试了大量的尺寸值,但没有一个能解决问题。有我应该知道的标准尺寸吗?

任何提示都会有所帮助!

    boolean VERBOSE = true;


private boolean concatenateFiles(File dst, List<File> sources) {
    if ((sources == null) || (sources.size() == 0)) {
        return false;
    }

    boolean result;
    MediaExtractor extractor = null;
    MediaMuxer muxer = null;
    try {
        // Set up MediaMuxer for the destination.
        muxer = new MediaMuxer(dst.getPath(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);

        // Copy the samples from MediaExtractor to MediaMuxer.
        boolean sawEOS = false;

        int frameCount = 0;
        int offset = 100;

        ByteBuffer dstBuf = ByteBuffer.allocate(256 * 1024);
        BufferInfo bufferInfo = new BufferInfo();

        long timeOffsetUs = 0;
        int dstTrackIndex = -1;

        for (int fileIndex = 0; fileIndex < sources.size(); fileIndex++) {
            int numberOfSamplesInSource = getNumberOfSamples(sources.get(fileIndex));
            if (VERBOSE) {
                Log.d(TAG, String.format("Source file: %s", sources.get(fileIndex).getPath()));
            }

            if (!sources.get(fileIndex).canRead()) {
                throw new FileNotFoundException("Unable to read " + sources.get(fileIndex));
            }

            // Set up MediaExtractor to read from the source.
            extractor = new MediaExtractor();
            extractor.setDataSource(sources.get(fileIndex).getPath());

            // Set up the tracks.
            SparseIntArray indexMap = new SparseIntArray(extractor.getTrackCount());
            for (int i = 0; i < extractor.getTrackCount(); i++) {
                extractor.selectTrack(i);
                MediaFormat format = extractor.getTrackFormat(i);
                if (dstTrackIndex < 0) {
                    dstTrackIndex = muxer.addTrack(format);
                    muxer.start();
                }
                indexMap.put(i, dstTrackIndex);
            }

            long lastPresentationTimeUs = 0;
            int currentSample = 0;

            while (!sawEOS) {
                bufferInfo.offset = offset;

                bufferInfo.size = extractor.readSampleData(dstBuf, offset);


                if (bufferInfo.size < 0) {
                    sawEOS = true;
                    bufferInfo.size = 0;
                    timeOffsetUs += (lastPresentationTimeUs);
                }
                else {
                    lastPresentationTimeUs = extractor.getSampleTime();
                    bufferInfo.presentationTimeUs = extractor.getSampleTime() + timeOffsetUs;
                    bufferInfo.flags = extractor.getSampleFlags();
                    int trackIndex = extractor.getSampleTrackIndex();

                    if ((currentSample < numberOfSamplesInSource) || (fileIndex == sources.size() - 1)) {
                        muxer.writeSampleData(indexMap.get(trackIndex), dstBuf, bufferInfo);
                    }
                    extractor.advance();

                    frameCount++;
                    currentSample++;
                    if (VERBOSE) {
                        Log.d(TAG, "Frame (" + frameCount + ") " +
                                "PresentationTimeUs:" + bufferInfo.presentationTimeUs +
                                " Flags:" + bufferInfo.flags +
                                " TrackIndex:" + trackIndex +
                                " Size(KB) " + bufferInfo.size / 1024);
                    }
                }
            }
            extractor.release();
            extractor = null;
        }

        result = true;
    }
    catch (Exception e) {
        e.printStackTrace();
        result = false;
    }
    finally {
        if (extractor != null) {
            extractor.release();
        }
        if (muxer != null) {
            muxer.stop();
            muxer.release();
        }
    }
    return result;
}
4

1 回答 1

10

解码 4k 电影时出现此错误。其他(包括其他非常高分辨率的电影)一直运行良好,但大约在中途 - co-incedentilly 在更复杂的场景附近...... - 我收到了这个错误。

我的缓冲区大小是 1mb (1024*1024),增加到 2mb,问题就消失了。

所以你在源代码中找到的 END_OF_STREAM 大概是“缓冲区结束”

于 2016-02-02T17:41:31.360 回答