我正在尝试使用xuggler从视频文件( .avi,*。mp4等)制作音频文件(* .mp3, .wav等),这里是我的代码
Code:
IMediaReader reader = ToolFactory.makeReader("D:/Frames/my.mp4");
IMediaWriter writer = ToolFactory.makeWriter("D:/a.mp3",reader);
int sampleRate = 44100;
int channels = 1;
writer.addAudioStream(0, 0, ICodec.ID.CODEC_ID_MP3, channels, sampleRate);
while (reader.readPacket() == null);
但它不会为我创建一个音频文件。请指导我在哪里做错了。如果你要纠正它或为此目的提供一些与我不同的代码,那么我将感激不尽。
答案 0 :(得分:15)
它实际上在场景后面使用了ffmpeg,它将从视频文件中提取音频以及更多用于编码的东西。 这是JAVE的链接 http://www.sauronsoftware.it/projects/jave/index.php
另见一个例子,我也在这里发帖也是为了您的方便
File source = new File("source.mp4");
File target = new File("target.mp3");
AudioAttributes audio = new AudioAttributes();
audio.setCodec("libmp3lame");
audio.setBitRate(new Integer(128000));
audio.setChannels(new Integer(2));
audio.setSamplingRate(new Integer(44100));
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("mp3");
attrs.setAudioAttributes(audio);
Encoder encoder = new Encoder();
encoder.encode(source, target, attrs);
希望它能帮到你..!
答案 1 :(得分:3)
这是一篇旧帖子,但是,我希望我的回答可以帮助某人:
我使用的代码与问题相同,但由于某些文件无法使用该代码进行转换,因此遇到了麻烦。
在这个答案中,我打开文件,当它包含音频时,我创建了该文件。否则不会创建任何文件。这种方式是提取任何文件的音频部分的正确方法。
可以将相同的模式应用于视频。我认为这段代码也会给出这个想法。
package test.video;
import com.xuggle.mediatool.IMediaReader;
import com.xuggle.mediatool.IMediaWriter;
import com.xuggle.mediatool.MediaToolAdapter;
import com.xuggle.mediatool.ToolFactory;
import com.xuggle.mediatool.event.IAudioSamplesEvent;
import com.xuggle.mediatool.event.ICloseEvent;
import com.xuggle.mediatool.event.IOpenCoderEvent;
import com.xuggle.xuggler.IContainer;
import com.xuggle.xuggler.IStreamCoder;
/**
*
* @author Pasban
*/
public class separateAudioVideo {
public static void main(String[] args) throws Exception {
String file = "pasban/22.mp4";
String to = "pasban/22.mp3";
convert(file, to);
}
public static void convert(String from, final String to) {
IMediaReader mediaReader = ToolFactory.makeReader(from);
final int mySampleRate = 44100;
final int myChannels = 2;
mediaReader.addListener(new MediaToolAdapter() {
private IContainer container;
private IMediaWriter mediaWriter;
@Override
public void onOpenCoder(IOpenCoderEvent event) {
container = event.getSource().getContainer();
mediaWriter = null;
}
@Override
public void onAudioSamples(IAudioSamplesEvent event) {
if (container != null) {
if (mediaWriter == null) {
mediaWriter = ToolFactory.makeWriter(to);
mediaWriter.addListener(new MediaListenerAdapter() {
@Override
public void onAddStream(IAddStreamEvent event) {
IStreamCoder streamCoder = event.getSource().getContainer().getStream(event.getStreamIndex()).getStreamCoder();
streamCoder.setFlag(IStreamCoder.Flags.FLAG_QSCALE, false);
streamCoder.setBitRate(128);
streamCoder.setChannels(myChannels);
streamCoder.setSampleRate(mySampleRate);
streamCoder.setBitRateTolerance(0);
}
});
mediaWriter.addAudioStream(0, 0, myChannels, mySampleRate);
}
mediaWriter.encodeAudio(0, event.getAudioSamples());
//System.out.println(event.getTimeStamp() / 1000);
}
}
@Override
public void onClose(ICloseEvent event) {
if (mediaWriter != null) {
mediaWriter.close();
}
}
});
while (mediaReader.readPacket() == null) {
}
}
}
答案 2 :(得分:2)
你应该让作者听取读者的数据包。
reader.addListener(writer);
这就是让你工作所需的一切。
答案 3 :(得分:0)
在我的代码中,按照@Kostas Andrianopoulos的回答。引发异常:"错误操作不被允许,无法将标题写入容器....."。修改后,代码为:
IMediaReader reader = ToolFactory.makeReader("D:\\123.flv");
IMediaWriter writer = ToolFactory.makeWriter("D:\\output.mp3", reader);
int sampleRate = 44100;
int channels = 2;
writer.setMaskLateStreamExceptions(true);
writer.addAudioStream(1, 0, ICodec.ID.CODEC_ID_MP3, channels, sampleRate);
reader.addListener(writer);
while(reader.readPacket() == null)
;
一切都很好。但最重要的是:我不知道为什么? addAudioStream的第一个参与者(阅读文档后)的含义是什么,所以有人会给我一些信息,谢谢。
答案 4 :(得分:-1)
我认为这是一项改进,对n个音频文件有好处。
import com.xuggle.mediatool.IMediaWriter;
import com.xuggle.mediatool.ToolFactory;
import com.xuggle.xuggler.IAudioSamples;
import com.xuggle.xuggler.IContainer;
import com.xuggle.xuggler.IPacket;
import com.xuggle.xuggler.IStreamCoder;
public class Concatenador_Audios {
public static void main(String[] args) {
ConcatenarAudios("D:\\out concatenate.mp3", "D:\\in Audio (1).mp3", "D:\\in Audio (2).mp3", "D:\\in Audio (3).mp3");
}
public static void ConcatenarAudios(String Ruta_AudioConcatenado,String... ruta_Audio) {
int n = ruta_Audio.length;
IMediaWriter mWriter = ToolFactory.makeWriter(Ruta_AudioConcatenado);
IPacket packet = IPacket.make();
for (int i = 0; i < n; i++) {
IContainer container = IContainer.make();
container.open(ruta_Audio[i], IContainer.Type.READ, null);
IStreamCoder audio = container.getStream(0).getStreamCoder();
audio.open(null, null);
if (i == 0) {
mWriter.addAudioStream(0, 0, audio.getChannels(), audio.getSampleRate());
}
while (container.readNextPacket(packet) >= 0) {
IAudioSamples samples = IAudioSamples.make(512, audio.getChannels(), IAudioSamples.Format.FMT_S32);
audio.decodeAudio(samples, packet, 0);
mWriter.encodeAudio(0, samples);
}
container.close();
audio.close();
}
mWriter.close();
}
}
}