Kasun Lee

Kasun Lee

  • 959
  • 811
  • 281

What's the easiest way to merge two videos into one?

Mar 5 2024 7:11 AM

I tried the FFmpeg library but the code seems not working.

    public void MergeVideos(string[] videoPaths, string outputFilePath)
    {
        // Combine video paths into FFmpeg command
        string command = "ffmpeg -i concat:" + string.Join("|", videoPaths[0]) + " -c copy " + outputFilePath;

        // Execute FFmpeg command
        ExecuteFFmpegCommand(command);
    }

       private void ExecuteFFmpegCommand(string command)
       {
           try
           {
               // Start the FFmpeg process
               Process ffmpegProcess = new Process
               {
                   StartInfo =
                   {
                       FileName = "ffmpeg.exe", // Assuming ffmpeg.exe is in the PATH environment variable
                       Arguments = command,
                       UseShellExecute = false,
                       RedirectStandardOutput = true,
                       RedirectStandardError = true,
                       CreateNoWindow = false
                   }
               };

               // Subscribe to process output data events
               ffmpegProcess.OutputDataReceived += (sender, e) => Console.WriteLine(e.Data);
               ffmpegProcess.ErrorDataReceived += (sender, e) => Console.WriteLine(e.Data);

               // Start process and redirect output
               ffmpegProcess.Start();
               ffmpegProcess.BeginOutputReadLine();
               ffmpegProcess.BeginErrorReadLine();

               // Wait for process to exit
               ffmpegProcess.WaitForExit();
           }

Are there bugs in the above code? Or Is there some other easier way to do it?

Thanks.


Answers (3)