-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainTask.cs
More file actions
60 lines (53 loc) · 2.66 KB
/
MainTask.cs
File metadata and controls
60 lines (53 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System.Diagnostics;
namespace VideoAudioTransfer
{
public class MainTask
{
public static void TransferAudio(string inputVideo1Path, string inputVideo2Path)
{
if (inputVideo1Path == null){
Form1.WriteToLog("Please choose video that TAKE audio!");
return;
}
if (inputVideo2Path == null){
Form1.WriteToLog("Please choose video that GIVE audio!");
return;
}
Form1.WriteToLog("Start transfering");
// Get the root folder of the application
string appRootFolder = AppDomain.CurrentDomain.BaseDirectory;
string outputFileName = Path.GetFileName(inputVideo1Path);
// Construct the output video file path in the application's root folder
string outputVideoPath = Path.Combine(appRootFolder, outputFileName);
// Construct the FFmpeg command to merge audio from video 1 into video 2
string ffmpegCommand = $"-y -i \"{inputVideo1Path}\" -i \"{inputVideo2Path}\" -c copy -map 0:v:0 -map 1:a:0 -shortest \"{outputVideoPath}\"";
// Run the FFmpeg command and capture the output
Form1.WriteToLog("Start ffmpeg");
using (Process ffmpegProcess = new Process())
{
ffmpegProcess.StartInfo.FileName = "ffmpeg"; // Make sure 'ffmpeg' is in your system's PATH.
ffmpegProcess.StartInfo.Arguments = ffmpegCommand;
ffmpegProcess.StartInfo.RedirectStandardOutput = true;
ffmpegProcess.StartInfo.RedirectStandardError = true;
ffmpegProcess.StartInfo.UseShellExecute = false;
ffmpegProcess.StartInfo.CreateNoWindow = true;
ffmpegProcess.Start();
ffmpegProcess.OutputDataReceived += new DataReceivedEventHandler((sender, e) => OutputHandler(e.Data, false));
ffmpegProcess.ErrorDataReceived += new DataReceivedEventHandler((sender, e) => OutputHandler(e.Data, true));
// Begin asynchronous read of the standard output and error streams
ffmpegProcess.BeginOutputReadLine();
ffmpegProcess.BeginErrorReadLine();
ffmpegProcess.WaitForExit();
string logMessage = string.Format("Done, output should be found at: {0}", outputVideoPath);
Form1.WriteToLog(logMessage);
}
}
private static void OutputHandler(string output, bool isError)
{
if (!String.IsNullOrEmpty(output))
{
Form1.WriteToLog("[ffmpeg] " + output);
}
}
}
}