C#通过FFmpeg获得视频文件参数 C#通过FFmpeg获得视频参数

 

FFmpeg简介

FFmpeg is a complete, cross-platform solution to record, convert and stream audio and video(录制、转换 、音/视频,并可将其转换为音/视频流的完整的,跨平台的j解决方案).FFmpeg的开发是基于Linux操作系统,但是可以在大多数操作系统中编译和使用。

官网:http://ffmpeg.org/

在windows环境下,需要用到其Windows Builds。

下载地址: http://ffmpeg.zeranoe.com/builds/

可根据自己的环境(32位或64位)选择下载相应的static builds即可。

解压下载后的包会发现,里面都是.exe文件。我们在.net中用FFmpeg只能利用命令参数的形式来执行这些文件,为我们服务,极为不便。如果有一个能在.net环境中以对象的方式调用ffmpeg.exe的包装类就好了。还真有,php有,C#的也有,如:

http://nolovelust.com/post/Another-Simple-C-Wrapper-For-FFmpeg.aspx

具体的可到这个网页下载,ffmpeg.exe的包装类使用。如果对ffmpeg.exe在C#中的调用感兴趣,可学习下其源码。

使用非常简单,如下解析视频的相关信息:

 1 /**
2 * 支持视频格式:mpeg,mpg,avi,dat,mkv,rmvb,rm,mov.
3 *不支持:wmv
4 * **/
5
6 VideoEncoder.Encoder enc =new VideoEncoder.Encoder();
7 //ffmpeg.exe的路径,程序会在执行目录(....FFmpeg测试inDebug)下找此文件,
8 enc.FFmpegPath ="ffmpeg.exe";
9 //视频路径
10 string videoFilePath ="d:\纯粹瑜伽-混合课程.avi";
11 VideoFile videoFile =new VideoFile(videoFilePath);
12
13 enc.GetVideoInfo(videoFile);
14
15 TimeSpan totaotp = videoFile.Duration;
16 string totalTime =string.Format("{0:00}:{1:00}:{2:00}", (int)totaotp.TotalHours, totaotp.Minutes, totaotp.Seconds);
17
18 Console.WriteLine("时间长度:{0}",totalTime);
19 Console.WriteLine("高度:{0}",videoFile.Height);
20 Console.WriteLine("宽度:{0}", videoFile.Width);
21 Console.WriteLine("数据速率:{0}",videoFile.VideoBitRate);
22 Console.WriteLine("数据格式:{0}", videoFile.VideoFormat);
23 Console.WriteLine("比特率:{0}", videoFile.BitRate);//平均混合码率
24 Console.WriteLine("文件路径:{0}", videoFile.Path);
25
26 Console.ReadKey();

输出:

C#通过FFmpeg获得视频文件参数
C#通过FFmpeg获得视频参数

实例源码:FFmpeg测试

FFmpeg简介

FFmpeg is a complete, cross-platform solution to record, convert and stream audio and video(录制、转换 、音/视频,并可将其转换为音/视频流的完整的,跨平台的j解决方案).FFmpeg的开发是基于Linux操作系统,但是可以在大多数操作系统中编译和使用。

官网:http://ffmpeg.org/

在windows环境下,需要用到其Windows Builds。

下载地址: http://ffmpeg.zeranoe.com/builds/

可根据自己的环境(32位或64位)选择下载相应的static builds即可。

解压下载后的包会发现,里面都是.exe文件。我们在.net中用FFmpeg只能利用命令参数的形式来执行这些文件,为我们服务,极为不便。如果有一个能在.net环境中以对象的方式调用ffmpeg.exe的包装类就好了。还真有,php有,C#的也有,如:

http://nolovelust.com/post/Another-Simple-C-Wrapper-For-FFmpeg.aspx

具体的可到这个网页下载,ffmpeg.exe的包装类使用。如果对ffmpeg.exe在C#中的调用感兴趣,可学习下其源码。

使用非常简单,如下解析视频的相关信息:

 1 /**
2 * 支持视频格式:mpeg,mpg,avi,dat,mkv,rmvb,rm,mov.
3 *不支持:wmv
4 * **/
5
6 VideoEncoder.Encoder enc =new VideoEncoder.Encoder();
7 //ffmpeg.exe的路径,程序会在执行目录(....FFmpeg测试inDebug)下找此文件,
8 enc.FFmpegPath ="ffmpeg.exe";
9 //视频路径
10 string videoFilePath ="d:\纯粹瑜伽-混合课程.avi";
11 VideoFile videoFile =new VideoFile(videoFilePath);
12
13 enc.GetVideoInfo(videoFile);
14
15 TimeSpan totaotp = videoFile.Duration;
16 string totalTime =string.Format("{0:00}:{1:00}:{2:00}", (int)totaotp.TotalHours, totaotp.Minutes, totaotp.Seconds);
17
18 Console.WriteLine("时间长度:{0}",totalTime);
19 Console.WriteLine("高度:{0}",videoFile.Height);
20 Console.WriteLine("宽度:{0}", videoFile.Width);
21 Console.WriteLine("数据速率:{0}",videoFile.VideoBitRate);
22 Console.WriteLine("数据格式:{0}", videoFile.VideoFormat);
23 Console.WriteLine("比特率:{0}", videoFile.BitRate);//平均混合码率
24 Console.WriteLine("文件路径:{0}", videoFile.Path);
25
26 Console.ReadKey();

输出:

C#通过FFmpeg获得视频文件参数
C#通过FFmpeg获得视频参数

实例源码:FFmpeg测试