V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
yuhangch
2.53D
V2EX  ›  C#

C# 调用 ffmpeg 录制流,手动关闭遇到问题需要大佬帮助

  •  
  •   yuhangch · Sep 14, 2021 · 2294 views
    This topic created in 1689 days ago, the information mentioned may be changed or developed.

    环境 .net 4.5

    需求是这样,设计两个 rest 请求,一个启动录制(调用 ffmpeg ),一个停止录制(找到 ffmpeg 进程关掉它):

        const string ffmpegPath = "/ffmpeg.exe";
        var output = @"c:/···/a.mp4";
        var url =  ···; 
        var cmdStr = $@"-i ""{url}"" {output} -y";
    
        var startInfo = new ProcessStartInfo
        {
            FileName = ffmpegPath,
            Arguments = cmdStr,
            // UseShellExecute = false,
            // RedirectStandardInput = true,
            WindowStyle = ProcessWindowStyle.Minimized
        };
    
    
    
        var rp = Process.Start(startInfo);
    
    

    情况是这样的,使用 terminal 启动 ffmpeg 来录制视频流时,ctrl+c、手动关闭窗口,视频文件是完整的,但直接关闭进程视频文件就损坏了。

    也就是说Process.Kill() 直接关闭进程不能用。

    手动关闭窗口倒是可以,在 startInfo 里:

      UseShellExecute = true
        
        ···
        
        //之后调用
      Process.CloseMainWindow()
    

    但这种方式会弹出一个 cmd 窗口,即使把它调成最小化,还是感觉不太能用。

    那么如果想要在UseShellExecute = false的情况下,模拟关闭窗口或者模拟ctrl+c命令要怎么做?

    请彦祖们指点!

    2 replies    2021-09-14 19:39:16 +08:00
    jifengg
        1
    jifengg  
       Sep 14, 2021
    ffmpeg cli 有一个快捷键“q“,用于退出。获取 ffmpeg process 的 stdin,往里 write 一个"q"试试。
    其次,ctrl+c 是发送一个 SIGINT 信号,可以看看 win sdk ( C#太久没用忘记了),模拟发送一个信号。
    tux
        2
    tux  
       Sep 14, 2021
    录制模式好像是有参数,专门解决这个问题 类似这样 请参考 --hls-prefer-ffmpeg --hls-use-mpegts
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2766 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 11:02 · PVG 19:02 · LAX 04:02 · JFK 07:02
    ♥ Do have faith in what you're doing.