C# +FFmpeg播放RTSP H264视频流
时间: 2024-06-03 16:03:32 浏览: 18
在C#中使用FFmpeg播放RTSP H264视频流可以通过使用FFmpeg库和C#的Process类来实现。下面是一个简单的示例代码:
```csharp
using System;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
string ffmpegPath = "path/to/ffmpeg.exe";
string rtspUrl = "rtsp://example.com/stream";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = ffmpegPath;
startInfo.Arguments = $"-i {rtspUrl} -vcodec copy -f mpegts -";
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
Process ffmpegProcess = new Process();
ffmpegProcess.StartInfo = startInfo;
ffmpegProcess.OutputDataReceived += FfmpegOutputDataReceived;
ffmpegProcess.Start();
ffmpegProcess.BeginOutputReadLine();
Console.WriteLine("Press any key to stop playback...");
Console.ReadKey();
ffmpegProcess.Kill();
}
static void FfmpegOutputDataReceived(object sender, DataReceivedEventArgs e)
{
// Handle the output data received from FFmpeg
Console.WriteLine(e.Data);
}
}
```
上述代码中,需要将`ffmpegPath`替换为你本地FFmpeg可执行文件的路径,`rtspUrl`替换为你要播放的RTSP视频流的URL。代码中使用`Process`类启动FFmpeg进程,并通过`RedirectStandardOutput`属性和`OutputDataReceived`事件来获取FFmpeg的输出数据。
请注意,上述代码只是一个简单的示例,你可能需要根据你的具体需求进行更多的配置和处理。