我正在嘗試使用 ffmpeg 錄制桌面并將視頻檔案保存到硬碟。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Testings
{
internal class FFmpeg_Capture
{
Process process;
public FFmpeg_Capture()
{
process = new Process();
}
public void Start(string FileName, int Framerate)
{
process.StartInfo.FileName = @"D:\Captured Videos\ffmpeg.exe"; // Change the directory where ffmpeg.exe is.
process.EnableRaisingEvents = false;
process.StartInfo.WorkingDirectory = @"D:\Captured Videos"; // The output directory
process.StartInfo.Arguments = @"-f gdigrab -framerate " Framerate
" -i desktop -preset ultrafast - pix_fmt yuv420p " FileName;
process.Start();
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = false;
Stop();
}
public void Stop()
{
process.Close();
}
}
}
并在 form1 中使用它:
private void btnRecord_Click(object sender, EventArgs e)
{
recordToggle = !recordToggle;
if (recordToggle)
{
btnRecord.Text = "Stop";
record.Start("Testing", 60);
}
else
{
btnRecord.Text = "Record";
record.Stop();
}
}
但檔案測驗從未保存到硬碟上。我的猜測是
process.Close();
不像 ctrl c 和 ctrl c 是停止 ffmpeg 并保存檔案的原因。
這是有效的,但如何洗掉 ffmpeg 的黑色視窗?

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Testings
{
internal class FFmpeg_Capture
{
Process process;
public FFmpeg_Capture()
{
process = new Process();
}
public void Start(string FileName, int Framerate)
{
process.StartInfo.FileName = @"D:\Captured Videos\ffmpeg.exe"; // Change the directory where ffmpeg.exe is.
process.EnableRaisingEvents = false;
process.StartInfo.WorkingDirectory = @"D:\Captured Videos\"; // The output directory
process.StartInfo.Arguments = @"-y -f gdigrab -framerate " Framerate
" -i desktop -preset ultrafast -pix_fmt yuv420p " FileName;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.RedirectStandardInput = true; //Redirect stdin
process.Start();
}
public void Stop()
{
byte[] qKey = Encoding.GetEncoding("gbk").GetBytes("q"); //Get encoding of 'q' key
process.StandardInput.BaseStream.Write(qKey, 0, 1); //Write 'q' key to stdin of FFmpeg sub-processs
process.StandardInput.BaseStream.Flush(); //Flush stdin (just in case).
process.Close();
}
}
}
uj5u.com熱心網友回復:
當我們開始錄制時,會出現以下訊息:
按 [q] 停止,按 [?] 尋求幫助。
寫“q”stdin模擬q按鍵,并停止錄音。
使用以下命令啟動 FFmpeg 子行程
RedirectStandardInput = true:process.StartInfo.RedirectStandardInput = true; process.Start();q將密鑰寫入stdinFFmpeg 子行程的管道:byte[] qKey = Encoding.GetEncoding("gbk").GetBytes("q"); process.StandardInput.BaseStream.Write(qKey, 0, 1);
完整的代碼示例:
using System.Diagnostics;
using System.Text;
namespace Testings
{
internal class FFmpeg_Capture
{
Process process;
public FFmpeg_Capture()
{
process = new Process();
}
public void Start(string FileName, int Framerate)
{
process.StartInfo.FileName = @"C:\FFmpeg\bin\ffmpeg.exe"; // Change the directory where ffmpeg.exe is.
process.EnableRaisingEvents = false;
process.StartInfo.WorkingDirectory = @"D:\"; // The output directory
process.StartInfo.Arguments = @"-y -f gdigrab -framerate " Framerate
" -i desktop -preset ultrafast -pix_fmt yuv420p " FileName;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.RedirectStandardInput = true; //Redirect stdin
process.Start();
System.Threading.Thread.Sleep(3000); //Wait 3 seconds (for testing).
Stop();
}
public void Stop()
{
byte[] qKey = Encoding.GetEncoding("gbk").GetBytes("q"); //Get encoding of 'q' key
process.StandardInput.BaseStream.Write(qKey, 0, 1); //Write 'q' key to stdin of FFmpeg sub-processs
process.StandardInput.BaseStream.Flush(); //Flush stdin (just in case).
process.Close();
}
}
class Program
{
static void Main(string[] args)
{
FFmpeg_Capture cap = new FFmpeg_Capture();
cap.Start("vid.mp4", 5);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/496691.html
上一篇:使按鈕與另一個按鈕動態保持距離
