Display progress bar while compressing video file

2019-06-02 00:19发布

I have to compress video files. So I used this link http://whaticode.com/tag/audio/ and xuggler for the compression. Now I want to show the progress bar while compressing the video file in 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();
            }

        }
}

Thanks for any help!

2条回答
相关推荐>>
2楼-- · 2019-06-02 00:27
  1. Use a Task or a Service instead of a Runnable.
  2. Use Task.updateProgress() to inform on the current progress / work done.
  3. Bind the progressProperty of your running Task to a ProgressBar or ProgressIndicator.
查看更多
老娘就宠你
3楼-- · 2019-06-02 00:51

I would suggest that you read this doc from oracle. It describes the basics of Concurrency in JavaFX.

查看更多
登录 后发表回答