gpt4 book ai didi

安卓 MediaExtractor readSampleData IllegalArgumentException

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:36:13 32 4
gpt4 key购买 nike

我试着回答这个问题 Concatenate multiple mp4 audio files using android´s MediaMuxer并连接视频文件。

但由于某些原因,当我调用 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;
}

最佳答案

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

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

因此您在源代码中找到的 END_OF_STREAM 大概是“缓冲区的结尾”

关于安卓 MediaExtractor readSampleData IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33148629/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com