gpt4 book ai didi

java - 使用 Xuggler 解码大型音频文件

转载 作者:行者123 更新时间:2023-12-03 02:02:33 24 4
gpt4 key购买 nike

我的脚本从一些 .jpg 生成视频图像效果很好。此外,它还从现有的.flac 中添加了一个音频流。文件。该代码在处理小型音频文件时效果很好。但是使用我在 1GB 附近的实际文件我收到以下警告/异常:

22:00:03.366 [main] WARN  com.xuggle.xuggler - error: not enough memory in internal frame buffer (../../../../../../../csrc/com/xuggle/xuggler/StreamCoder.cpp:1768)
Exception in thread "main" java.lang.RuntimeException: failed to encode audio
at com.xuggle.mediatool.MediaWriter.encodeAudio(MediaWriter.java:855)
at diaporama.Renderer.renderVideo(Renderer.java:129)
at diaporama.Renderer.<init>(Renderer.java:32)
at Run.main(Run.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

我猜没有足够的内存来缓冲大的音频文件。但我很难找到解决方案。有人对如何可靠地处理这方面的大型音频文件有任何建议吗?

这是我的代码:
private void renderVideo() {
final IMediaWriter writer = ToolFactory.makeWriter(this.outputFileName);

// video stream
writer.addVideoStream(0, 0, this.videoCodec, this.width, this.height);

// audio stream
IContainer containerAudio = IContainer.make();
containerAudio.open(this.audioFile.getPath(), IContainer.Type.READ, null);
IStreamCoder coderAudio = containerAudio.getStream(0).getStreamCoder();
if (coderAudio.open(null, null) < 0)
throw new RuntimeException("Cannot open audio coder");
writer.addAudioStream(1, 0, this.audioCodec, coderAudio.getChannels(), coderAudio.getSampleRate());

// video rendering
// ... video rendering takes place here ...

// audio rendering
System.out.println("Adding audio file: " + this.audioFile.getPath() + " to stream");

// read audio file and create stream
IPacket packetaudio = IPacket.make();
IAudioSamples samples;

while (containerAudio.readNextPacket(packetaudio) >= 0) {
/*
* We allocate a set of samples with the same number of channels as the
* coder tells us is in this buffer.
*
* We also pass in a buffer size (1024 in our example), although Xuggler
* will probably allocate more space than just the 1024 (it's not important why).
*/
samples = IAudioSamples.make(1024, coderAudio.getChannels());

/*
* A packet can actually contain multiple sets of samples (or frames of samples
* in audio-decoding speak). So, we may need to call decode audio multiple
* times at different offsets in the packet's data. We capture that here.
*/
int offset = 0;

/*
* Keep going until we've processed all data
*/
while (offset < packetaudio.getSize()) {
int bytesDecoded = coderAudio.decodeAudio(samples, packetaudio, offset);
if (bytesDecoded < 0)
throw new RuntimeException("got error decoding audio in: " + this.audioFile);
offset += bytesDecoded;
/*
* Some decoder will consume data in a packet, but will not be able to construct
* a full set of samples yet. Therefore you should always check if you
* got a complete set of samples from the decoder
*/
if (samples.isComplete()) {
writer.encodeAudio(1, samples);
}
}

}

coderAudio.close();
containerAudio.close();

writer.close();
}

最佳答案

你给你的JVM多少内存?可能你应该增加最大允许的堆大小,-Xmx4g (对于 4Gb 堆)或类似的东西。这里有很多选择。

由于 Xuggle 似乎是一个原生库,所以原生堆大小也可以很小。这很难解决,但您至少可以确保您运行的是 64 位版本的 JVM。

如果您使用某些 IDE,这些选项可以设置为运行配置。

关于java - 使用 Xuggler 解码大型音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30311995/

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