forked from Mercury/2dxAutoClip
Fix disposing wasapi audio device on chart exit
This commit is contained in:
parent
a57c1c2131
commit
426268b512
|
@ -13,7 +13,7 @@ class Program
|
|||
private static Process? _ffmpegProcess;
|
||||
private static string _ffmpegFolderPath = "E:\\autorecording"; // Output folder path
|
||||
private static WasapiCapture? _waveSource;
|
||||
private static WaveFileWriter? _waveFile;
|
||||
private static WaveFileWriter _writer;
|
||||
private static string _audioFilePath;
|
||||
private static string _videoFilePath;
|
||||
|
||||
|
@ -122,40 +122,27 @@ class Program
|
|||
Directory.CreateDirectory(outputFolder);
|
||||
|
||||
// Set up the WasapiLoopbackCapture
|
||||
using var capture = new WasapiLoopbackCapture();
|
||||
var writer = new WaveFileWriter(_audioFilePath, capture.WaveFormat);
|
||||
_waveSource = new WasapiLoopbackCapture();
|
||||
_writer = new WaveFileWriter(_audioFilePath, _waveSource.WaveFormat);
|
||||
|
||||
// Handle the DataAvailable event
|
||||
capture.DataAvailable += (sender, args) =>
|
||||
_waveSource.DataAvailable += (sender, args) =>
|
||||
{
|
||||
writer.Write(args.Buffer, 0, args.BytesRecorded);
|
||||
|
||||
// Optional: Stop recording after a certain time
|
||||
if (writer.Position > capture.WaveFormat.AverageBytesPerSecond * 20) // 20 seconds
|
||||
{
|
||||
capture.StopRecording();
|
||||
}
|
||||
_writer.Write(args.Buffer, 0, args.BytesRecorded);
|
||||
};
|
||||
|
||||
// Handle the RecordingStopped event
|
||||
capture.RecordingStopped += (sender, args) =>
|
||||
_waveSource.RecordingStopped += (sender, args) =>
|
||||
{
|
||||
writer.Dispose();
|
||||
writer = null;
|
||||
capture.Dispose();
|
||||
_writer.Dispose();
|
||||
_writer = null;
|
||||
_waveSource.Dispose();
|
||||
_waveSource = null;
|
||||
};
|
||||
|
||||
// Start recording
|
||||
capture.StartRecording();
|
||||
_waveSource.StartRecording();
|
||||
Console.WriteLine("WASAPI Audio recording started.");
|
||||
|
||||
// Wait for the recording to stop
|
||||
while (capture.CaptureState != CaptureState.Stopped)
|
||||
{
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
|
||||
Console.WriteLine("WASAPI Audio recording stopped.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
@ -178,15 +165,13 @@ class Program
|
|||
_ffmpegProcess.StartInfo.FileName = "ffmpeg";
|
||||
_ffmpegProcess.StartInfo.Arguments = ffmpegArguments;
|
||||
_ffmpegProcess.StartInfo.UseShellExecute = false;
|
||||
_ffmpegProcess.StartInfo.RedirectStandardOutput = true;
|
||||
_ffmpegProcess.StartInfo.RedirectStandardOutput = false;
|
||||
_ffmpegProcess.StartInfo.RedirectStandardError = true;
|
||||
_ffmpegProcess.StartInfo.CreateNoWindow = true;
|
||||
|
||||
_ffmpegProcess.OutputDataReceived += (_, args) => Console.WriteLine(args.Data);
|
||||
_ffmpegProcess.ErrorDataReceived += (_, args) => Console.WriteLine(args.Data);
|
||||
|
||||
_ffmpegProcess.Start();
|
||||
_ffmpegProcess.BeginOutputReadLine();
|
||||
_ffmpegProcess.BeginErrorReadLine();
|
||||
}
|
||||
|
||||
|
@ -200,12 +185,20 @@ class Program
|
|||
private static void StopAudioRecording()
|
||||
{
|
||||
if (_waveSource == null) return;
|
||||
|
||||
_waveSource.StopRecording();
|
||||
_waveSource.Dispose();
|
||||
_waveSource = null;
|
||||
if (_writer != null)
|
||||
{
|
||||
_writer.Dispose();
|
||||
_writer = null;
|
||||
}
|
||||
|
||||
Console.WriteLine("Audio recording stopped.");
|
||||
}
|
||||
|
||||
|
||||
private static void StopFfmpegRecording()
|
||||
{
|
||||
if (_ffmpegProcess != null && _ffmpegProcess.HasExited) return;
|
||||
|
@ -219,13 +212,14 @@ class Program
|
|||
private static void CombineAudioAndVideo(string videoFilePath, string audioFilePath, string songName)
|
||||
{
|
||||
var combinedOutputFilePath = $"{_ffmpegFolderPath}\\{songName}_combined_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.mkv";
|
||||
var ffmpegArgs = $"-y -i \"{videoFilePath}\" -i \"{audioFilePath}\" -c:v copy -c:a aac -strict experimental \"{combinedOutputFilePath}\"";
|
||||
var ffmpegArgs =
|
||||
$"-y -i \"{videoFilePath}\" -i \"{audioFilePath}\" -c:v copy -c:a aac -strict experimental \"{combinedOutputFilePath}\"";
|
||||
|
||||
var processInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = "ffmpeg",
|
||||
Arguments = ffmpegArgs,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardOutput = false,
|
||||
RedirectStandardError = true,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true
|
||||
|
@ -238,17 +232,16 @@ class Program
|
|||
return;
|
||||
}
|
||||
|
||||
var output = process.StandardOutput.ReadToEnd();
|
||||
var error = process.StandardError.ReadToEnd();
|
||||
|
||||
process.WaitForExit();
|
||||
|
||||
Console.WriteLine("FFmpeg Output: " + output);
|
||||
Console.WriteLine("FFmpeg Error: " + error);
|
||||
|
||||
Console.WriteLine(File.Exists(combinedOutputFilePath)
|
||||
? "Audio and video have been successfully combined."
|
||||
: "Failed to combine audio and video. Check the logs for errors.");
|
||||
File.Delete(videoFilePath);
|
||||
File.Delete(audioFilePath);
|
||||
}
|
||||
|
||||
}
|
|
@ -13,7 +13,7 @@ using System.Reflection;
|
|||
[assembly: System.Reflection.AssemblyCompanyAttribute("2dxAutoClip")]
|
||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+bc7244cc9260f9dc5bd7601f412243f4172cb980")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+a57c1c2131efda17587033bc71e491a80341c7a5")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("2dxAutoClip")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("2dxAutoClip")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
|
|
@ -1 +1 @@
|
|||
dd66343c077a7a4fb066a7cf71b273cc5e05053f50e94596cc83ffbf03d2e4ab
|
||||
3e6cb3d517cf16f5024618620589e2a0f678470e27b98cc7a5015180d535c471
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in a new issue