[转]C#使用FFmpeg 将视频格式转换成MP4示例

(转载请删除括号里的内容)

一、常用视频格式分辨率

  • 640x480p
  • 720p格式,分辨率为1280×720p / 60Hz,行频为45kHz
  • 1080p格式,分辨率为1920×1080逐行扫描,专业格式

二、FFmpeg部分参数说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//参数说明
/*
    * -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)
    *
    */

  

三、使用实例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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);
            //    }
            //}
        }
    }
}

  

 

更多参考:

ffmpeg ffplay ffprobe资料整理

HTML5媒体播放说明


---------------------
作者:天马3798
来源:CNBLOGS
原文:https://www.cnblogs.com/tianma3798/p/6285602.html
版权声明:本文为作者原创文章,转载请附上博文链接!
内容解析By:CSDN,CNBLOG博客文章一键转载插件

posted @   JackieZhengChina  阅读(872)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示