C# 調用FFmpeg 合併視頻和音頻

C#修改環境變量:

 string pathStr = System.Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine);
            // 修改系統環境變量Path值
            System.Environment.SetEnvironmentVariable("Path", pathStr + ";D:\\KK", EnvironmentVariableTarget.Machine);

//D:\\KK 是新加的值

  

 

C#調用FFmpeg合併視頻個音頻:

void Test()
        {

            string ffmpegPath = @"D:\Soft\ffmpeg\bin\ffmpeg.exe";  // 替換爲你的FFmpeg可執行文件路徑
            string videoFile = @"C:\Users\Administrator\Desktop\work\1\idle.mp4";  // 替換爲要合併的視頻文件路徑
            string audioFile = @"D:\dl.mp3";  // 替換爲要合併的音頻文件路徑
            string outputFile = @"D:\ok.mp4";  // 替換爲輸出文件的保存路徑和名稱

            string arguments = $"-i \"{videoFile}\" -i \"{audioFile}\" -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 \"{outputFile}\"";

            ProcessStartInfo psi = new ProcessStartInfo(ffmpegPath)
            {
                Arguments = arguments,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true
            };

            Process process = new Process();
            process.StartInfo = psi;
            process.OutputDataReceived += (sender, eventArgs) =>
            {
                //Title=("FFmpeg output: " + eventArgs.Data);
            };
            process.ErrorDataReceived += (sender, eventArgs) =>
            {
               // Title=("FFmpeg error: " + eventArgs.Data);
            };

            process.Start();
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();
            process.WaitForExit();

            Title=("合併完成");

        }

  

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