gpt4 book ai didi

c# - 如何从 C# 应用程序在 Windows 中暂停/恢复 ffmpeg 视频捕获

转载 作者:行者123 更新时间:2023-11-30 17:32:58 30 4
gpt4 key购买 nike

我正在使用 ffmpeg 通过 C# 应用程序捕获屏幕和音频。我用来执行 ffmpeg 的命令如下:

ffmpeg -f gdigrab -i desktop -f dshow -i audio="Microphone (Realtek High Definition Audio)" -v
codec libx264 output.mp4

现在我想暂停/恢复录制并想从 C# 应用程序控制它。无论如何我可以做到这一点吗?

最佳答案

在项目中添加Helper类如下:

static class Helper
{
public static T[] ToArray<T>(this ICollection collection)
{
var items = new T[collection.Count];
collection.CopyTo(items, 0);

return items;
}
}

将类添加到您的项目中,如下所示:

 public static class ProcessExtensions
{
#region Methods

public static void Suspend(this Process process)
{
Action<ProcessThread> suspend = pt =>
{
var threadHandle = NativeMethods.OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)pt.Id);

if (threadHandle != IntPtr.Zero)
{
try
{
NativeMethods.SuspendThread(threadHandle);
}
finally
{
NativeMethods.CloseHandle(threadHandle);
}
};
};

var threads = process.Threads.ToArray<ProcessThread>();

if (threads.Length > 1)
{
Parallel.ForEach(threads, new ParallelOptions { MaxDegreeOfParallelism = threads.Length }, pt =>
{
suspend(pt);
});
}
else
{
suspend(threads[0]);
}
}

public static void Resume(this Process process)
{
Action<ProcessThread> resume = pt =>
{
var threadHandle = NativeMethods.OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)pt.Id);

if (threadHandle != IntPtr.Zero)
{
try
{
NativeMethods.ResumeThread(threadHandle);
}
finally
{
NativeMethods.CloseHandle(threadHandle);
}
}
};

var threads = process.Threads.ToArray<ProcessThread>();

if (threads.Length > 1)
{
Parallel.ForEach(threads, new ParallelOptions { MaxDegreeOfParallelism = threads.Length }, pt =>
{
resume(pt);
});
}
else
{
resume(threads[0]);
}
}

#endregion

#region Interop

static class NativeMethods
{
[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseHandle(IntPtr hObject);

[DllImport("kernel32.dll")]
public static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId);

[DllImport("kernel32.dll")]
public static extern uint SuspendThread(IntPtr hThread);

[DllImport("kernel32.dll")]
public static extern uint ResumeThread(IntPtr hThread);
}

[Flags]
enum ThreadAccess : int
{
TERMINATE = (0x0001),
SUSPEND_RESUME = (0x0002),
GET_CONTEXT = (0x0008),
SET_CONTEXT = (0x0010),
SET_INFORMATION = (0x0020),
QUERY_INFORMATION = (0x0040),
SET_THREAD_TOKEN = (0x0080),
IMPERSONATE = (0x0100),
DIRECT_IMPERSONATION = (0x0200)
}

#endregion
}

启动ffmpeg如下:

process =new Process();

process.StartInfo.FileName = "ffmpeg";
process.StartInfo.Arguments = "-i foo.VOB blabla.mp4";

process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;

process.Start();

使用 process.Suspend(); 暂停,使用 process.Resume(); 恢复。 注意 ffmpeg 必须从您的应用程序开始。我在 Windows 应用程序中对其进行了测试,它运行良好。

关于c# - 如何从 C# 应用程序在 Windows 中暂停/恢复 ffmpeg 视频捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45206312/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com