如何使用 xuggler 获取视频的持续时间长度

how to get duration length of the Video using xuggler

此代码以长格式读取持续时间,但当它转换为带时间格式的日期时 'hh:mm:ss' 它给出不同的值,视频长度为 00:08:07。这段代码有什么问题

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);  

如果您在 milliseconds

中获得持续时间
long ms = xxxxx;

您可以将其转换为 hh:mm:ss 格式,如下所示:

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);

我使用 IBMPlayerForMpeg4SDK-1.0 获得了正确的视频持续时间。0.jar 通过使用以下代码,它的工作很好

/**
     * 
     * @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();
    }

实际上您的代码 returns 时间以微秒为单位。如果你想得到java.util.Duration,你应该使用:

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);
}

为了格式化结果时间,你可以使用来自@SASM 的代码,他的代码的输入应该是

long ms = getVideoDuration("your_path_here").toMillis()
//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;
    }