FFmpeg本身不支持直接输出RTSP流作为服务器,但你可以使用:
bash
ffmpeg -f gdigrab -framerate 15 -video_size 1920x1080 -i desktop -c:v libx264 -preset ultrafast -f rtsp rtsp://localhost:8554/mystream
需要先安装并运行RTSP服务器https://github.com/aler9/rtsp-simple-server
如果想使用本地MP4文件作为视频源推RTSP流,将命令修改如下:
bash
ffmpeg -re -i 1.mp4 -c copy -f rtsp rtsp://localhost:8554/mystream
1. 基础推流(保持原编码)
bash
ffmpeg -re -i 1.mp4 -c copy -f rtsp rtsp://localhost:8554/mystream
-
-re:以原始帧率读取输入文件(重要!否则会极速播放) -
-i 1.mp4:指定输入文件 -
-c copy:流复制,不重新编码(高效) -
-f rtsp:指定输出格式为RTSP
2.重新编码推流(需要转码时)
bash
ffmpeg -re -i 1.mp4 -c:v libx264 -preset ultrafast -c:a aac -f rtsp rtsp://localhost:8554/mystream
3. 带优化的完整命令
bash
ffmpeg -re -i 1.mp4 \
-c:v libx264 -preset ultrafast -tune zerolatency \
-c:a aac -ar 44100 -b:a 128k \
-f rtsp -rtsp_transport tcp rtsp://localhost:8554/mystream
4. 循环播放推流
bash
ffmpeg -re -stream_loop -1 -i 1.mp4 -c copy -f rtsp rtsp://localhost:8554/mystream
-stream_loop -1:无限循环播放
5. 指定分辨率/帧率推流
bash
ffmpeg -re -i 1.mp4 \
-vf "scale=1280:720" -r 25 \
-c:v libx264 -preset ultrafast \
-c:a aac \
-f rtsp rtsp://localhost:8554/mystream
常见使用场景
场景1:简单的文件推流
bash
ffmpeg -re -i 1.mp4 -c copy -f rtsp rtsp://192.168.1.100:554/live
场景2:推流到远程RTSP服务器(TCP传输)
bash
ffmpeg -re -i 1.mp4 \
-c:v libx264 -preset ultrafast \
-c:a aac \
-f rtsp -rtsp_transport tcp rtsp://192.168.1.244:554/live/test
场景3:多文件连续推流
bash
ffmpeg -re -f concat -safe 0 -i filelist.txt -c copy -f rtsp rtsp://localhost:8554/mystream
创建 filelist.txt:
file '1.mp4'
file '2.mp4'
file '3.mp4'
场景4:带水印推流
bash
ffmpeg -re -i 1.mp4 -i watermark.png \
-filter_complex "overlay=10:10" \
-c:v libx264 -preset ultrafast \
-c:a aac \
-f rtsp rtsp://localhost:8554/mystream
重要参数对比
| 原屏幕录制命令参数 | 文件推流对应参数 | 说明 |
|---|---|---|
-f gdigrab |
-re |
文件推流需要-re保持正常帧率 |
-i desktop |
-i 1.mp4 |
输入源改为文件 |
-video_size 1920x1080 |
(可选)-vf "scale=1920:1080" |
如果需要缩放 |
-framerate 15 |
(可选)-r 15 |
如果需要调整帧率 |
验证推流是否成功
使用FFplay播放测试:
ffplay rtsp://localhost:8554/mystream
使用VLC播放测试:
打开VLC → 媒体 → 打开网络串流 → 输入 rtsp://localhost:8554/mystream
常见问题解决
问题1:推流速度太快
原因 :缺少 -re 参数
解决 :添加 -re 参数
问题2:音频不同步
解决:
bash
ffmpeg -re -i 1.mp4 -async 1 -c copy -f rtsp rtsp://localhost:8554/mystream
问题3:连接被拒绝
解决:
-
确保RTSP服务器正在运行
-
检查端口是否开放
-
尝试使用TCP传输:
-rtsp_transport tcp -
最简单的命令就是:
bash
ffmpeg -re -i 1.mp4 -c copy -f rtsp rtsp://localhost:8554/mystream
解析
bash
推流到远程RTSP服务器(TCP传输
ffmpeg -re -i 1.mp4 \
-c:v libx264 -preset ultrafast \
-c:a aac \
-f rtsp -rtsp_transport tcp rtsp://192.168.1.244:554/live/test
| 组成部分 | 含义 | 说明 |
|---|---|---|
rtsp:// |
协议 | 使用RTSP(实时流协议) |
192.168.1.244 |
服务器IP地址 | 这是你要推流到的目标服务器 |
:554 |
端口号 | RTSP默认端口(可选,默认就是554) |
/live/test |
流路径/名称 | 流的访问路径,服务器端定义的 |
详细说明
目标服务器
-
IP地址 :
192.168.1.244 -
这是局域网内的一个设备(192.168.x.x是私有IP段)
-
可能是:另一台电脑、NAS、监控NVR、媒体服务器、路由器等
2. 端口
-
554是RTSP标准端口 -
如果你的服务器使用其他端口,需要相应修改
3. 流路径
-
/live/test是流的标识符 -
不同服务器可能有不同的路径格式:
-
rtsp://192.168.1.244/live(无具体流名) -
rtsp://192.168.1.244/stream1 -
rtsp://192.168.1.244/channel1
-
常见RTSP服务器类型
1. 专业的媒体服务器
-
Wowza、Nginx-rtmp-module(带RTSP支持)
-
路径通常是自定义的
2. 监控/NVR设备
-
海康威视:
rtsp://192.168.1.244:554/Streaming/Channels/101 -
大华:
rtsp://192.168.1.244:554/cam/realmonitor?channel=1&subtype=0
3. 开源RTSP服务器
-
RtspSimpleServer :
rtsp://192.168.1.244:8554/mystream -
Live555 :
rtsp://192.168.1.244/live/test
4. 软件应用程序
-
VLC(作为服务器)
-
FFmpeg(配合其他组件)
如何确定服务器地址和路径?
方法1:查看服务器配置
登录到 192.168.1.244 设备的管理界面,查看:
-
RTSP服务是否启用
-
端口号是多少
-
流路径格式是什么
方法2:尝试常见路径
bash
# 尝试几种常见路径
ffmpeg -re -i 1.mp4 -c copy -f rtsp rtsp://192.168.1.244/live
ffmpeg -re -i 1.mp4 -c copy -f rtsp rtsp://192.168.1.244/stream
ffmpeg -re -i 1.mp4 -c copy -f rtsp rtsp://192.168.1.244:8554/live
方法3:先测试连接
bash
# 使用FFprobe检测服务器
ffprobe rtsp://192.168.1.244:554
# 或尝试播放(如果服务器已有流)
ffplay rtsp://192.168.1.244:554/live
命令执行流程
bash
你的电脑(运行FFmpeg)
↓ 推送RTSP流(TCP协议)
↓
192.168.1.244:554(远程服务器)
↓
其他设备可以从服务器拉流观看
↓
播放地址:rtsp://192.168.1.244:554/live/test
验证服务器是否可达
1. Ping测试
bash
ping 192.168.1.244
2. 端口测试
bash
telnet 192.168.1.244 554
# 或
nc -zv 192.168.1.244 554
3. 查看服务器日志
在 192.168.1.244 服务器上查看是否有连接请求
常见错误及解决
错误1:连接拒绝
Connection refused
原因:服务器未运行RTSP服务或端口错误
错误2:认证失败
401 Unauthorized
解决:添加用户名密码
bash
rtsp://username:password@192.168.1.244:554/live/test
错误3:路径不存在
404 Not Found
原因:流路径不正确,需要确认服务器要求的路径格式
总结
你的命令是推流到:
-
服务器 :
192.168.1.244(局域网内的设备) -
端口:554(RTSP默认端口)
-
流路径 :
/live/test
如果你不知道这个服务器是否支持RTSP,需要:
-
确认该IP的设备确实运行了RTSP服务
-
确认端口和路径格式正确
-
或者先在自己电脑上搭建RTSP服务器测试(localhost)
最简单测试方法:
bash
# 先推流到本地测试
ffmpeg -re -i 1.mp4 -c copy -f rtsp rtsp://localhost:8554/test
# 再用播放器测试
ffplay rtsp://localhost:8554/test
确认本地工作正常后,再尝试推流到远程服务器 192.168.1.244。