C#使用MediaInfo查看媒体信息
网上关于MediaInfo相关的使用经验很少,而且官方Demo介绍也只是输出INFO,没有每一项信息的调用参数,今天,将自己总结的经验备份下,省得以后又得找。
1.将MediaInfo.dll放入可执行目录。
2.将官网Demo里的MediaInfoDLL.cs放入项目中。
View MediaInfoDLL.cs Code
1 using System;
2 using System.Runtime.InteropServices;
3
4 namespace PlayVideoTest
5 {
6 public enum StreamKind
7 {
8 General,
9 Video,
10 Audio,
11 Text,
12 Chapters,
13 Image
14 }
15
16 public enum InfoKind
17 {
18 Name,
19 Text,
20 Measure,
21 Options,
22 NameText,
23 MeasureText,
24 Info,
25 HowTo
26 }
27
28 public enum InfoOptions
29 {
30 ShowInInform,
31 Support,
32 ShowInSupported,
33 TypeOfValue
34 }
35
36 public enum InfoFileOptions
37 {
38 FileOption_Nothing = 0x00,
39 FileOption_NoRecursive = 0x01,
40 FileOption_CloseAll = 0x02,
41 FileOption_Max = 0x04
42 };
43
44
45 public class MediaInfo
46 {
47 //Import of DLL functions. DO NOT USE until you know what you do (MediaInfo DLL do NOT use CoTaskMemAlloc to allocate memory)
48 [DllImport("MediaInfo.dll")]
49 private static extern IntPtr MediaInfo_New();
50 [DllImport("MediaInfo.dll")]
51 private static extern void MediaInfo_Delete(IntPtr Handle);
52 [DllImport("MediaInfo.dll")]
53 private static extern IntPtr MediaInfo_Open(IntPtr Handle, [MarshalAs(UnmanagedType.LPWStr)] string FileName);
54 [DllImport("MediaInfo.dll")]
55 private static extern IntPtr MediaInfoA_Open(IntPtr Handle, IntPtr FileName);
56 [DllImport("MediaInfo.dll")]
57 private static extern IntPtr MediaInfo_Open_Buffer_Init(IntPtr Handle, Int64 File_Size, Int64 File_Offset);
58 [DllImport("MediaInfo.dll")]
59 private static extern IntPtr MediaInfoA_Open(IntPtr Handle, Int64 File_Size, Int64 File_Offset);
60 [DllImport("MediaInfo.dll")]
61 private static extern IntPtr MediaInfo_Open_Buffer_Continue(IntPtr Handle, IntPtr Buffer, IntPtr Buffer_Size);
62 [DllImport("MediaInfo.dll")]
63 private static extern IntPtr MediaInfoA_Open_Buffer_Continue(IntPtr Handle, Int64 File_Size, byte[] Buffer, IntPtr Buffer_Size);
64 [DllImport("MediaInfo.dll")]
65 private static extern Int64 MediaInfo_Open_Buffer_Continue_GoTo_Get(IntPtr Handle);
66 [DllImport("MediaInfo.dll")]
67 private static extern Int64 MediaInfoA_Open_Buffer_Continue_GoTo_Get(IntPtr Handle);
68 [DllImport("MediaInfo.dll")]
69 private static extern IntPtr MediaInfo_Open_Buffer_Finalize(IntPtr Handle);
70 [DllImport("MediaInfo.dll")]
71 private static extern IntPtr MediaInfoA_Open_Buffer_Finalize(IntPtr Handle);
72 [DllImport("MediaInfo.dll")]
73 private static extern void MediaInfo_Close(IntPtr Handle);
74 [DllImport("MediaInfo.dll")]
75 private static extern IntPtr MediaInfo_Inform(IntPtr Handle, IntPtr Reserved);
76 [DllImport("MediaInfo.dll")]
77 private static extern IntPtr MediaInfoA_Inform(IntPtr Handle, IntPtr Reserved);
78 [DllImport("MediaInfo.dll")]
79 private static extern IntPtr MediaInfo_GetI(IntPtr Handle, IntPtr StreamKind, IntPtr StreamNumber, IntPtr Parameter, IntPtr KindOfInfo);
80 [DllImport("MediaInfo.dll")]
81 private static extern IntPtr MediaInfoA_GetI(IntPtr Handle, IntPtr StreamKind, IntPtr StreamNumber, IntPtr Parameter, IntPtr KindOfInfo);
82 [DllImport("MediaInfo.dll")]
83 private static extern IntPtr MediaInfo_Get(IntPtr Handle, IntPtr StreamKind, IntPtr StreamNumber, [MarshalAs(UnmanagedType.LPWStr)] string Parameter, IntPtr KindOfInfo, IntPtr KindOfSearch);
84 [DllImport("MediaInfo.dll")]
85 private static extern IntPtr MediaInfoA_Get(IntPtr Handle, IntPtr StreamKind, IntPtr StreamNumber, IntPtr Parameter, IntPtr KindOfInfo, IntPtr KindOfSearch);
86 [DllImport("MediaInfo.dll")]
87 private static extern IntPtr MediaInfo_Option(IntPtr Handle, [MarshalAs(UnmanagedType.LPWStr)] string Option, [MarshalAs(UnmanagedType.LPWStr)] string Value);
88 [DllImport("MediaInfo.dll")]
89 private static extern IntPtr MediaInfoA_Option(IntPtr Handle, IntPtr Option, IntPtr Value);
90 [DllImport("MediaInfo.dll")]
91 private static extern IntPtr MediaInfo_State_Get(IntPtr Handle);
92 [DllImport("MediaInfo.dll")]
93 private static extern IntPtr MediaInfo_Count_Get(IntPtr Handle, IntPtr StreamKind, IntPtr StreamNumber);
94
95 //MediaInfo class
96 public MediaInfo()
97 {
98 Handle = MediaInfo_New();
99 if (Environment.OSVersion.ToString().IndexOf("Windows")==-1)
100 MustUseAnsi=true;
101 else
102 MustUseAnsi=false;
103 }
104 ~MediaInfo() { MediaInfo_Delete(Handle); }
105 public int Open(String FileName)
106 {
107 if (MustUseAnsi)
108 {
109 IntPtr FileName_Ptr = Marshal.StringToHGlobalAnsi(FileName);
110 int ToReturn = (int)MediaInfoA_Open(Handle, FileName_Ptr);
111 Marshal.FreeHGlobal(FileName_Ptr);
112 return ToReturn;
113 }
114 else
115 return (int)MediaInfo_Open(Handle, FileName);
116 }
117 public int Open_Buffer_Init(Int64 File_Size, Int64 File_Offset)
118 {
119 return (int)MediaInfo_Open_Buffer_Init(Handle, File_Size, File_Offset);
120 }
121 public int Open_Buffer_Continue(IntPtr Buffer, IntPtr Buffer_Size)
122 {
123 return (int)MediaInfo_Open_Buffer_Continue(Handle, Buffer, Buffer_Size);
124 }
125 public Int64 Open_Buffer_Continue_GoTo_Get()
126 {
127 return (int)MediaInfo_Open_Buffer_Continue_GoTo_Get(Handle);
128 }
129 public int Open_Buffer_Finalize()
130 {
131 return (int)MediaInfo_Open_Buffer_Finalize(Handle);
132 }
133 public void Close() { MediaInfo_Close(Handle); }
134 public String Inform()
135 {
136 if (MustUseAnsi)
137 return Marshal.PtrToStringAnsi(MediaInfoA_Inform(Handle, (IntPtr)0));
138 else
139 return Marshal.PtrToStringUni(MediaInfo_Inform(Handle, (IntPtr)0));
140 }
141 public String Get(StreamKind StreamKind, int StreamNumber, String Parameter, InfoKind KindOfInfo, InfoKind KindOfSearch)
142 {
143 if (MustUseAnsi)
144 {
145 IntPtr Parameter_Ptr=Marshal.StringToHGlobalAnsi(Parameter);
146 String ToReturn=Marshal.PtrToStringAnsi(MediaInfoA_Get(Handle, (IntPtr)StreamKind, (IntPtr)StreamNumber, Parameter_Ptr, (IntPtr)KindOfInfo, (IntPtr)KindOfSearch));
147 Marshal.FreeHGlobal(Parameter_Ptr);
148 return ToReturn;
149 }
150 else
151 return Marshal.PtrToStringUni(MediaInfo_Get(Handle, (IntPtr)StreamKind, (IntPtr)StreamNumber, Parameter, (IntPtr)KindOfInfo, (IntPtr)KindOfSearch));
152 }
153 public String Get(StreamKind StreamKind, int StreamNumber, int Parameter, InfoKind KindOfInfo)
154 {
155 if (MustUseAnsi)
156 return Marshal.PtrToStringAnsi(MediaInfoA_GetI(Handle, (IntPtr)StreamKind, (IntPtr)StreamNumber, (IntPtr)Parameter, (IntPtr)KindOfInfo));
157 else
158 return Marshal.PtrToStringUni(MediaInfo_GetI(Handle, (IntPtr)StreamKind, (IntPtr)StreamNumber, (IntPtr)Parameter, (IntPtr)KindOfInfo));
159 }
160 public String Option(String Option, String Value)
161 {
162 if (MustUseAnsi)
163 {
164 IntPtr Option_Ptr=Marshal.StringToHGlobalAnsi(Option);
165 IntPtr Value_Ptr=Marshal.StringToHGlobalAnsi(Value);
166 String ToReturn=Marshal.PtrToStringAnsi(MediaInfoA_Option(Handle, Option_Ptr, Value_Ptr));
167 Marshal.FreeHGlobal(Option_Ptr);
168 Marshal.FreeHGlobal(Value_Ptr);
169 return ToReturn;
170 }
171 else
172 return Marshal.PtrToStringUni(MediaInfo_Option(Handle, Option, Value));
173 }
174 public int State_Get() { return (int)MediaInfo_State_Get(Handle); }
175 public int Count_Get(StreamKind StreamKind, int StreamNumber) { return (int)MediaInfo_Count_Get(Handle, (IntPtr)StreamKind, (IntPtr)StreamNumber); }
176 private IntPtr Handle;
177 private bool MustUseAnsi;
178
179 //Default values, if you know how to set default values in C#, say me
180 public String Get(StreamKind StreamKind, int StreamNumber, String Parameter, InfoKind KindOfInfo) { return Get(StreamKind, StreamNumber, Parameter, KindOfInfo, InfoKind.Name); }
181 public String Get(StreamKind StreamKind, int StreamNumber, String Parameter) { return Get(StreamKind, StreamNumber, Parameter, InfoKind.Text, InfoKind.Name); }
182 public String Get(StreamKind StreamKind, int StreamNumber, int Parameter) { return Get(StreamKind, StreamNumber, Parameter, InfoKind.Text); }
183 public String Option(String Option_) { return Option(Option_, ""); }
184 public int Count_Get(StreamKind StreamKind) { return Count_Get(StreamKind, -1); }
185 }
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203 public class MediaInfoList
204 {
205 //Import of DLL functions. DO NOT USE until you know what you do (MediaInfo DLL do NOT use CoTaskMemAlloc to allocate memory)
206 [DllImport("MediaInfo.dll")]
207 private static extern IntPtr MediaInfoList_New();
208 [DllImport("MediaInfo.dll")]
209 private static extern void MediaInfoList_Delete(IntPtr Handle);
210 [DllImport("MediaInfo.dll")]
211 private static extern IntPtr MediaInfoList_Open(IntPtr Handle, [MarshalAs(UnmanagedType.LPWStr)] string FileName, IntPtr Options);
212 [DllImport("MediaInfo.dll")]
213 private static extern void MediaInfoList_Close(IntPtr Handle, IntPtr FilePos);
214 [DllImport("MediaInfo.dll")]
215 private static extern IntPtr MediaInfoList_Inform(IntPtr Handle, IntPtr FilePos, IntPtr Reserved);
216 [DllImport("MediaInfo.dll")]
217 private static extern IntPtr MediaInfoList_GetI(IntPtr Handle, IntPtr FilePos, IntPtr StreamKind, IntPtr StreamNumber, IntPtr Parameter, IntPtr KindOfInfo);
218 [DllImport("MediaInfo.dll")]
219 private static extern IntPtr MediaInfoList_Get(IntPtr Handle, IntPtr FilePos, IntPtr StreamKind, IntPtr StreamNumber, [MarshalAs(UnmanagedType.LPWStr)] string Parameter, IntPtr KindOfInfo, IntPtr KindOfSearch);
220 [DllImport("MediaInfo.dll")]
221 private static extern IntPtr MediaInfoList_Option(IntPtr Handle, [MarshalAs(UnmanagedType.LPWStr)] string Option, [MarshalAs(UnmanagedType.LPWStr)] string Value);
222 [DllImport("MediaInfo.dll")]
223 private static extern IntPtr MediaInfoList_State_Get(IntPtr Handle);
224 [DllImport("MediaInfo.dll")]
225 private static extern IntPtr MediaInfoList_Count_Get(IntPtr Handle, IntPtr FilePos, IntPtr StreamKind, IntPtr StreamNumber);
226
227 //MediaInfo class
228 public MediaInfoList() { Handle = MediaInfoList_New(); }
229 ~MediaInfoList() { MediaInfoList_Delete(Handle); }
230 public int Open(String FileName, InfoFileOptions Options) { return (int)MediaInfoList_Open(Handle, FileName, (IntPtr)Options); }
231 public void Close(int FilePos) { MediaInfoList_Close(Handle, (IntPtr)FilePos); }
232 public String Inform(int FilePos) { return Marshal.PtrToStringUni(MediaInfoList_Inform(Handle, (IntPtr)FilePos, (IntPtr)0)); }
233 public String Get(int FilePos, StreamKind StreamKind, int StreamNumber, String Parameter, InfoKind KindOfInfo, InfoKind KindOfSearch) { return Marshal.PtrToStringUni(MediaInfoList_Get(Handle, (IntPtr)FilePos, (IntPtr)StreamKind, (IntPtr)StreamNumber, Parameter, (IntPtr)KindOfInfo, (IntPtr)KindOfSearch)); }
234 public String Get(int FilePos, StreamKind StreamKind, int StreamNumber, int Parameter, InfoKind KindOfInfo) { return Marshal.PtrToStringUni(MediaInfoList_GetI(Handle, (IntPtr)FilePos, (IntPtr)StreamKind, (IntPtr)StreamNumber, (IntPtr)Parameter, (IntPtr)KindOfInfo)); }
235 public String Option(String Option, String Value) { return Marshal.PtrToStringUni(MediaInfoList_Option(Handle, Option, Value)); }
236 public int State_Get() { return (int)MediaInfoList_State_Get(Handle); }
237 public int Count_Get(int FilePos, StreamKind StreamKind, int StreamNumber) { return (int)MediaInfoList_Count_Get(Handle, (IntPtr)FilePos, (IntPtr)StreamKind, (IntPtr)StreamNumber); }
238 private IntPtr Handle;
239
240 //Default values, if you know how to set default values in C#, say me
241 public void Open(String FileName) { Open(FileName, 0); }
242 public void Close() { Close(-1); }
243 public String Get(int FilePos, StreamKind StreamKind, int StreamNumber, String Parameter, InfoKind KindOfInfo) { return Get(FilePos, StreamKind, StreamNumber, Parameter, KindOfInfo, InfoKind.Name); }
244 public String Get(int FilePos, StreamKind StreamKind, int StreamNumber, String Parameter) { return Get(FilePos, StreamKind, StreamNumber, Parameter, InfoKind.Text, InfoKind.Name); }
245 public String Get(int FilePos, StreamKind StreamKind, int StreamNumber, int Parameter) { return Get(FilePos, StreamKind, StreamNumber, Parameter, InfoKind.Text); }
246 public String Option(String Option_) { return Option(Option_, ""); }
247 public int Count_Get(int FilePos, StreamKind StreamKind) { return Count_Get(FilePos, StreamKind, -1); }
248 }
249
250 } //NameSpace
3.接下来是调用了。
1 public bool LoadVideoInfo()
2{
3 MediaInfo MI = new MediaInfo();
4 MI.Open("C:\\test.avi");
5 string width =Convert.ToInt32(MI.Get(StreamKind.Video, 0, "Width"));//视频width
6 string height =Convert.ToInt32(MI.Get(StreamKind.Video, 0, "Height"));
7 MI.Close();
8 }
由MI.Get(StreamKind.Video, 0, "Width")这个函数调用方式可知,我们想查询视频宽度,直接使用width参数就行了,但是视频、音频有大量的参数,我们不可以都猜的到,网上反正我没找到这些参数的介绍,幸好找到了一个方法可以把这些方法遍历出来,参数出来了,就好办了,不明白意思的话google下,基本上就可以把全部参数弄懂了。
1 // 遍历Video所有可有参数
2 //如果要遍历Audio的参数,StreamKind.Video换成StreamKind.Audio即可,General同理。
3 string parameter="";//存放所有参数
4 string tempstr;int i=0;
5 while (true)
6 {
7 tempstr = MI.Get(StreamKind.Video, 0, i++, InfoKind.Name);
8 if (tempstr == "")
9 {
10 break;
11 }
12 parameter += "\r\n" + tempstr;
13 }