我试图合并音频和视频,但我得到了java.lang.IllegalStateException: Failed to add the track to the muxer。我认为问题是,我不能添加.weba音频到穆克塞。如果是这样的话,我怎么才能把它们合并呢?
fun mux() {
var outputFile = ""
val dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).absolutePath
val file = File("$dir${File.separator}final.mp4")
file.createNewFile()
outputFile = file.absolutePath
val videoExtractor = MediaExtractor()
videoExtractor.setDataSource("$dir${File.separator}output.mp4")
videoExtractor.selectTrack(0)
val videoFormat = videoExtractor.getTrackFormat(0)
val videoMimeType = videoFormat.getString(MediaFormat.KEY_MIME)
val videoFrameMaxInputSize = videoFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE)
val videoFrameRate = videoFormat.getInteger(MediaFormat.KEY_FRAME_RATE)
val videoDuration = videoFormat.getLong(MediaFormat.KEY_DURATION)
val audioExtractor = MediaExtractor()
audioExtractor.setDataSource("$dir${File.separator}output.mp3")
audioExtractor.selectTrack(0)
val audioFormat = audioExtractor.getTrackFormat(0)
val audioMimeType = audioFormat.getString(MediaFormat.KEY_MIME)
// start muxer
val mediaMuxer = MediaMuxer(outputFile, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4)
val videoTrackIndex = mediaMuxer.addTrack(videoFormat)
val audioTrackIndex = mediaMuxer.addTrack(audioFormat)
val byteBuffer = ByteBuffer.allocate(videoFrameMaxInputSize)
mediaMuxer.start()
// writing video
val videoBufferInfo = MediaCodec.BufferInfo()
videoExtractor.selectTrack(videoTrackIndex)
while (true) {
val readVideoSampleSize = videoExtractor.readSampleData(byteBuffer, 0)
if (readVideoSampleSize < 0) {
videoExtractor.unselectTrack(videoTrackIndex)
break
}
val videoSampleTime = videoExtractor.sampleTime
videoBufferInfo.size = readVideoSampleSize
videoBufferInfo.presentationTimeUs = videoSampleTime
//videoBufferInfo.presentationTimeUs += 1000 * 1000 / frameRate;
videoBufferInfo.offset = 0
videoBufferInfo.flags = videoExtractor.sampleFlags
mediaMuxer.writeSampleData(videoTrackIndex, byteBuffer, videoBufferInfo)
videoExtractor.advance()
Log.d("hello", readVideoSampleSize.toString())
}
// writing audio
val audioBufferInfo = MediaCodec.BufferInfo()
audioExtractor.selectTrack(audioTrackIndex)
while (true) {
val readAudioSampleSize = audioExtractor.readSampleData(byteBuffer, 0)
if (readAudioSampleSize < 0) {
break
}
val audioSampleTime = audioExtractor.sampleTime
audioBufferInfo.size = readAudioSampleSize
audioBufferInfo.presentationTimeUs = audioSampleTime
if (audioBufferInfo.presentationTimeUs > videoDuration) {
audioExtractor.unselectTrack(audioTrackIndex)
break
}
audioBufferInfo.offset = 0
audioBufferInfo.flags = audioExtractor.sampleFlags
mediaMuxer.writeSampleData(audioTrackIndex, byteBuffer, audioBufferInfo)
audioExtractor.advance()
}
// releasing resources
try {
mediaMuxer.stop()
mediaMuxer.release()
} catch (e: Exception) {
e.printStackTrace()
}
try {
videoExtractor.release()
} catch (e: Exception) {
e.printStackTrace()
}
try {
audioExtractor.release()
} catch (e: Exception) {
e.printStackTrace()
}
}编辑的
我也尝试过使用.mp3音频,但这也给了我同样的错误。
发布于 2020-04-25 22:15:44
简单的答案是,您不能直接将它们与MediaMuxer API合并。该类的文档具有一个具有所有支持格式的表格。对于mpeg4输出,支持的音频编解码器是AAC、AMR_NB和AMR_WB。
您可以尝试使用像MediaCodec类这样的工具来解码音频数据,然后将其编码为支持的格式之一。如果您只想使用Android。否则,有大量的库允许使用更多种类的编解码器进行修改。
https://stackoverflow.com/questions/61198338
复制相似问题