求助:如何将FFmpeg桌面RTSP推流命令转为.NET FFMpegWrapper参数?
问题解决:将FFmpeg桌面推流命令转换为.NET FFMpegConverter适配代码
问题背景
原本通过FFmpeg命令行可正常将桌面画面推流至RTSP服务器:
ffmpeg -f gdigrab -rtbufsize 100M -framerate 30 -probesize 10M -draw_mouse 1 -i desktop -c:v libx264 -r 30 -preset ultrafast -tune zerolatency -crf 25 -f rtsp -rtsp_transport tcp rtsp://1.1.1.1:8554/player/1ef8d9a5-b0b3-4dab-93a0-ac2bdef2f8aa/index.m3u8
但使用.NET的FFMpegConverter.ConvertLiveMedia方法时,因参数配置逻辑错误报错(提示输入需为流、desktop无效),需调整参数适配包装器的输入输出分离逻辑。
错误原因分析
原代码核心问题:
- 将**所有命令行参数(含输入配置)**全部塞到
CustomOutputArgs中,混淆了输入/输出参数的边界 - 直接将
inPath设为"desktop",但包装器无法自动识别这是gdigrab设备输入,需配合输入格式声明 - 错误指定输出格式为
Format.flv,实际应为RTSP格式
修正后的代码
Dim rtspUrl As String = "rtsp://1.1.1.1:8554/player/1ef8d9a5-b0b3-4dab-93a0-ac2bdef2f8aa/index.m3u8" Dim videoConv = New FFMpegConverter() ' 配置转换参数 Dim convertSettings As New ConvertSettings() With { ' 对应命令行的输入参数部分:-rtbufsize 100M -framerate 30 -probesize 10M -draw_mouse 1 .CustomInputArgs = "-rtbufsize 100M -framerate 30 -probesize 10M -draw_mouse 1", ' 对应命令行的输出编码及RTSP传输参数 .CustomOutputArgs = "-c:v libx264 -r 30 -preset ultrafast -tune zerolatency -crf 25 -rtsp_transport tcp", .VideoFrameRate = 30, .MaxDuration = 5 } ' 调用ConvertLiveMedia:明确输入格式为gdigrab,输入源为desktop,输出格式为rtsp Dim ffMpegTask = videoConv.ConvertLiveMedia( inputPath:="desktop", inputFormat:="gdigrab", outputPath:=rtspUrl, outputFormat:="rtsp", settings:=convertSettings ) ffMpegTask.Start()
参数对应说明
| 原命令行参数 | 包装器配置方式 |
|---|---|
-f gdigrab | 作为ConvertLiveMedia的inputFormat参数传入 |
-i desktop | 作为ConvertLiveMedia的inputPath参数传入 |
-rtbufsize 100M -framerate 30 -probesize 10M -draw_mouse 1 | 放入CustomInputArgs |
-c:v libx264 -r 30 -preset ultrafast -tune zerolatency -crf 25 | 放入CustomOutputArgs |
-f rtsp -rtsp_transport tcp | -f rtsp作为outputFormat参数,-rtsp_transport tcp放入CustomOutputArgs |
内容的提问来源于stack exchange,提问作者Luiz
相关产品推荐
相关产品推荐

