【问题标题】:Decoding large audio file usind Xuggler使用 Xuggler 解码大型音频文件
【发布时间】:2015-05-18 20:11:16
【问题描述】:

我的脚本从一些 .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();
}

【问题讨论】:

    标签: java audio filesize xuggler flac


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-01
      • 2011-10-17
      • 1970-01-01
      • 2014-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多