[英]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);
實際上,您的代碼返回的時間以微秒為單位。 如果要獲取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()
如果持續時間以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();
}
//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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.