gpt4 book ai didi

c# winforms显示ffmpeg输出

转载 作者:行者123 更新时间:2023-12-04 23:10:40 24 4
gpt4 key购买 nike

我正在开发一个应用程序,该应用程序可以使用网络摄像头和麦克风录制视频/音频,同时还可以在 winforms 上显示它。

Process.Start(new ProcessStartInfo
{
FileName = "ffmpeg",
Arguments = $"-f dshow -video_size 1280x720 -framerate 30 -vcodec mjpeg -i video=\"{video device}\":audio=\"{audio device}\" -pix_fmt yuv420p -f mp4 -movflags frag_keyframe+empty_moov {output}"
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
});
目前使用这个 ffmpeg 命令来记录和保存 mp4。如果我使用 pipe:1 作为输出而不是文件路径,我想知道如何在 winforms 中显示输出流。我以前使用过 aforge.Directshow,但保存的视频质量很差。欢迎任何其他有关如何执行此操作的建议。

最佳答案

这是一个启动 FF 并将事件处理程序附加到相关流程事件的类,因此它可以看到生成的输出数据。它在一个有趣的上下文中使用,知道音频是否已经开始或停止,这依赖于 ff 具有监控音频 channel 并在静音开始或停止时发送消息的扩展,但它演示了如何拥有自己的事件在这个类上,并在 ffmpeg 泵送感兴趣的消息时提出它们。大多数情况下,该类只是将输出捕获到日志中

public class FfmpegRecorder 
{
public event Action SilenceDetected;
public event Action NoiseDetected;

private StringBuilder _ffLog = new StringBuilder();
private Process _ffmpeg;
private string _streamStats;

public override void StartRecording()
{
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = Properties.Settings.Default.CommandLineFfmpegPath,
Arguments = string.Format(
Properties.Settings.Default.CommandLineFfmpegArgs,
OutputPath
),
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true
};

_ffmpeg = System.Diagnostics.Process.Start(psi);
_ffmpeg.OutputDataReceived += Ffmpeg_OutputDataReceived;
_ffmpeg.ErrorDataReceived += Ffmpeg_ErrorDataReceived;
_ffmpeg.BeginOutputReadLine();
_ffmpeg.BeginErrorReadLine();

_ffmpeg.PriorityClass = ProcessPriorityClass.High;
}

void Ffmpeg_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null && IsInteresting(e.Data))
{
_ffLog.Append("STDOUT: ").AppendLine(e.Data);
}
}
void Ffmpeg_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null && IsInteresting(e.Data))
{
_ffLog.Append("STDERR: ").AppendLine(e.Data);
}
}

bool IsInteresting(string data)
{
if (data.StartsWith("frame="))
{
_streamStats = data;
return false;
}

try
{
if (SilenceDetected != null && data.Contains("silence_start"))
SilenceDetected();
else if (NoiseDetected != null && data.Contains("silence_end"))
NoiseDetected();
}
catch { }

return true;
}




}
}

关于c# winforms显示ffmpeg输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69356234/

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