2

我试图使用 Xuggler 将音频格式转换为 ogg ( vorbis ) 文件。

首先,我已经尝试了非常基本的方法,

IMediaReader readermp3 = ToolFactory.makeReader(sourcefile);
IMediaWriter writer = ToolFactory.makeWriter(targetfilemp3, readermp3);
readermp3.addListener(writer);
while (readermp3.readPacket() == null)

目标文件以 .ogg 结尾。这实际上有效,但是它编码为 ogg FLAC 而不是 ogg vorbis。

然后我尝试手动完成它的一些部分,

首先 - 获取原始采样率和通道号,其次 - 添加音频流,使用这些采样率、通道号和 vorbis 的编解码器 ID。

IMediaReader readermp3 = ToolFactory.makeReader(sourcefile);
    IContainer readercontainer = readermp3.getContainer();
    readercontainer.open(sourcefile, IContainer.Type.READ,null);

    int numStreams = readercontainer.getNumStreams();  

    IStream stream = null;
    for(int i = 0; i < numStreams; i++) {  
    stream = readercontainer.getStream(i); } 

IStreamCoder mycoder = stream.getStreamCoder();
int sourcechannels,sourcesamplerate;
sourcechannels = mycoder.getChannels();
sourcesamplerate = mycoder.getSampleRate();

/* 这些是用于获取采样率和通道,我认为它们不相关,但我*/复制它只是为了以防万一

IMediaWriter writer = ToolFactory.makeWriter(targetfilemp3, readermp3);
writer.addAudioStream(0, 0,ICodec.ID.CODEC_ID_VORBIS,sourcechannels, sourcesamplerate);
readermp3.addListener(writer);
while (readermp3.readPacket() == null)
       ;

当我尝试运行它时,它给了我一个错误“错误 org.ffmpeg - [libvorbis @ 0436F300] 不支持指定的 sample_fmt。” “警告 com.xuggle.xuggler - 错误:无法打开编解码器 (../../../../../../../csrc/com/xuggle/xuggler/StreamCoder.cpp:831) "

旁注 = 它适用于 mp3、wav 和 flac ogg 文件。另外,我尝试将扩展名更改为 .vorbis 而不是 .ogg。还是同样的错误。

然后当然,我用谷歌搜索了它。关于这个问题只有 1 个相关的 q/a https://groups.google.com/forum/?fromgroups=#!topic/xuggler-users/18hsI_LGxI4

但是,我不明白答案=)。

非常感谢您的回答。

4

1 回答 1

1

Xuggler发生了一件奇怪的事情,它并不总是允许您将采样率设置为IAudioSamples. 您需要使用IAudioResampler.

我花了一段时间才弄清楚这一点。Marty的这篇文章很有帮助,尽管他的代码现在已经过时了。

这是你如何解决它。

.

编码前

我在这里假设音频输入已正确设置,导致IStreamCoder调用audioCoder.

完成后,您可能正在启动IMediaWriter并添加一个音频流,如下所示:

final IMediaWriter oggWriter = ToolFactory.makeWriter(oggOutputFile);

// Using stream 1 'cause there is also a video stream.
// For an audio only file you should use stream 0.
oggWriter.addAudioStream(1, 1, ICodec.ID.CODEC_ID_VORBIS, 
                         audioCoder.getChannels(), audioCoder.getSampleRate());

现在创建一个IAudioResampler

IAudioResampler oggResampler = IAudioResampler.make(audioCoder.getChannels(), 
                                                   audioCoder.getChannels(), 
                                                   audioCoder.getSampleRate(),
                                                   audioCoder.getSampleRate(),  
                                                   IAudioSamples.Format.FMT_FLT, 
                                                   audioCoder.getSampleFormat());

并告诉您IMediaWriter更新到其示例格式:

// The stream 1 here is consistent with the stream we added earlier.
oggWriter.getContainer().getStream(1).getStreamCoder().
                         setSampleFormat(IAudioSamples.Format.FMT_FLT);

.

编码期间

您当前可能正在启动一个IAudioSamples并用音频数据填充它,如下所示:

IAudioSamples audioSample = IAudioSamples.make(512, audioCoder.getChannels(), 
                                                    audioCoder.getSampleFormat());

int bytesDecoded = audioCoder.decodeAudio(audioSample, packet, offset);

现在为我们的重采样数据启动IAudioSamples :

IAudioSamples vorbisSample = IAudioSamples.make(512, audioCoder.getChannels(),
                                                IAudioSamples.Format.FMT_FLT);

最后,重新采样音频数据并写入结果:

oggResampler.resample(vorbisSample, audioSample, 0);

oggWriter.encodeAudio(1, vorbisSample);  

.

最后的想法

只是让您的输出文件正常播放的提示:

  • 如果在同一个容器中使用音频和视频,那么音频和视频数据包的写入顺序应该是每个数据包的时间戳都高于前一个数据包的时间戳。因此,您几乎可以肯定需要某种交替写入音频和视频的缓冲机制。
于 2014-05-07T05:03:44.747 回答