C # Get video file playback length

Source: Internet
Author: User

The following two methods support only partial video format, general format mp3,wma, etc.

1. Using Shell32

Add Reference, select Microsoft Shell Controls and Automation Reference in COM

        <summary>//Length minutes (support mp4?)        </summary>//<param name= "path" ></param>//<returns></returns> public static string Getmediatimelenminute (string path) {try {Shell32.shell                Shell = new Shell32.shell (); File path Shell32.folder Folder = Shell. NameSpace (path. Substring (0, path.                LastIndexOf ("\ \"))); File name Shell32.folderitem FolderItem = folder. ParseName (path. Substring (path.                LastIndexOf ("\ \") + 1); if (Environment.OSVersion.Version.Major >= 6) {String medialength = folder.                    Getdetailsof (FolderItem, 27);                return medialength; } else {String medialength = folder.                    Getdetailsof (FolderItem, 21);                return medialength;     }       } catch (Exception ex) {return null;}        }///<summary>//Length seconds (support mp4?)        </summary>//<param name= "path" ></param>//<returns></returns> public static int Getmediatimelensecond (string path) {try {Shell32.shell she                ll = new Shell32.shell (); File path Shell32.folder Folder = Shell. NameSpace (path. Substring (0, path.                LastIndexOf ("\ \"))); File name Shell32.folderitem FolderItem = folder. ParseName (path. Substring (path.                LastIndexOf ("\ \") + 1);                String Len; if (Environment.OSVersion.Version.Major >= 6) {len = folder.                Getdetailsof (FolderItem, 27); } else {len = folder.                Getdetailsof (FolderItem, 21); } string[] str = len.                Split (new char[] {': '});                int sum = 0; sum = Int. Parse (str[0]) * * + int. Parse (str[1]) * + int.                Parse (str[2]);            return sum;        } catch (Exception ex) {return 0;} }

  

1. Using DirectShowLib.dll

You can search for directshowlib in NuGet to add references

    public static string Getmediatimelen (string path) {var mediadet = (imediadet) new Mediadet ();        DSERROR.THROWEXCEPTIONFORHR (path);//Find the video stream in the Fileint index;        var type = Guid.Empty; for (int index = 0; index < + && type! = Mediatype.video; index++) {mediadet.put_currents            Tream (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;//This is the video length, unit seconds mediadet.get_streamlength (out medialength);     var framecount = (int) (framerate * medialength);        var duration = Framecount/framerate;    Return ""; }

  

C # Get video file playback length

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.