Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Diagnostics;
- public class ScreenRecorder
- {
- private Process ffmpegProcess;
- public void StartRecording(string outputPath)
- {
- // Replace the path with your actual path to ffmpeg.exe and your audio device name
- string ffmpegPath = @"D:\Captured Videos\ffmpeg.exe";
- string audioDeviceName = "Stereo Mix (Realtek(R) Audio)"; // Replace with actual device name
- string videoArguments = "-f gdigrab -framerate 30 -i desktop";
- string audioArguments = $"-f dshow -i audio=\"{audioDeviceName}\"";
- string fileArguments = $"-preset ultrafast -pix_fmt yuv420p \"{outputPath}\"";
- ProcessStartInfo startInfo = new ProcessStartInfo
- {
- FileName = ffmpegPath,
- Arguments = $"{videoArguments} {audioArguments} {fileArguments}",
- UseShellExecute = false,
- CreateNoWindow = true,
- RedirectStandardInput = true,
- RedirectStandardOutput = true,
- RedirectStandardError = true
- };
- ffmpegProcess = new Process { StartInfo = startInfo };
- ffmpegProcess.EnableRaisingEvents = true;
- ffmpegProcess.Start();
- ffmpegProcess.BeginErrorReadLine();
- ffmpegProcess.BeginOutputReadLine();
- }
- public void StopRecording()
- {
- if (ffmpegProcess != null && !ffmpegProcess.HasExited)
- {
- ffmpegProcess.StandardInput.WriteLine("q"); // Sending 'q' to FFmpeg to stop recording
- ffmpegProcess.Kill();
- ffmpegProcess.WaitForExit();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment