Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to get some information of an mp4 file(such as duration,file size,title and so on) , I try to use Shell32 , but it does not support mp4 format , I have found some dlls in the website , but all they are not COM component , then I try to use IBMToolkitForMpeg4SDK.jar , and then meets some problems Exception in thread "main" java.net.MalformedURLException: Unrecognized extension of file:

e:\1.mpeg at I4g.d(Unknown Source) at I4g.a(Unknown Source) at I4i.a(Unknown Source) at I4b.a(Unknown Source) at I4b.open(Unknown Source) at AVgenSample.getMp4Duration(AVgenSample.java:48) at AVgenSample.main(AVgenSample.java:36)


So I want to get the information of mp4 file , anyone have any methods , please help!Thsnks all!
Posted
Updated 17-May-12 18:04pm
v2

1 solution


1.download DirectShowLib,I use the version DirectShowLibV2-1
its website:

http://directshownet.sourceforge.net/
and download page:
http://sourceforge.net/projects/directshownet/files/
2.using the lib in c#
3.code using MediaDet class

reference:


C#
using System.Runtime.InteropServices;
using DirectShowLib;
using DirectShowLib.DES;


code:

C#
public static string GetDuration(string fileName)
            {
                var mediaDet = (IMediaDet)new MediaDet();
                DsError.ThrowExceptionForHR(mediaDet.put_Filename(fileName));
    
                // find the video stream in the file
                int index;
                var type = Guid.Empty;
                for (index = 0; index < 1000 && type != MediaType.Video; index++)
                {
                    mediaDet.put_CurrentStream(index);
                    mediaDet.get_StreamType(out type);
                }
    
                // retrieve some measurements from the video
                double frameRate;
                mediaDet.get_FrameRate(out frameRate);
    
                var mediaType = new AMMediaType();
                mediaDet.get_StreamMediaType(mediaType);
                var videoInfo = (VideoInfoHeader)Marshal.PtrToStructure(mediaType.formatPtr, typeof(VideoInfoHeader));
                DsUtils.FreeAMMediaType(mediaType);
                var width = videoInfo.BmiHeader.Width;
                var height = videoInfo.BmiHeader.Height;
    
                double mediaLength;
                mediaDet.get_StreamLength(out mediaLength);
                //var frameCount = (int)(frameRate * mediaLength);
                //var duration = frameCount / frameRate;
    
                return mediaLength.ToString();
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900