C#使用FFmpeg 将视频格式转换成MP4示例
一、常用视频格式分辨率
- 640x480p
- 720p格式,分辨率为1280×720p / 60Hz,行频为45kHz
- 1080p格式,分辨率为1920×1080逐行扫描,专业格式
二、FFmpeg部分参数说明:
//参数说明
/*
* -i filename(input) 源文件目录
* -y 输出新文件,是否强制覆盖已有文件
* -c 指定编码器
* -fs limit_size(outinput) 设置文件大小的限制,以字节表示的。没有进一步的字节块被写入后,超过极限。输出文件的大小略大于所请求的文件大小。
* -s 视频比例 4:3 320x240/640x480/800x600 16:9 1280x720 ,默认值 'wxh',和原视频大小相同
* -vframes number(output) 将视频帧的数量设置为输出。别名:-frames:v
* -dframes number (output) 将数据帧的数量设置为输出.别名:-frames:d
* -frames[:stream_specifier] framecount (output,per-stream) 停止写入流之后帧数帧。
* -bsf[:stream_specifier] bitstream_filters (output,per-stream) 指定输出文件流格式,
例如输出h264编码的MP4文件:ffmpeg -i h264.mp4 -c:v copy -bsf:v h264_mp4toannexb -an out.h264
* -r 29.97 桢速率(可以改,确认非标准桢率会导致音画不同步,所以只能设定为15或者29.97)
*
*/
三、使用实例代码:
public class Demo2
{
public static string ffmpegtool = @"F:\SolutionSet\ABCSolution\VideoSolution\Demo1\bin\Debug\ffmpeg.exe";
//public static string ffmpegtool = @"F:\ABCSolution\ffmpeg-20160808-ce2217b-win64-static\bin\ffplay.exe";
public static string playFile = @"F:\SolutionSet\ABCSolution\VideoSolution\VideoSolution\Content\Video\my3.mp4";
public static string imgFile = @"F:\SolutionSet\ABCSolution\VideoSolution\VideoSolution\Content\Video\my3.gif";
public static string sourceFile = @"F:\SolutionSet\ABCSolution\VideoSolution\VideoSolution\Content\Video\COOLUI.mp4";
//public static string sourceFile = @"F:\ABCSolution\VideoSolution\VideoSolution\Content\Video\theme.mp4";
public void ConvertVideo()
{
Process p = new Process();//建立外部调用线程
p.StartInfo.FileName = ffmpegtool;//要调用外部程序的绝对路径
//参数(这里就是FFMPEG的参数了)
//p.StartInfo.Arguments = @"-i "+sourceFile+ " -ab 56 -b a -ar 44100 -b 500 -r 29.97 -s 1280x720 -y " + playFile+""; // p.StartInfo.Arguments = "-y -i \""+sourceFile+"\" -b v -s 800x600 -r 29.97 -b 1500 -acodec aac -ac 2 -ar 24000 -ab 128 -vol 200 -f psp \""+playFile+"\" "; //string strArg = "-i " + sourceFile + " -y -s 640x480 " + playFile + " ";
string strArg = "-i " + sourceFile + " -y -s 1280x720 " + playFile + " "; //获取图片
//截取图片jpg
//string strArg = "-i " + sourceFile + " -y -f image2 -t 1 " + imgFile;
//string strArg = "-i " + sourceFile + " -y -s 1280x720 -f image2 -t 1 " + imgFile; //视频截取
//string strArg = " -i " + sourceFile + " -y -ss 0:20 -frames 100 " + playFile; //转化gif动画
//string strArg = "-i " + sourceFile + " -y -s 1280x720 -f gif -vframes 30 " + imgFile;
//string strArg = " -i " + sourceFile + " -y -f gif -vframes 50 " + imgFile;
// string strArg = " -i " + sourceFile + " -y -f gif -ss 0:20 -dframes 10 -frames 50 " + imgFile; //显示基本信息
//string strArg = "-i " + sourceFile + " -n OUTPUT"; //播放视频
//string strArg = "-stats -i " + sourceFile + " "; p.StartInfo.Arguments = strArg; p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动线程(一定为FALSE,详细的请看MSDN)
p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中(这个一定要注意,FFMPEG的所有输出信息,都为错误输出流,用StandardOutput是捕获不到任何消息的...这是我耗费了2个多月得出来的经验...mencoder就是用standardOutput来捕获的)
p.StartInfo.CreateNoWindow = false;//不创建进程窗口
p.ErrorDataReceived += new DataReceivedEventHandler(Output);//外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
p.Start();//启动线程
p.BeginErrorReadLine();//开始异步读取
p.WaitForExit();//阻塞等待进程结束
p.Close();//关闭进程
p.Dispose();//释放资源
}
private void Output(object sendProcess, DataReceivedEventArgs output)
{
if (!String.IsNullOrEmpty(output.Data))
{
//处理方法...
Console.WriteLine(output.Data); ////去获取时长
//string partitio1 = @"Duration: \d{2}:\d{2}:\d{2}.\d{2}";
//if (RegexHelper.IsMatch(partitio1, output.Data))
//{
// string partition = @"(?<=Duration: )\d{2}:\d{2}:\d{2}.\d{2}";
// string timespan = RegexHelper.Matchs(output.Data, partition).FirstOrDefault();
// TimeSpan span;
// if (TimeSpan.TryParse(timespan, out span))
// {
// Console.WriteLine(span.TotalMilliseconds);
// }
//} ////获取时刻
//string partitio2 = @"time=\d{2}:\d{2}:\d{2}.\d{2}";
//if (RegexHelper.IsMatch(partitio2, output.Data))
//{
// string partition = @"(?<=time=)\d{2}:\d{2}:\d{2}.\d{2}"; // string timespan = RegexHelper.Matchs(output.Data, partition).FirstOrDefault();
// TimeSpan span;
// if (TimeSpan.TryParse(timespan, out span))
// {
// Console.WriteLine(span.TotalMilliseconds);
// }
//}
}
}
}
更多参考:
C#使用FFmpeg 将视频格式转换成MP4示例的更多相关文章
- C#使用FFmpeg 将视频格式转换成Gif图片示例
根据EFmpeg封装的视频转换gif工具:https://my.oschina.net/tianma3798/blog/825317 一.本次使用参数说明 /* * 参数说明: * -i 源文件位置 ...
- 不用 qlv 格式转换成 mp4 - 优雅的下载腾讯视频(mp4 格式)
不用 qlv 格式转换成 mp4 - 优雅的下载腾讯视频(mp4 格式) 问题描述: 朋友说离线腾讯视频是 qlv 格式的,只能使用腾讯视频软件打开.让我帮忙想想办法,能不能将 qlv 格式转换成 m ...
- Linux下安装ffmpeg,视频格式转换
下载ffmpeg 从ffmpeg官网:http://ffmpeg.org/download.html 下载最新的ffmpeg安装包,然后通过如下命令解压: 解压 ffmpeg-*.tar.bz2 文件 ...
- qlv格式转换成MP4格式
腾讯视频下载:1.先下载腾讯的客户端--->播放视频在客户端 2.播放视频一段后停止 3.点击腾讯客户端的右上角的设置 4.复制缓存地址(注意:在此之前需要设置"显示隐藏的文件夹&qu ...
- Flv视频格式如何转换成MP4格式
如何将flv视频格式转换成MP4格式呢?随着现在视频格式的不断多样化,视频格式转换的问题也成了现在生活中常见的问题,那么我们应该怎样将flv视频格式转换成MP4格式呢?下面我们就一起来看一下吧. 操作 ...
- 【转】qlv文件如何转换成mp4 怎样把下载好的qlv格式视频转换成MP4格式
狸窝 复制 收藏 保存到桌面 快速找教程方案 反馈需求 社会主义核心价值观 客服QQ41442901 马上注册 升级VIP 对于视频文件之间的转换问题,我也已经是无力吐槽了,每个 ...
- Unity 利用FFmpeg实现录屏、直播推流、音频视频格式转换、剪裁等功能
目录 一.FFmpeg简介. 二.FFmpeg常用参数及命令. 三.FFmpeg在Unity 3D中的使用. 1.FFmpeg 录屏. 2.FFmpeg 推流. 3.FFmpeg 其他功能简述. 一. ...
- asp.net实现调用ffmpeg实现视频格式的转换
视频格式转换的函数 //视频转换 public void VideoConvertFlv(string FromName, string ExportName) { string ffmpeg = H ...
- Ubuntu18.04下使用Blender进行视频格式转换
Ubuntu下可以使用Blender的Video Editing功能进行视频格式转换, 具体步骤: 打开Blender后, 在顶层菜单栏中, 将Choose Screen Layout修改为Video ...
随机推荐
- 在Javascript中使用protobuf与c++进行通信
环境:Win7_64旗舰版,VS2013 最近在研究Webkit,已经编译成功,接下来就是Javascript与c++如何传输数据,立刻就想到了protobuf,但是谷歌不支持Javascript,百 ...
- CodeIgniter 如何去掉 Index.php
步骤; 1.打开你的WEB服务器的httpd.conf文件. 2.找到LoadModule rewrite_module modules/mod_rewrite.so这行,把该行前的#去掉.保存该文件 ...
- dump文件生成与调试(VS2008)
总结一下dump文件生成和调试的方法: 1:用SetUnhandledExceptionFilter捕获未处理的异常,包含头文件<windows.h>.函数原型为: LPTOP_LEVEL ...
- 【转载】关于SetWindowOrgEx、SetViewportOrgEx、SetViewportExtEx 和SetWindowExtEx 详解
关于SetWindowOrgEx.SetViewportOrgEx.SetViewportExtEx 和SetWindowExtEx 详解 1. SetWindowOrgEx是设置窗口的原点坐标. ...
- UVA - 437 The Tower of Babylon(dp-最长递增子序列)
每一个长方形都有六种放置形态,其实可以是三种,但是判断有点麻烦直接用六种了,然后按照底面积给这些形态排序,排序后就完全变成了LIS的问题.代码如下: #include<iostream> ...
- UVALive 2056 Lazy Math Instructor(递归处理嵌套括号)
因为这个题目说明了优先级的规定,所以可以从左到右直接运算,在处理嵌套括号的时候,可以使用递归的方法,给定每一个括号的左右边界,伪代码如下: int Cal(){ if(括号) sum += Cal( ...
- AngularJS 基础用法
判断语句: <li ng-repeat=”person in persons”> <span ng-switch on=”person.sex”> <span ng-sw ...
- Java 堆内存(Heap)[转]
将jvm内存很不错的文章,转自 堆(Heap)又被称为:优先队列(Priority Queue),是计算机科学中一类特殊的数据结构的统称.堆通常是一个可以被看做一棵树的数组对象.在队列中,调度程序反复 ...
- HDU1372,BFS象棋马走日
简单的对于bfs的运用,但是还是写的太慢了写了TMD的1H,主要是不熟悉,以后慢慢熟悉就好了,模型基本已经能建立了,主要出现bug是在方向数组的运用上面,一定要记得是从0开始的,而不是从1开始的,导致 ...
- git使用之如何将github库下载到本地与如何将代码上传github
git使用之如何将github库下载到本地与如何将代码上传github ---------------------------------------------------------------- ...