C#利用ffmpeg对上传视频转码处理

1 篇文章 0 订阅
1 篇文章 0 订阅

本人项目中做到前端的一些视频页面开发,遇到有很多坑,主要问题在于浏览器支持的视频编码格式的问题上。很多视频没经过处理直接上传到后台,因为目前浏览器对视频编码格式的支持有限,导致很多视频都不能播放,特别是移动端IOS,兼容性很差。

[转]HTML5的视频格式之争

我这里想到的一种解决方案是上传视频到后台后进行转码处理, 处理成传统浏览器都支持的视频编码格式,比如H.264.

这里用的是C#控制台做的一个转码处理例子:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace VideoTranscoding
{
    class Program
    {
        static void Main(string[] args)
        {
            Test1();
        }

        static string path = AppDomain.CurrentDomain.BaseDirectory;
        static string pathVedio = "";

        //原方法转码
        private static void Test1()
        {

            Process p = new Process();


            p.StartInfo.FileName = path + "ffmpeg.exe";

            p.StartInfo.UseShellExecute = false;
            string srcFileName = "";
            string destFileName = "";
            srcFileName = path + "1.WMV";

            destFileName = path + "1.WMV";

            //p.StartInfo.Arguments = "-i " + srcFileName + " -y  -vcodec h264 -b 5942 " + destFileName;    //执行参数
            //p.StartInfo.Arguments = "-i " + srcFileName + " -c:v libx264 -crf 23 -c:a libfaac -q:a 100 output.mp4";    //执行参数
            p.StartInfo.Arguments = "-i " + srcFileName + " -c:v libx264 -strict -2 output.mp4";    //执行参数


            p.StartInfo.UseShellExecute = false;  不使用系统外壳程序启动进程
            p.StartInfo.CreateNoWindow = true;  //不显示dos程序窗口

            p.StartInfo.RedirectStandardInput = true;

            p.StartInfo.RedirectStandardOutput = true;

            p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中

            p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);

            p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);

            p.StartInfo.UseShellExecute = false;

            p.Start();

            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

            p.BeginErrorReadLine();//开始异步读取

            p.WaitForExit();//阻塞等待进程结束

            p.Close();//关闭进程

            p.Dispose();//释放资源
        }



        private static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            Console.WriteLine(e.Data);
        }

        private static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            Console.WriteLine(e.Data);
        }
    }
}

例子中用的是WMV编码格式转MP4,具体其他转码方式,可自行百度或到ffmpeg官网查看学习。

 

源码链接: https://pan.baidu.com/s/12QBAiizdEYDnJserVUt61A 提取码: c4dg 

[转]Ffmpeg常用转码命令

ffmpeg官网:http://ffmpeg.org

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值