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

时间:2016-03-29 13:54:48

标签: java xuggler

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

operator<<

4 个答案:

答案 0 :(得分:2)

实际上你的代码以微秒为单位返回时间。如果你想获得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()

答案 1 :(得分:1)

如果您在[{1}}

中获得持续时间
""

您可以将其转换为$var格式,如下所示:

milliseconds

答案 2 :(得分:1)

我使用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();
    }

答案 3 :(得分:0)

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