在xaml中,我具有带有video.mp4源的简单MediaElement mediaElement控件。 视频时长约为4.3秒。
当mediaElement.MediaOpened被触发时,我正在尝试获取视频时长。
private void mediaElement_MediaOpened(object sender, RoutedEventArgs e)
{
double TotalTime = mediaElement.NaturalDuration.TimeSpan.TotalMilliseconds;
Console.WriteLine("Total video time: {0}", TotalTime);
}
控制台返回“视频总时间:4000”。 如何获得确切的视频时长?
答案 0 :(得分:0)
我的项目中也遇到了同样的问题,无法找到WPF的内置方法来获取准确的毫秒数。因此,我们必须转到外部库。
出于某些原因,我选择使用Javi.MediaInfo,其中一些原因是它是MIT许可的MediaInfoLib包装器(BSD 2子句)。 Nuget上的其他一些包装器看起来好像是MIT /类似许可证,但是如果您查看源代码,则它们使用的是GPL代码,例如MP-MediaInfo。要使用Javi.MediaInfo
,我还必须从NuGet安装MediaInfo.Native v19.4.0(本地MediaInfo.dll
)。
在尝试使用Javi.MediaInfo
功能时,我遇到了Options
的问题。结果发现MediaInfoWrapper
类使用的旧函数声明与最新的MediaInfo.dll
不匹配。为了解决这个问题,我对MediaInfo.cs
和MediaInfoWrapper.cs
进行了一些调整(请参见下文)。我使用19.4.0 DLL code download获得了后者。我还将整个Javi.MediaInfo
源代码放置在我的源代码中,以避免以前的目标.NET Standard 2
出现问题,并且我的项目没有这样做(如果使用NuGet,它会给我重复的DLL参考警告)。 / p>
MediaInfoWrapper.cs
/* Copyright (c) MediaArea.net SARL. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license that can
* be found in the License.html file in the root of the source tree.
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// Microsoft Visual C# wrapper for MediaInfo Library
// See MediaInfo.h for help
//
// To make it working, you must put MediaInfo.Dll
// in the executable folder
//
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// from https://mediaarea.net/en/MediaInfo/Download/Windows -- for version 19.4.0; modified slightly
using System;
using System.Runtime.InteropServices;
#pragma warning disable 1591 // Disable XML documentation warnings
namespace MediaInfoLib
{
public enum StreamKind
{
General,
Video,
Audio,
Text,
Other,
Image,
Menu,
}
public enum InfoKind
{
Name,
Text,
Measure,
Options,
NameText,
MeasureText,
Info,
HowTo
}
public enum InfoOptions
{
ShowInInform,
Support,
ShowInSupported,
TypeOfValue
}
public enum InfoFileOptions
{
FileOption_Nothing = 0x00,
FileOption_NoRecursive = 0x01,
FileOption_CloseAll = 0x02,
FileOption_Max = 0x04
};
public enum Status
{
None = 0x00,
Accepted = 0x01,
Filled = 0x02,
Updated = 0x04,
Finalized = 0x08,
}
public class MediaInfo : IDisposable
{
//Import of DLL functions. DO NOT USE until you know what you do (MediaInfo DLL do NOT use CoTaskMemAlloc to allocate memory)
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfo_New();
[DllImport("MediaInfo.dll")]
private static extern void MediaInfo_Delete(IntPtr Handle);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfo_Open(IntPtr Handle, [MarshalAs(UnmanagedType.LPWStr)] string FileName);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfoA_Open(IntPtr Handle, IntPtr FileName);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfo_Open_Buffer_Init(IntPtr Handle, Int64 File_Size, Int64 File_Offset);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfoA_Open(IntPtr Handle, Int64 File_Size, Int64 File_Offset);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfo_Open_Buffer_Continue(IntPtr Handle, IntPtr Buffer, IntPtr Buffer_Size);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfoA_Open_Buffer_Continue(IntPtr Handle, Int64 File_Size, byte[] Buffer, IntPtr Buffer_Size);
[DllImport("MediaInfo.dll")]
private static extern Int64 MediaInfo_Open_Buffer_Continue_GoTo_Get(IntPtr Handle);
[DllImport("MediaInfo.dll")]
private static extern Int64 MediaInfoA_Open_Buffer_Continue_GoTo_Get(IntPtr Handle);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfo_Open_Buffer_Finalize(IntPtr Handle);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfoA_Open_Buffer_Finalize(IntPtr Handle);
[DllImport("MediaInfo.dll")]
private static extern void MediaInfo_Close(IntPtr Handle);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfo_Inform(IntPtr Handle, IntPtr Reserved);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfoA_Inform(IntPtr Handle, IntPtr Reserved);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfo_GetI(IntPtr Handle, IntPtr StreamKind, IntPtr StreamNumber, IntPtr Parameter, IntPtr KindOfInfo);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfoA_GetI(IntPtr Handle, IntPtr StreamKind, IntPtr StreamNumber, IntPtr Parameter, IntPtr KindOfInfo);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfo_Get(IntPtr Handle, IntPtr StreamKind, IntPtr StreamNumber, [MarshalAs(UnmanagedType.LPWStr)] string Parameter, IntPtr KindOfInfo, IntPtr KindOfSearch);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfoA_Get(IntPtr Handle, IntPtr StreamKind, IntPtr StreamNumber, IntPtr Parameter, IntPtr KindOfInfo, IntPtr KindOfSearch);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfo_Option(IntPtr Handle, [MarshalAs(UnmanagedType.LPWStr)] string Option, [MarshalAs(UnmanagedType.LPWStr)] string Value);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfoA_Option(IntPtr Handle, IntPtr Option, IntPtr Value);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfo_State_Get(IntPtr Handle);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfo_Count_Get(IntPtr Handle, IntPtr StreamKind, IntPtr StreamNumber);
public string[] InfoParametersCSV { get; }
//MediaInfo class
public MediaInfo()
{
try
{
Handle = MediaInfo_New();
this.InfoParametersCSV = this.Option("Info_Parameters_CSV").Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
}
catch
{
Handle = (IntPtr)0;
}
if (Environment.OSVersion.ToString().IndexOf("Windows") == -1)
MustUseAnsi = true;
else
MustUseAnsi = false;
}
~MediaInfo() { if (Handle == (IntPtr)0) return; MediaInfo_Delete(Handle); }
public int Open(String FileName)
{
if (Handle == (IntPtr)0)
return 0;
if (MustUseAnsi)
{
IntPtr FileName_Ptr = Marshal.StringToHGlobalAnsi(FileName);
int ToReturn = (int)MediaInfoA_Open(Handle, FileName_Ptr);
Marshal.FreeHGlobal(FileName_Ptr);
return ToReturn;
}
else
return (int)MediaInfo_Open(Handle, FileName);
}
public int Open_Buffer_Init(Int64 File_Size, Int64 File_Offset)
{
if (Handle == (IntPtr)0) return 0; return (int)MediaInfo_Open_Buffer_Init(Handle, File_Size, File_Offset);
}
public int Open_Buffer_Continue(IntPtr Buffer, IntPtr Buffer_Size)
{
if (Handle == (IntPtr)0) return 0; return (int)MediaInfo_Open_Buffer_Continue(Handle, Buffer, Buffer_Size);
}
public Int64 Open_Buffer_Continue_GoTo_Get()
{
if (Handle == (IntPtr)0) return 0; return (Int64)MediaInfo_Open_Buffer_Continue_GoTo_Get(Handle);
}
public int Open_Buffer_Finalize()
{
if (Handle == (IntPtr)0) return 0; return (int)MediaInfo_Open_Buffer_Finalize(Handle);
}
public void Close() { if (Handle == (IntPtr)0) return; MediaInfo_Close(Handle); }
public String Inform()
{
if (Handle == (IntPtr)0)
return "Unable to load MediaInfo library";
if (MustUseAnsi)
return Marshal.PtrToStringAnsi(MediaInfoA_Inform(Handle, (IntPtr)0));
else
return Marshal.PtrToStringUni(MediaInfo_Inform(Handle, (IntPtr)0));
}
public String Get(StreamKind StreamKind, int StreamNumber, String Parameter, InfoKind KindOfInfo, InfoKind KindOfSearch)
{
if (Handle == (IntPtr)0)
return "Unable to load MediaInfo library";
if (MustUseAnsi)
{
IntPtr Parameter_Ptr = Marshal.StringToHGlobalAnsi(Parameter);
String ToReturn = Marshal.PtrToStringAnsi(MediaInfoA_Get(Handle, (IntPtr)StreamKind, (IntPtr)StreamNumber, Parameter_Ptr, (IntPtr)KindOfInfo, (IntPtr)KindOfSearch));
Marshal.FreeHGlobal(Parameter_Ptr);
return ToReturn;
}
else
return Marshal.PtrToStringUni(MediaInfo_Get(Handle, (IntPtr)StreamKind, (IntPtr)StreamNumber, Parameter, (IntPtr)KindOfInfo, (IntPtr)KindOfSearch));
}
public String Get(StreamKind StreamKind, int StreamNumber, int Parameter, InfoKind KindOfInfo)
{
if (Handle == (IntPtr)0)
return "Unable to load MediaInfo library";
if (MustUseAnsi)
return Marshal.PtrToStringAnsi(MediaInfoA_GetI(Handle, (IntPtr)StreamKind, (IntPtr)StreamNumber, (IntPtr)Parameter, (IntPtr)KindOfInfo));
else
return Marshal.PtrToStringUni(MediaInfo_GetI(Handle, (IntPtr)StreamKind, (IntPtr)StreamNumber, (IntPtr)Parameter, (IntPtr)KindOfInfo));
}
public String Option(String Option, String Value)
{
if (Handle == (IntPtr)0)
return "Unable to load MediaInfo library";
if (MustUseAnsi)
{
IntPtr Option_Ptr = Marshal.StringToHGlobalAnsi(Option);
IntPtr Value_Ptr = Marshal.StringToHGlobalAnsi(Value);
String ToReturn = Marshal.PtrToStringAnsi(MediaInfoA_Option(Handle, Option_Ptr, Value_Ptr));
Marshal.FreeHGlobal(Option_Ptr);
Marshal.FreeHGlobal(Value_Ptr);
return ToReturn;
}
else
return Marshal.PtrToStringUni(MediaInfo_Option(Handle, Option, Value));
}
public int State_Get() { if (Handle == (IntPtr)0) return 0; return (int)MediaInfo_State_Get(Handle); }
public int Count_Get(StreamKind StreamKind, int StreamNumber) { if (Handle == (IntPtr)0) return 0; return (int)MediaInfo_Count_Get(Handle, (IntPtr)StreamKind, (IntPtr)StreamNumber); }
private IntPtr Handle;
private bool MustUseAnsi;
//Default values, if you know how to set default values in C#, say me
public String Get(StreamKind StreamKind, int StreamNumber, String Parameter, InfoKind KindOfInfo) { return Get(StreamKind, StreamNumber, Parameter, KindOfInfo, InfoKind.Name); }
public String Get(StreamKind StreamKind, int StreamNumber, String Parameter) { return Get(StreamKind, StreamNumber, Parameter, InfoKind.Text, InfoKind.Name); }
public String Get(StreamKind StreamKind, int StreamNumber, int Parameter) { return Get(StreamKind, StreamNumber, Parameter, InfoKind.Text); }
public String Option(String Option_) { return Option(Option_, ""); }
public int Count_Get(StreamKind StreamKind) { return Count_Get(StreamKind, -1); }
private bool isdisposed = false;
public void Dispose()
{
if (!this.isdisposed)
{
// Clean-up Unmanaged
if (Handle != (IntPtr)0)
{
MediaInfo_Delete(Handle);
Handle = IntPtr.Zero;
}
this.isdisposed = true;
}
}
}
public class MediaInfoList
{
//Import of DLL functions. DO NOT USE until you know what you do (MediaInfo DLL do NOT use CoTaskMemAlloc to allocate memory)
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfoList_New();
[DllImport("MediaInfo.dll")]
private static extern void MediaInfoList_Delete(IntPtr Handle);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfoList_Open(IntPtr Handle, [MarshalAs(UnmanagedType.LPWStr)] string FileName, IntPtr Options);
[DllImport("MediaInfo.dll")]
private static extern void MediaInfoList_Close(IntPtr Handle, IntPtr FilePos);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfoList_Inform(IntPtr Handle, IntPtr FilePos, IntPtr Reserved);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfoList_GetI(IntPtr Handle, IntPtr FilePos, IntPtr StreamKind, IntPtr StreamNumber, IntPtr Parameter, IntPtr KindOfInfo);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfoList_Get(IntPtr Handle, IntPtr FilePos, IntPtr StreamKind, IntPtr StreamNumber, [MarshalAs(UnmanagedType.LPWStr)] string Parameter, IntPtr KindOfInfo, IntPtr KindOfSearch);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfoList_Option(IntPtr Handle, [MarshalAs(UnmanagedType.LPWStr)] string Option, [MarshalAs(UnmanagedType.LPWStr)] string Value);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfoList_State_Get(IntPtr Handle);
[DllImport("MediaInfo.dll")]
private static extern IntPtr MediaInfoList_Count_Get(IntPtr Handle, IntPtr FilePos, IntPtr StreamKind, IntPtr StreamNumber);
//MediaInfo class
public MediaInfoList() { Handle = MediaInfoList_New(); }
~MediaInfoList() { MediaInfoList_Delete(Handle); }
public int Open(String FileName, InfoFileOptions Options) { return (int)MediaInfoList_Open(Handle, FileName, (IntPtr)Options); }
public void Close(int FilePos) { MediaInfoList_Close(Handle, (IntPtr)FilePos); }
public String Inform(int FilePos) { return Marshal.PtrToStringUni(MediaInfoList_Inform(Handle, (IntPtr)FilePos, (IntPtr)0)); }
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)); }
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)); }
public String Option(String Option, String Value) { return Marshal.PtrToStringUni(MediaInfoList_Option(Handle, Option, Value)); }
public int State_Get() { return (int)MediaInfoList_State_Get(Handle); }
public int Count_Get(int FilePos, StreamKind StreamKind, int StreamNumber) { return (int)MediaInfoList_Count_Get(Handle, (IntPtr)FilePos, (IntPtr)StreamKind, (IntPtr)StreamNumber); }
private IntPtr Handle;
//Default values, if you know how to set default values in C#, say me
public void Open(String FileName) { Open(FileName, 0); }
public void Close() { Close(-1); }
public String Get(int FilePos, StreamKind StreamKind, int StreamNumber, String Parameter, InfoKind KindOfInfo) { return Get(FilePos, StreamKind, StreamNumber, Parameter, KindOfInfo, InfoKind.Name); }
public String Get(int FilePos, StreamKind StreamKind, int StreamNumber, String Parameter) { return Get(FilePos, StreamKind, StreamNumber, Parameter, InfoKind.Text, InfoKind.Name); }
public String Get(int FilePos, StreamKind StreamKind, int StreamNumber, int Parameter) { return Get(FilePos, StreamKind, StreamNumber, Parameter, InfoKind.Text); }
public String Option(String Option_) { return Option(Option_, ""); }
public int Count_Get(int FilePos, StreamKind StreamKind) { return Count_Get(FilePos, StreamKind, -1); }
}
} //NameSpace
由于字符限制,我不允许我在此处粘贴整个MediaInfo.cs
文件。要使用新的包装器,请将using MediaInfoWrapper = MediaInfoLib.MediaInfo;
添加到顶部的using
语句中,并将new MediaInfoWrapper(someStringVar)
的出现次数更改为new MediaInfoWrapper()
。之后,您应该已经设置好!