先看拉取的视频流效果:
代码如下:
一开始打算使用python写多路视频推流,但在ubuntu系统上搞了好久就是搞不定openh264导致的错误,然后改用c++了,代码如下,我这里推了两路视频流,一路是网络摄像头,另一路是MP4视频文件,环境还是之前的,可参考以下两篇博文:
Docker中创建nginx-rtmp流媒体服务器
Docker中使用nginx-rtmp推拉网络摄像头视频流
#include <iostream>
#include <thread>void executeCommand(const char* command) {int result = std::system(command);if (result == 0) {std::cout << "Command executed successfully." << std::endl;} else {std::cerr << "Command execution failed." << std::endl;}
}int main() {// 构建推流命令const char* command1 = "ffmpeg -use_wallclock_as_timestamps 1 -rtsp_transport tcp -i rtsp://admin:admin@网络摄象头ip -c:v h264 -b:v 1000k -c:a aac -ar 44100 -f flv rtmp://流媒体服务器IP:1935/stream/example1";// 构建另一个推流命令const char* command2 = "ffmpeg -re -stream_loop -1 -i ./5-2.mp4 -vcodec copy -acodec aac -ar 44100 -f flv rtmp://流媒体服务器IP:1935/stream/example2";// 使用线程同时执行两个命令std::thread thread1(executeCommand, command1);std::thread thread2(executeCommand, command2);// 等待线程执行完毕thread1.join();thread2.join();return 0;
}
编译命令:
g++ -o multffmpeg-test multffmpeg-test.cpp -pthread
运行:
./multffmpeg-test