gpt4 book ai didi

c# - 录制桌面时如何停止ffmpeg将文件保存到硬盘?

转载 作者:行者123 更新时间:2023-12-04 22:56:39 27 4
gpt4 key购买 nike

我正在尝试使用 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 的黑色窗口?
ffmpeg black window
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();
}
}
}

最佳答案

当我们开始录制时,会出现以下消息:

Press [q] to stop, [?] for help.


将 'q' 写入 stdin模拟按压 q键,然后停止录制。
  • 使用 RedirectStandardInput = true 启动 FFmpeg 子进程:
     process.StartInfo.RedirectStandardInput = true;
    process.Start();
  • q stdin 的 key FFmpeg 子流程的管道:
     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);
    }
    }
    }

    关于c# - 录制桌面时如何停止ffmpeg将文件保存到硬盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72769085/

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