首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

.NET 影音處理實戰:FFmpeg 與 MEncoder 影片轉碼、截圖完整範例

     編輯:關於.NET

專案需處理使用者上傳影片轉碼、擷取縮圖,本文比較 FFmpeg 與 MEncoder 特性,提供 .NET `Process` 呼叫完整範例與參數速查表。

工具特性比較

特性FFmpegMEncoder
授權LGPL/GPLGPL
支援格式極廣 (不含 RM/RMVB)極廣 (含 RM/RMVB)
轉碼速度快慢 (運算量大)
畫質良好極佳
截圖/擷取強大 (支援精確時間點)較弱
適用場景ASF/AVI/MPG/3GP/MOV、截圖、快速轉碼WMV/RMVB/RM、高畫質需求

.NET 呼叫封裝類

using System.Diagnostics;
using System.IO;

public class VideoProcessor
{
    private readonly string _ffmpegPath;
    private readonly string _mencoderPath;

    public VideoProcessor(string ffmpegPath, string mencoderPath)
    {
        _ffmpegPath = ffmpegPath;
        _mencoderPath = mencoderPath;
    }

    public bool Convert(string input, string output, string args = null)
    {
        string ext = Path.GetExtension(input).ToLower();
        string tool = (ext == ".wmv" || ext == ".rmvb" || ext == ".rm") 
            ? _mencoderPath : _ffmpegPath;
        string arguments = args ?? GetDefaultArgs(ext, input, output);
        return RunProcess(tool, arguments);
    }

    public bool ExtractFrame(string input, string outputImage, double seconds)
    {
        // 僅用 FFmpeg 擷取
        string args = $"-ss {seconds} -i \"{input}\" -vframes 1 -f image2 \"{outputImage}\"";
        return RunProcess(_ffmpegPath, args);
    }

    private bool RunProcess(string fileName, string arguments)
    {
        try
        {
            var psi = new ProcessStartInfo
            {
                FileName = fileName,
                Arguments = arguments,
                UseShellExecute = false,
                CreateNoWindow = true,
                RedirectStandardOutput = true,
                RedirectStandardError = true
            };
            using (var proc = Process.Start(psi))
            {
                proc.WaitForExit(300000); // 5 分鐘逾時
                return proc.ExitCode == 0;
            }
        }
        catch { return false; }
    }

    private string GetDefaultArgs(string ext, string input, string output)
    {
        // 簡單預設:同畫質轉碼
        return $"-i \"{input}\" -c:v libx264 -crf 23 -preset medium -c:a aac -b:a 128k \"{output}\"";
    }
}

實戰示範

環境準備

  1. 下載 FFmpeg 靜態建置 (https://ffmpeg.org/download.html) 解壓至專案 `Tools/ffmpeg/bin`
  2. 下載 MEncoder (通常隨 MPlayer 附贈) 解壓至 `Tools/mencoder`
  3. 測試影片:Windows 範例 `C:\Windows\clock.avi`

轉碼測試

var processor = new VideoProcessor(
    @"Tools\ffmpeg\bin\ffmpeg.exe",
    @"Tools\mencoder\mencoder.exe");

bool ok = processor.Convert(@"C:\Windows\clock.avi", @"Output\clock.mp4");
// 輸出檔案可正常播放

擷取縮圖測試

// clock.avi 每秒顯示一個數字
processor.ExtractFrame(@"C:\Windows\clock.avi", @"Output\frame_2s.jpg", 2); // 應擷取到 "3"
processor.ExtractFrame(@"C:\Windows\clock.avi", @"Output\frame_4s.jpg", 4); // 應擷取到 "5"

測試影片資訊

影片內容每秒一數字

轉碼後輸出檔案

輸出檔案播放正常

第 2 秒擷取圖

第 4 秒擷取圖

FFmpeg 核心參數速查

通用:
  -i            輸入檔
  -y                   覆蓋輸出
  -ss 

部署注意事項

  • 伺服器需安裝 Visual C++ Redistributable (FFmpeg 相依)
  • 資料夾權限:`Tools` 唯讀,`Output` 寫入權限
  • 長時間轉碼建議採用背景工作 (Hangfire/Quartz/Windows Service) 避免 HTTP 逾時
  • 並發控制:限制同時轉碼程序數,避免 CPU/IO 耗盡

原始碼下載:MaoyaVideo.rar (含 FFmpeg/MEncoder 工具目錄結構)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved