How with JavaScript & jQuery or PHP get media file duration eg video.mp4

how with JavaScript & jQuery or PHP get media file duration eg video.mp4?

Just about any server-side script should be able to get at least the file size. Not sure about duration, though. It might be in the file meta-data, if PHP can access that.

Found a SO post on this same question.



For HTML5 you should be able to use the video tag’s duration property, once the file’s metadata is loaded.

var myVideoPlayer = document.getElementById('video_player');
myVideoPlayer.addEventListener('loadedmetadata', function() {

This gets triggered when the browser received all the meta data from the video.


HTML5 is nice, but I (personally) would hold off using HTML5 unless I had in place code that would gracefully degrade if the video tag isn’t supported (big differences between IE8, IE9, IE10, FireFox, etc.)

And I’m not sure (the OPs English doesn’t seem native), but I suspect the OP wanted the information prior to the video being loaded. Just guessing.


Yes, I want to list the videos & duration without yet these go to player… Only server side option?

Yes, only server-side. I’m assuming the media file will be on the server. Only a server-side script would be able to access the file and get that information.

Unless the media file in question is on another server and a different domain (like, or similar.) In that case, the hosting service would have to do it.




Sure, but that entirely depends on what you are working on, who it is intended for and so on.


I’m not quite sure what the OP’s reply means.
However, to demonstrate how it would work client-side, here is a demo.
Maybe it helps, maybe not :slight_smile:

It does require a bit of guessing. :smiley: OP wants to list the title (or filename) and the duration (assuming in min:sec format) of each media file (MP3, MP4, WMV, etc.) - sort of like on YouTube - each thumbnail displays a frame of the video, with a small duration displayed in the lower right corner. OP did not say anything about thumbnails, so I’m assuming this will probably be a text display of file/duration for a user to click on so said user knows what they are clicking on.