gpt4 book ai didi

android - 如何使用 MediaMuxer 合并音频和视频?

转载 作者:太空宇宙 更新时间:2023-11-03 11:02:41 32 4
gpt4 key购买 nike

我正在开发一个带有 mp3 音频文件和 mp4 视频文件(无声音)的 Android 应用。

现在我想混合它们并创建一个新的 mp4 视频文件(带声音)。从 Android 4.3 开始,Google 建议使用 MediaMuxer 类来混合流式音频和视频。我试了很多次都没有成功。

代码如下:

    private void muxing() {

String outputFile = "";

try {

File file = new File(Environment.getExternalStorageDirectory() + "/final2.mp4");
file.createNewFile();
outputFile = file.getAbsolutePath();
MediaExtractor videoExtractor = new MediaExtractor();
videoExtractor.setDataSource(Environment.getExternalStorageDirectory().toString()+"/vd1.h264");
MediaExtractor audioExtractor = new MediaExtractor();
audioExtractor.setDataSource(Environment.getExternalStorageDirectory()+"/audioVideoDir/audio.m4a");
MediaMuxer muxer = new MediaMuxer(outputFile, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);

videoExtractor.selectTrack(0);
MediaFormat videoFormat = videoExtractor.getTrackFormat(0);
int videoTrack = muxer.addTrack(videoFormat);

audioExtractor.selectTrack(0);
MediaFormat audioFormat = audioExtractor.getTrackFormat(0);
int audioTrack = muxer.addTrack(audioFormat);
boolean sawEOS = false;
int frameCount = 0;
int offset = 100;
int sampleSize = 256 * 1024;
ByteBuffer videoBuf = ByteBuffer.allocate(sampleSize);
ByteBuffer audioBuf = ByteBuffer.allocate(sampleSize);
MediaCodec.BufferInfo videoBufferInfo = new MediaCodec.BufferInfo();
MediaCodec.BufferInfo audioBufferInfo = new MediaCodec.BufferInfo();


videoExtractor.seekTo(0, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
audioExtractor.seekTo(0, MediaExtractor.SEEK_TO_CLOSEST_SYNC);

muxer.start();

while (!sawEOS)
{
videoBufferInfo.offset = offset;
videoBufferInfo.size = videoExtractor.readSampleData(videoBuf, offset);


if (videoBufferInfo.size < 0 || audioBufferInfo.size < 0)
{
// Log.d(TAG, "saw input EOS.");
sawEOS = true;
videoBufferInfo.size = 0;

}
else
{
videoBufferInfo.presentationTimeUs = videoExtractor.getSampleTime();
videoBufferInfo.flags = videoExtractor.getSampleFlags();
muxer.writeSampleData(videoTrack, videoBuf, videoBufferInfo);
videoExtractor.advance();


frameCount++;

}
}

Toast.makeText(getApplicationContext() , "frame:" + frameCount , Toast.LENGTH_SHORT).show();



boolean sawEOS2 = false;
int frameCount2 =0;
while (!sawEOS2)
{
frameCount2++;

audioBufferInfo.offset = offset;
audioBufferInfo.size = audioExtractor.readSampleData(audioBuf, offset);

if (videoBufferInfo.size < 0 || audioBufferInfo.size < 0)
{
// Log.d(TAG, "saw input EOS.");
sawEOS2 = true;
audioBufferInfo.size = 0;
}
else
{
audioBufferInfo.presentationTimeUs = audioExtractor.getSampleTime();
audioBufferInfo.flags = audioExtractor.getSampleFlags();
muxer.writeSampleData(audioTrack, audioBuf, audioBufferInfo);
audioExtractor.advance();

}
}

Toast.makeText(getApplicationContext() , "frame:" + frameCount2 , Toast.LENGTH_SHORT).show();

muxer.stop();
muxer.release();


} catch (IOException e) {

} catch (Exception e) {

}
}

我收到以下错误:

未知的 MIME 类型“音频/mpeg”。 12-09 11:58:33.569: A/MPEG4Writer(332): frameworks/av/media/libstagefright/MPEG4Writer.cpp:2699 CHECK("不应该在这里,未知的 MIME 类型。")

是否有解决我的 Google MediaMuxer API 问题的解决方案?即使我在将音频和视频合并在一起后尝试使用 ffmpeg,视频也不会在 Android 设备的默认视频播放器中播放,但它会在 Vlc 播放器中加载。

FFMPEG 从这里跟随:https://github.com/WritingMinds/ffmpeg-android-java

最佳答案

尝试添加mp4 视频文件。它为我工作。但输出文件带有音频持续时间。

关于android - 如何使用 MediaMuxer 合并音频和视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38768746/

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