C#利用ffmpeg對上傳視頻轉碼處理

本人項目中做到前端的一些視頻頁面開發,遇到有很多坑,主要問題在於瀏覽器支持的視頻編碼格式的問題上。很多視頻沒經過處理直接上傳到後臺,因爲目前瀏覽器對視頻編碼格式的支持有限,導致很多視頻都不能播放,特別是移動端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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章