有一件奇怪的事情发生了Xuggler它并不总是允许您设置采样率IAudioSamples
。你需要使用一个IAudioResampler
.
我花了一段时间才弄清楚这一点。马蒂的这篇文章 https://groups.google.com/d/msg/xuggler-users/18hsI_LGxI4/vLYE1AeHVHEJ尽管他的代码现在已经过时了,但还是有很大帮助。
以下是修复方法。
.
编码前
我在这里假设音频输入已正确设置,从而导致IStreamCoder
called 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 vorbisSample = IAudioSamples.make(512, audioCoder.getChannels(),
IAudioSamples.Format.FMT_FLT);
最后,重新采样音频数据并写入结果:
oggResampler.resample(vorbisSample, audioSample, 0);
oggWriter.encodeAudio(1, vorbisSample);
.
最后的想法
只是让您的输出文件正常播放的提示:
- 如果在同一个容器内使用音频和视频,则音频和视频数据包的写入顺序应使每个数据包的时间戳高于前一个数据包的时间戳。因此,您几乎肯定需要某种缓冲机制来交替写入音频和视频。