我必须压缩视频文件。所以我使用此链接http://whaticode.com/tag/audio/和xuggler进行压缩。现在我想在javafx中压缩视频文件时显示进度条。
import java.io.File;
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.AudioSamplesEvent;
import com.xuggle.mediatool.event.IAddStreamEvent;
import com.xuggle.mediatool.event.IAudioSamplesEvent;
import com.xuggle.mediatool.event.IVideoPictureEvent;
import com.xuggle.mediatool.event.VideoPictureEvent;
import com.xuggle.xuggler.IAudioResampler;
import com.xuggle.xuggler.IAudioSamples;
import com.xuggle.xuggler.IRational;
import com.xuggle.xuggler.IStreamCoder;
import com.xuggle.xuggler.IVideoPicture;
import com.xuggle.xuggler.IVideoResampler;
import com.xuggle.xuggler.ICodec;
public class ConvertVideo extends MediaToolAdapter implements Runnable{
private int VIDEO_WIDTH = 640;
private int VIDEO_HEIGHT = 360;
private IMediaWriter writer;
private IMediaReader reader;
private File outputFile;
public ConvertVideo(File inputFile, File outputFile) {
this.outputFile = outputFile;
reader = ToolFactory.makeReader(inputFile.getAbsolutePath());
reader.addListener(this);
}
private IVideoResampler videoResampler = null;
private IAudioResampler audioResampler = null;
@Override
public void onAddStream(IAddStreamEvent event) {
int streamIndex = event.getStreamIndex();
IStreamCoder streamCoder = event.getSource().getContainer().getStream(streamIndex).getStreamCoder();
if (streamCoder.getCodecType() == ICodec.Type.CODEC_TYPE_AUDIO) {
writer.addAudioStream(streamIndex, streamIndex, 2, 44100);
} else if (streamCoder.getCodecType() == ICodec.Type.CODEC_TYPE_VIDEO) {
streamCoder.setWidth(VIDEO_WIDTH);
streamCoder.setHeight(VIDEO_HEIGHT);
streamCoder.setBitRate(100);
streamCoder.setBitRateTolerance(100);
writer.addVideoStream(streamIndex, streamIndex, ICodec.ID.CODEC_ID_H264,IRational.make((double)15),VIDEO_WIDTH, VIDEO_HEIGHT);
}
super.onAddStream(event);
}
@Override
public void onVideoPicture(IVideoPictureEvent event) {
IVideoPicture pic = event.getPicture();
if (videoResampler == null) {
videoResampler = IVideoResampler.make(VIDEO_WIDTH, VIDEO_HEIGHT, pic.getPixelType(), pic.getWidth(), pic.getHeight(), pic.getPixelType());
}
IVideoPicture out = IVideoPicture.make(pic.getPixelType(), VIDEO_WIDTH, VIDEO_HEIGHT);
videoResampler.resample(out, pic);
IVideoPictureEvent asc = new VideoPictureEvent(event.getSource(), out, event.getStreamIndex());
super.onVideoPicture(asc);
out.delete();
}
@Override
public void onAudioSamples(IAudioSamplesEvent event) {
IAudioSamples samples = event.getAudioSamples();
if (audioResampler == null) {
audioResampler = IAudioResampler.make(2, samples.getChannels(), 44100, samples.getSampleRate());
}
if (event.getAudioSamples().getNumSamples() > 0) {
IAudioSamples out = IAudioSamples.make(samples.getNumSamples(), samples.getChannels());
audioResampler.resample(out, samples, samples.getNumSamples());
AudioSamplesEvent asc = new AudioSamplesEvent(event.getSource(), out, event.getStreamIndex());
super.onAudioSamples(asc);
out.delete();
}
}
public void run() {
writer = ToolFactory.makeWriter(outputFile.getAbsolutePath(), reader);
this.addListener(writer);
while (reader.readPacket() == null) {
System.out.println("Converting file..");
}
}
public static void main(String[] args) {
try {
System.out.println("Converting process started");
File file = new File("C:\\Development\\1.mp4");
file.createNewFile();
ConvertVideo obj = new ConvertVideo(new File("C:\\Development\\camera 1_record_1417702745727.wmv"),file);
obj.run();
System.out.println("Converting process end");
} catch (Exception e) {
e.printStackTrace();
}
}
}
感谢您的帮助!
答案 0 :(得分:0)
Task
or a Service
代替Runnable。Task.updateProgress()
通知当前进度/已完成的工作。progressProperty
绑定到ProgressBar或ProgressIndicator。答案 1 :(得分:0)
我建议你从oracle阅读this doc。它描述了JavaFX中的并发基础。