package zall.transcoder;

import com.xuggle.mediatool.IMediaReader;
import com.xuggle.mediatool.MediaToolAdapter;
import com.xuggle.mediatool.event.IVideoPictureEvent;

/**
 *
 */
class ProgressListener extends MediaToolAdapter {
	private long currentLength;
	private long totalLength;
	private int progress = -1;

	public ProgressListener(IMediaReader reader) {
        if( !reader.isOpen() )
            reader.open(); // read container
		this.totalLength = reader.getContainer().getDuration();
	}

	@Override
	public void onVideoPicture(IVideoPictureEvent event) {
		currentLength = event.getTimeStamp();

		if( (int)(100*getProgress()) != progress ){
			progress = (int)(100*getProgress());
			System.out.print("\n"+(int)(100*getProgress())+"% ");
		}
		else System.out.print(".");
	}

	public long getProcessedLength(){
		return currentLength;
	}

	public long getTotalLength(){
		return totalLength;
	}

	public double getProgress(){
        if(totalLength > 0)
            return ((double)currentLength/totalLength);
        return 0;
	}
}
