作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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/
我是一名优秀的程序员,十分优秀!