C# 读取视频 时长,要求结果是7位数
- User
C# 读取视频 时长,要求结果是7位数
- Assistant
你可以使用以下代码来读取视频时长并以7位数的格式显示:
using System; using System.IO; using Accord.Video.FFMPEG; class Program { static void Main(string[] args) { string videoFilePath = "path/to/video.mp4"; using (var reader = new VideoFileReader()) { reader.Open(videoFilePath); TimeSpan duration = reader.GetVideoDuration(); Console.WriteLine(duration.TotalSeconds.ToString("0000000")); } } }这个代码使用Accord.Video.FFMPEG库来读取视频时长。然后,它将时间跨度转换为总秒数并使用
ToString("0000000")格式化字符串将结果转换为7位数。 - User
如果不用FFMPEG能实现吗
- Assistant
如果不使用FFMPEG,你也可以通过其他方式读取视频时长并将其格式化为7位数。例如,可以使用Windows Media Player COM组件来读取视频时长。你可以使用以下代码:
using System; using WMPLib; class Program { static void Main(string[] args) { string videoFilePath = "path/to/video.mp4"; WindowsMediaPlayer wmp = new WindowsMediaPlayer(); IWMPMedia media = wmp.newMedia(videoFilePath); TimeSpan duration = TimeSpan.FromSeconds(media.duration); Console.WriteLine(duration.TotalSeconds.ToString("0000000")); } }这个代码使用Windows Media Player COM组件来读取视频的时长,然后将它转换为TimeSpan并格式化为7位数。 请注意,需要添加对
Interop.WMPLib.dll的引用。但是需要注意的是,Windows Media Player COM组件在64位操作系统上可能不可用。因此,使用FFMPEG是一个更好的跨平台解决方案。
- User
还有没有其它方式
- Assistant
除了使用FFMPEG和Windows Media Player COM组件之外,还可以使用其他框架或库来读取视频时长,比如:
- VLC.DotNet:这是一个基于VLC播放器的库,可以读取各种视频格式的元数据,包括时长等信息。
- MediaToolkit:这是一个C#库,它包装了FFMPEG和其他多媒体库,提供了易用的API来读取视频信息。
- DirectShow.NET:这是一个基于Microsoft DirectShow的库,可以读取视频时长等元数据信息。
这些库都可以用来读取视频时长等信息,但注意它们的需要引用外部库或框架,并且可能需要一些额外的配置和依赖项。
- User
如果用DirectShow.NET的话怎么写
- Assistant
使用DirectShow.NET读取视频时长可以使用MediaDet类。以下是一个示例代码,可以读取视频文件的时长并将其格式化为7位数:
using System; using DirectShowLib; class Program { static void Main(string[] args) { string videoFilePath = "path/to/video.mp4"; // Create the MediaDet object IMediaDet mediaDet = new MediaDet(); mediaDet.Filename = videoFilePath; // Get the length of the video double durationSeconds; mediaDet.get_StreamLength(out durationSeconds); TimeSpan duration = TimeSpan.FromSeconds(durationSeconds); Console.WriteLine(duration.TotalSeconds.ToString("0000000")); } }此代码使用DirectShow.NET连接到video.mp4文件并使用MediaDet类获取其流长度并转换为TimeSpan对象。编辑输出以格式化为7位数。请确保已添加对DirectShowNET.dll的引用并且项目已配置为为您的操作系统构建目标。
- User
使用DirectShow.NET时 需要安装FFMPEG吗
- Assistant
使用DirectShow.NET并不需要安装FFMPEG,因为DirectShow是Windows自带的多媒体框架之一,而FFMPEG是另一个跨平台的开源多媒体框架。DirectShow.NET是一个.NET语言的封装库,可以使用DirectShow API来读取多媒体数据,而不需要使用FFMPEG。因此,使用DirectShow.NET读取视频时长不需要安装FFMPEG。
- User
DirectShow.NET 没找到这个
- Assistant
如果你在项目中没有安装
DirectShowLib,那么你需要通过NuGet安装。以下是具体步骤:- 在Visual Studio中打开你的C#项目。
- 在“解决方案资源管理器”中右键单击项目名称,选择“管理NuGet程序包”。
- 在NuGet包管理器窗口中选择“浏览”选项卡。
- 搜索“DirectShowLib”。
- 在列表中选择“DirectShowLib”并单击“安装”按钮。
- 等待安装完成后就可以使用
DirectShowLib库了。
在NuGet安装完成后,你需要在代码中添加
using DirectShowLib;来使用该库。 - User
还有其它方式实现吗
- Assistant
除了上面提到的方式,还有一种方法可以实现读取视频时长,那就是使用.NET自带的Windows Media Format SDK。以下是一个示例代码,可以读取AVI和WMV格式视频文件的时长并将其格式化为7位数:
using System; using System.Runtime.InteropServices.ComTypes; using WMPLib; class Program { [DllImport("WMVCORE.DLL", CharSet = CharSet.Unicode, PreserveSig = true)] public static extern int WMCreateSyncReader(IntPtr pUnkCert, uint dwRights, out IWMSyncReader ppReader); static void Main(string[] args) { string videoFilePath = "path/to/video.avi"; IWMSyncReader reader; int hr = WMCreateSyncReader(IntPtr.Zero, 0, out reader); IWMHeaderInfo3 headerInfo = (IWMHeaderInfo3)reader; INSSBuffer buf; uint len; foreach (var stream in reader.GetOutputFormatCount()) { reader.SetOutputProps(stream, null); headerInfo.GetAttribute((ushort)stream, "Duration", out len, out buf); double durationNanoSeconds; buf.GetData(out durationNanoSeconds, ref len); TimeSpan duration = TimeSpan.FromTicks((long)durationNanoSeconds / 100); Console.WriteLine(duration.TotalSeconds.ToString("0000000")); } Marshal.ReleaseComObject(reader); } } - User
还有其它库吗
- Assistant
还有一些其他的库可以实现读取视频时长,比如:
- VideoLAN.LibVLC:这是一个VLC播放器的封装库,可以在.NET应用程序中轻松地使用VLC播放器功能。
- NReco.VideoInfo:这是一个可以检索视频元数据的.NET库,包括视频时长和格式等信息。
- FFME:这是一个基于FFmpeg和WPF的.NET库,可以用于播放音频和视频文件。
这些库都可以用来读取视频时长等信息,但是它们可能会有不同的依赖项、API和使用方法。你可以根据自己的项目需要来选择适合的库。
- User
ffmpeg.autogen 用这个库可以实现吗
- Assistant
是的,你可以使用
FFmpeg.AutoGen库来读取视频时长信息。这个库提供了一个 C# 封装器,可以使用 FFmpeg 的 API 解码和操作音频和视频数据。以下是一个示例代码,可以使用
FFmpeg.AutoGen库读取视频时长并将其格式化为7位数:using System; using FFmpeg.AutoGen; using System.Diagnostics; class Program { static void Main(string[] args) { string videoFilePath = "path/to/video.mp4"; ffmpeg.av_register_all(); AVFormatContext* formatContext = null; // Open input file context if (ffmpeg.avformat_open_input(&formatContext, videoFilePath, null, null) != 0) { Console.WriteLine("Error opening file " + videoFilePath); return; } // Get format info if (ffmpeg.avformat_find_stream_info(formatContext, null) < 0) { Console.WriteLine("Error finding stream information."); ffmpeg.avformat_close_input(&formatContext); return; } // Find video stream and get index int streamIndex = -1; AVCodecParameters* codecParams = null; for (int i = 0; i < formatContext->nb_streams; i++) { var stream = formatContext->streams[i]; codecParams = stream->codecpar; if (codecParams->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO) { streamIndex = i; break; } } // Print duration in seconds double durationSeconds = (double)formatContext->duration / ffmpeg.AV_TIME_BASE; TimeSpan duration = TimeSpan.FromSeconds(durationSeconds); Console.WriteLine(duration.TotalSeconds.ToString("0000000")); // Close input file context ffmpeg.avformat_close_input(&formatContext); } } - User
我的工程是.net 4.5.2,请问ffmpeg.autogen要用哪个版本
- Assistant
如果你的项目是 .NET Framework 4.5.2,则你可以使用
FFmpeg.AutoGen库版本 4.2.1 或更高版本。从版本4.2.1开始 ,FFmpeg.AutoGen支持 .NET Framework 4.5 和更高版本。你可以通过在NuGet上搜索"FFmpeg.AutoGen"并选择最新版本进行安装。安装后,你需要将
ffmpeg的共享库文件添加到项目中。具体步骤如下:- 下载预编译好的
ffmpeg库压缩包。你可以从官网下载,也可以从其他网站下载。 - 解压缩后,将
ffmpeg\bin文件夹添加到你的项目中。 - 选中你项目中的
bin文件夹,在属性面板中将ffmpeg的共享库文件设置为始终复制。 - 运行代码时,确保将
ffmpeg的共享库文件放在可执行文件所在的路径中,或者将其添加到系统的Path环境变量中。
在使用
FFmpeg.AutoGen库之前,请确保你已经了解 FFmpeg 的相关知识。你可以参考官方文档或其他教程了解更多信息。 - 下载预编译好的
- User
能不能直接通过参数来调用ffmpeg .exe获取时长
- Assistant
是的,你可以直接通过执行 FFmpeg 的命令行工具来获取视频文件的时长。以下是一个示例代码,可以执行 FFmpeg 命令来读取视频时长并将其格式化为7位数:
using System; using System.Diagnostics; class Program { static void Main(string[] args) { string videoFilePath = "path/to/video.mp4"; ProcessStartInfo startInfo = new ProcessStartInfo { FileName = "ffmpeg.exe", Arguments = "-i " + videoFilePath + " -show_entries format=duration -v quiet -of csv=\"p=0\"", RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true }; using (Process process = Process.Start(startInfo)) { string output = process.StandardOutput.ReadToEnd(); double durationSeconds = Double.Parse(output); TimeSpan duration = TimeSpan.FromSeconds(durationSeconds); Console.WriteLine(duration.TotalSeconds.ToString("0000000")); } } }此代码使用了
System.Diagnostics命名空间,启动了一个新的进程以执行 FFmpeg 命令行工具。在命令行参数中定义了-i表示输入文件名,-show_entries format=duration表示要列出时长信息,-v quiet表示不输出冗余信息,-of csv=\"p=0\"表示以 CSV 格式输出并仅输出第一列(时长)。编辑输出以格式化为7位数。