Using SHELL32 to read AV file properties

Source: Internet
Author: User

Due to the need to use real-time read audio and video files (MP3, WMA, WMV ...) Playback time length of the function, the results of the search are:

(1) Hard code Analysis of audio-visual files, the need to analyze various media formats, the most expensive;

(2) The use of Wmlib SDK, the need to familiarize themselves with the various SDK interface, and different versions of the WM interface, the cost of the second;

(3) using the COM interface of the system SHELL32, directly accessing the media stylistic properties, taking its specific content, the cost is minimal.

It is clear that the 3rd option is the fastest and immediately fenced:

① reference Shell32 the underlying interface c:\windows\system32\ Shell32.dll,vs automatically converted to Interop.Shell32.dll (note: 64-bit system and 32-bit system generated Interop.Shell32.dll different)

② encoding Read playback time length:

/// <summary>
/// 获取媒体播放时间长度,格式00:00:00。
/// </summary>
/// <param name="path">媒体路径</param>
/// <returns>播放时间长度</returns>
public static string GetMediaDuration(this string path)
{
 try
 {
  Shell32.Shell shell = new Shell32.ShellClass();
  Shell32.Folder folder = shell.NameSpace(path.Substring(0, path.LastIndexOf("\\")));
  Shell32.FolderItem folderItem = folder.ParseName(path.Substring(path.LastIndexOf("\\") + 1));
  return folder.GetDetailsOf(folderItem, 21);
 }
 catch (Exception ex)
 {
  ex.Error();
  return null;
 }
}

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.