2

this code reads the time duration in long, but when it converts into the date with time format 'hh:mm:ss' it gives different value and the video length is 00:08:07. what is wrong in this code

String filename = "C:\\Documents\\Airtel Youthstar-Tutorial.mp4";   
    IContainer container = IContainer.make();  
    int result = container.open(filename, IContainer.Type.READ, null);  
    long duration = container.getDuration();  
    System.out.println("Duration (ms): " + duration);  
Sandip Bhoi
  • 440
  • 3
  • 9

4 Answers4

2

Actually your code returns time in microseconds. If you want to get java.util.Duration, you should use:

public static Duration getVideoDuration(String videoPath) {
    IContainer container = IContainer.make();
    int result = container.open(videoPath, IContainer.Type.READ, null);
    long durationInMicrosec = container.getDuration();
    long durationInNanoSec = durationInMicrosec * 1000;     
    return Duration.ofNanos(durationInNanoSec);
}

And for formating the result time, you can use code from @SASM, and the input for his code should be

long ms = getVideoDuration("your_path_here").toMillis()
jno
  • 31
  • 3
1

If you get the duration in milliseconds

long ms = xxxxx;

You can convert it to hh:mm:ss format as below:

String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(ms),
        TimeUnit.MILLISECONDS.toMinutes(ms) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(ms)),
        TimeUnit.MILLISECONDS.toSeconds(ms) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(ms)));
System.out.println(hms);
SASM
  • 1,292
  • 1
  • 22
  • 44
1

I got the proper video time duration using IBMPlayerForMpeg4SDK-1.0.0.jar and its work fine me by using following code

/**
     * 
     * @param filename is the video full file path stored at any location of the system 
     * @return the value containing the time format of the video file
     */
    public static String getDurationInString(String filename)
    {
        try {
            //
            long ms=getDuration(new File(filename));
            String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(ms),
                    TimeUnit.MILLISECONDS.toMinutes(ms) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(ms)),
                    TimeUnit.MILLISECONDS.toSeconds(ms) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(ms)));
            //System.out.println(hms);
            return hms;
        } catch (IOException ex) {
            Logger.getLogger(VideoInfo.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "";
    }


    /**
     * 
     * @param file : file that specify the file in the File location
     * @return the duration in long 
     * @throws IOException if any exception is thrown by the system 
     */
    public static long getDuration(File file) throws IOException {
        PlayerControl playerControl = PlayerFactory.createLightweightMPEG4Player();
        playerControl.open(file.getAbsolutePath());
        return playerControl.getDuration();
    }
Sandip Bhoi
  • 440
  • 3
  • 9
1
//In order to import you need to get mp4parser from here: https://github.com/sannies/mp4parser
//imports
import com.coremedia.iso.IsoFile;

//get the duration of video
    private double GetVideoDuration () throws IOException {
        double DurationVideo = 0;
        //pathPlaying is a string this format: "src/videos/video.mp4"
        IsoFile isoFile = new IsoFile(pathPlaying);
        DurationVideo = (double)
                isoFile.getMovieBox().getMovieHeaderBox().getDuration() /
                isoFile.getMovieBox().getMovieHeaderBox().getTimescale();
        return DurationVideo;
    }
Dan
  • 171
  • 1
  • 6
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Clijsters Nov 21 '17 at 11:00
  • Im sorry, I set the explanation as comment in the function, but I will write then next time :) – Dan Aug 19 '19 at 11:01