【发布时间】:2016-02-02 04:42:07
【问题描述】:
我不想使用 ffmpeg。目前,我能够将 m4a 音频与 mp4 视频混合。我还希望添加需要 mp3 到 m4a 转换的 mp3 音频。我能够使用以下代码将 wav 转换为 m4a 但不能将 mp3 转换为 m4a
这是我的代码。
private void convertAudio(String filename) throws IOException {
String outputpath =Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getPath()+"/converted.m4a";
// Set up MediaExtractor to read from the source.
MediaExtractor extractor = new MediaExtractor();
extractor.setDataSource(filename);
int trackCount = extractor.getTrackCount();
// Set up MediaMuxer for the destination.
MediaMuxer muxer;
muxer = new MediaMuxer(outputpath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
// Set up the tracks.
HashMap<Integer, Integer> indexMap = new HashMap<Integer, Integer>(trackCount);
for (int i = 0; i < trackCount; i++) {
extractor.selectTrack(i);
MediaFormat format = extractor.getTrackFormat(i);
format.setString(MediaFormat.KEY_MIME, MediaFormat.MIMETYPE_AUDIO_AMR_NB);
int dstIndex = muxer.addTrack(format);
indexMap.put(i, dstIndex);
}
// Copy the samples from MediaExtractor to MediaMuxer.
boolean sawEOS = false;
int bufferSize = 32000;
int frameCount = 0;
int offset = 100;
ByteBuffer dstBuf = ByteBuffer.allocate(bufferSize);
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
/* if (degrees >= 0) {
muxer.setOrientationHint(degrees);
}*/
// Test setLocation out of bound cases
muxer.start();
while (!sawEOS) {
bufferInfo.offset = offset;
bufferInfo.size = extractor.readSampleData(dstBuf, offset);
if (bufferInfo.size < 0) {
sawEOS = true;
bufferInfo.size = 0;
} else {
bufferInfo.presentationTimeUs = extractor.getSampleTime();
bufferInfo.flags = extractor.getSampleFlags();
int trackIndex = extractor.getSampleTrackIndex();
muxer.writeSampleData(indexMap.get(trackIndex), dstBuf,
bufferInfo);
extractor.advance();
frameCount++;
}
}
muxer.stop();
muxer.release();
return;
}
【问题讨论】:
-
目前我正在努力将音频和视频与媒体多路复用器合并,但我失败了,您可以通过分享您的合并代码来帮助我
-
这是我的问题:未知的 mime 类型 'audio/mpeg'。 12-09 11:58:33.569: A/MPEG4Writer(332): frameworks/av/media/libstagefright/MPEG4Writer.cpp:2699 CHECK(!"不应该在这里,未知的 mime 类型。")
-
您找到将 mp3 转换为 m4a 的方法了吗?如果你已经完成了,请分享它,谢谢。
-
这个问题的困惑在于mp3和m4a是两个不同的东西。 Mp3 是一种音频编解码器,而 m4a 只是 mp4 容器的另一个文件扩展名。虽然 AAC 是 mp4 中最常用的音频编解码器,但将 mp3 放入 mp4 中是完全有效的。所以不需要转换。只需将 mp3 混合到 mp4 容器中。
标签: android mp3 m4a mediamuxer