gpt4 book ai didi

java - 在android中合并音频和视频文件

转载 作者:行者123 更新时间:2023-11-30 02:02:46 27 4
gpt4 key购买 nike

我正在使用 mp4parser 合并音频和视频文件,下面是我尝试过的示例,但我在第一行本身遇到了空指针异常。我已将音频和视频文件保存在手机内存中的所需位置。如果我调试,第一行会花费很多时间,并且会在 mins 后因空指针错误而停止

try
{
H264TrackImpl h264Track = new H264TrackImpl(new FileDataSourceImpl("/mnt/sdcard/myvideo/video.mp4"));
AACTrackImpl aacTrack = new AACTrackImpl(new FileDataSourceImpl("/mnt/sdcard/myvideo/audio.acc"));

Movie movie = new Movie();
movie.addTrack(h264Track);
movie.addTrack(aacTrack);


Container mp4file = new DefaultMp4Builder().build(movie);

FileChannel fc = new FileOutputStream(new File("output.mp4")).getChannel();
mp4file.writeContainer(fc);
fc.close();

} catch (Exception ee)
{
Toast.makeText(this,ee.getMessage(),Toast.LENGTH_LONG).show();
}

我上面的代码有什么问题?

最佳答案

**试试这个我遵循几件事来合并音频和视频。

  • 1) 捕获空白视频 不要使用任何音频源,例如"mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_RECOGNITION);"
  • 2)保存到sd卡
  • 3)不支持MIME type=mp3。
  • 4) 因此,如果我们必须合并视频和视频。音频必须是 mp4 或 aac.**

5)在点击按钮或在克里特岛上调用方法

  • String audiopath = "/sdcard/audio.m4a";

  • String videopath = "/sdcard/video.mp4";

  • String outputpath = "/sdcard/output.mp4";

  • 多路复用(视频、音频、输出);

6) 此处主要代码只传递存储视频、音频(m4a、aac)、输出路径的路径。

 public boolean mux(String videoFile, String audioFile, String outputFile) {
Movie video;
try {
video = new MovieCreator().build(videoFile);

} catch (RuntimeException e) {
e.printStackTrace();

return false;
} catch (IOException e) {
e.printStackTrace();

return false;
}

Movie audio;
try {

audio = new MovieCreator().build(audioFile);

} catch (IOException e) {
e.printStackTrace();

return false;
} catch (NullPointerException e) {
e.printStackTrace();

return false;
}

Track audioTrack = audio.getTracks().get(0);
video.addTrack(audioTrack);

Container out = new DefaultMp4Builder().build(video);

FileOutputStream fos;
try {
fos = new FileOutputStream(outputFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
BufferedWritableFileByteChannel byteBufferByteChannel = new BufferedWritableFileByteChannel(fos);
try {

out.writeContainer(byteBufferByteChannel);
byteBufferByteChannel.close();
Log.e("Audio Video", "11");
fos.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}

private static class BufferedWritableFileByteChannel implements WritableByteChannel {
// private static final int BUFFER_CAPACITY = 1000000;
private static final int BUFFER_CAPACITY = 10000000;

private boolean isOpen = true;
private final OutputStream outputStream;
private final ByteBuffer byteBuffer;
private final byte[] rawBuffer = new byte[BUFFER_CAPACITY];

private BufferedWritableFileByteChannel(OutputStream outputStream) {
this.outputStream = outputStream;
this.byteBuffer = ByteBuffer.wrap(rawBuffer);
Log.e("Audio Video", "13");
}

@Override
public int write(ByteBuffer inputBuffer) throws IOException {
int inputBytes = inputBuffer.remaining();

if (inputBytes > byteBuffer.remaining()) {
Log.e("Size ok ", "song size is ok");
dumpToFile();
byteBuffer.clear();

if (inputBytes > byteBuffer.remaining()) {
Log.e("Size ok ", "song size is not okssss ok");
throw new BufferOverflowException();
}
}

byteBuffer.put(inputBuffer);

return inputBytes;
}

@Override
public boolean isOpen() {
return isOpen;
}

@Override
public void close() throws IOException {
dumpToFile();
isOpen = false;
}

private void dumpToFile() {
try {
outputStream.write(rawBuffer, 0, byteBuffer.position());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

关于java - 在android中合并音频和视频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31221467/

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