使用下面的方法,我可以降低mp3的比特率(使用xuggler lib),
public void reduce_file(File input, final File output, final int kbps, int index) { //modify on your convenience
// create a media reader
try {
IMediaReader mediaReader = ToolFactory.makeReader(input.getPath());
System.out.println("around");
// create a media writer
IMediaWriter mediaWriter = ToolFactory.makeWriter(output.getPath(), mediaReader);
// add a writer to the reader, to create the output file
mediaReader.addListener(mediaWriter);
// add a IMediaListner to the writer to change bit rate
mediaWriter.addListener(new IMediaListener() {
@Override
public void onVideoPicture(IVideoPictureEvent ivpe) {
}
@Override
public void onAudioSamples(IAudioSamplesEvent iase) {
}
@Override
public void onOpen(IOpenEvent ioe) {
System.out.println("open");
}
@Override
public void onClose(ICloseEvent ice) {
System.out.println("closed");
}
@Override
public void onAddStream(IAddStreamEvent event) {
IStreamCoder streamCoder = event.getSource().getContainer().getStream(event.getStreamIndex()).getStreamCoder();
//streamCoder.setFlag(IStreamCoder.Flags.FLAG_QSCALE, false);
streamCoder.setBitRate(kbps);
streamCoder.setBitRateTolerance(0);
//streamCoder.setSampleRate(22050);
streamCoder.setSampleFormat(IAudioSamples.Format.FMT_S32);
// streamCoder.setChannels(2);
}
@Override
public void onOpenCoder(IOpenCoderEvent ioce) {
}
@Override
public void onCloseCoder(ICloseCoderEvent icce) {
}
@Override
public void onReadPacket(IReadPacketEvent irpe) {
}
@Override
public void onWritePacket(IWritePacketEvent iwpe) {
}
@Override
public void onWriteHeader(IWriteHeaderEvent iwhe) {
iwhe.getSource().getContainer().writeHeader();
}
@Override
public void onFlush(IFlushEvent ife) {
}
@Override
public void onWriteTrailer(IWriteTrailerEvent iwte) {
}
});
while (mediaReader.readPacket() == null) ;
} catch (Exception ex) {
}
}
}
但是,对于某些媒体文件,我收到此错误:
ERROR org.ffmpeg - [mp3 @ 0x4e6e8e0] Header missing.
如何更正此错误,或者是否有其他lib。这将使我实现这一目标。提前谢谢。