I tried the FFmpeg library but the code seems not working.
C#
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.


Amit MohantyRank #112 · 20K reputationPosted Mar 5, 2024, 2:20 AM EST
I think when joining the video paths into a single command, you are using string.Join on videoPaths[0], which would only concatenate the first video path. You should concatenate all video paths in the array.
public void MergeVideos(string[] videoPaths, string outputFilePath) { // Combine all video paths into FFmpeg command string command = "ffmpeg -i concat:" + string.Join("|", videoPaths) + " -c copy " + outputFilePath; // Execute FFmpeg command ExecuteFFmpegCommand(command); }Kasun Lee16 reputationPosted Mar 5, 2024, 8:27 AM EST
>> I think when joining the video paths into a single command, you are using string.Join on videoPaths[0], which would only concatenate the first video path. You should concatenate all video paths in the array.
Yes, my bad, fixed it, but still it's not working. ffmpeg.exe is being executed, but the video merging does not happen...
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 9, 2024, 10:00 PM EST
To correctly concatenate all video paths in the array, we should use
string.Joinon the entirevideoPathsarray, not justvideoPaths[0].public void MergeVideos(string[] videoPaths, string outputFilePath) { if (videoPaths == null || videoPaths.Length == 0) { throw new ArgumentException("Video paths array is empty or null."); } // Combine all video paths into FFmpeg command string command = "ffmpeg -i concat:" + string.Join("|", videoPaths) + " -c copy " + outputFilePath; // Execute FFmpeg command ExecuteFFmpegCommand(command); }In this improvement:
I added a check to ensure that the
videoPathsarray is not null or empty before attempting to concatenate the paths. If the array is empty or null, anArgumentExceptionis thrown to indicate the error.I updated
string.Jointo concatenate all video paths in thevideoPathsarray, ensuring that the correct FFmpeg command is generated with all video paths included in the concatenation.With this improvement, the
MergeVideosmethod will correctly concatenate all video paths provided in thevideoPathsarray when generating the FFmpeg command.