How to get the total duration of a video file in Java

by kpkmd54461 on 2012-02-28 18:33:58

Here is the translation of your text into English:

For example, getting the duration of an FLV file — if a video can play for 2 minutes in total, then this 2 minutes is the duration. How to get it and implement it in Java?

---

### Problem Supplement:

a881127b wrote the solution: Call a DLL file. As long as you find a DLL that can parse the playback duration well, then call it using JNI (Java Native Interface). You can use C++ or Delphi; as long as it works, it's OK. Place the DLL in the BIN directory.

```java

public class CtrlFtpFile {

// Load the Dll

static {

System.loadLibrary("GetPlayTime");

}

public native String getMediaTime(String arg);

public static void main(String[] args) {

CtrlFtpFile hw = new CtrlFtpFile();

// System.out.println(hw.getMediaTime("D:\\TDDOWNLOAD\\小强.mp3"));

System.out.println(hw.getMediaTime("file:///G:/tomcat/apache-tomcat-6.0.18/webapps/mbs/ftp/0/real/B1224487037153.mpeg"));

// file:///G:\tomcat\apache-tomcat-6.0.18\webapps\mbs\ftp\0/real/B1224487037153.mpeg

}

}

```

Attachment link: [http://a881127b.iteye.com/blog/724516](http://a881127b.iteye.com/blog/724516)

You could explain it more specifically with step-by-step instructions because I am still unclear and couldn't make it work after trying for a while.

---

### Solution Approach:

Call the DLL file. The member name cannot be the same as its enclosing type. As long as you find a DLL that can parse the playback duration well, then call it using JNI (Java Native Interface). You can use C++ or Delphi; as long as it works, it's OK. Place the DLL in the BIN directory.

```java

public class CtrlFtpFile {

// Load the Dll

static {

System.loadLibrary("GetPlayTime");

}

public native String getMediaTime(String arg);

public static void main(String[] args) {

CtrlFtpFile hw = new CtrlFtpFile();

// System.out.println(hw.getMediaTime("D:\\TDDOWNLOAD\\小强.mp3"));

System.out.println(hw.getMediaTime("file:///G:/tomcat/apache-tomcat-6.0.18/webapps/mbs/ftp/0/real/B1224487037153.mpeg"));

// file:///G:\tomcat\apache-tomcat-6.0.18\webapps\mbs\ftp\0/real/B1224487037153.mpeg

}

}

```

Attachment link: [http://a881127b.iteye.com/blog/724516](http://a881127b.iteye.com/blog/724516)

---

### Reference:

For obtaining the resolution and playback duration of a video file in PowerBuilder (PB), refer to: [How PB retrieves the resolution and playback duration of a video file](http://www.myexception.cn/pb/240155.html).

Relevant articles on related topics:

- When using Uploadify version 2.1.2, there is always an HTTP Error. Does anyone know how to resolve it?

- How to display Flash files or hidden Divs in a popup window in Ext for Air.

- Chinese data read from MySQL appears garbled when passed to the EXTJS textfield component on the front end. How should this be handled?