基于VLC實作RTSP推流視訊
- 一、添加VLC頭檔案和庫檔案
- 二、封裝RTSPServer推流類
- 三、測試代碼
不清楚推流大概原理的小夥伴,參考《設定VLC播放器進行RTSP推流視訊》
這裡以VLC 2.2.4版本為例,我們下載下傳VLC播放器,其中帶有VLC的SDK,下載下傳位址:
http://download.videolan.org/vlc/2.2.4/win64/vlc-2.2.4-win64.7z
一、添加VLC頭檔案和庫檔案
我們建立一個測試工程RSTPVideo,在pro檔案中添加如下内容:
win32 {
# VLC相關庫
LIBS += -L$$PWD/../VLC/lib -llibvlc -llibvlccore
# VLC頭檔案目錄
INCLUDEPATH += $$PWD/../VLC/include
}
二、封裝RTSPServer推流類
封裝一個RSTPServer類,實作推流視訊的主要邏輯代碼。
RTSPServer.h
#ifndef RTSPSERVER_H
#define RTSPSERVER_H
#include <QString>
#include <vlc/vlc.h>
class RTSPServer
{
public:
RTSPServer();
~RTSPServer();
// 使用VLC實作RTSP推流。将視訊檔案轉碼後,推流到指定ip的端口上,協定一般為udp。
// 在VLC播放器中輸入rtsp://xx.xx.xx.xx:端口/,即可播放推流的視訊
// 推流到本地時,如rtsp://127.0.0.1:8554/,即可播放推流的視訊
bool pushVideo(const QString& ip, int port, const QString& filePath);
private:
libvlc_instance_t *instance;
};
#endif // RTSPSERVER_H
RTSPServer.cpp
#include "RTSPServer.h"
#include <QDir>
#define VIDEO_MEDIA_NAME "Video"
RTSPServer::RTSPServer()
{
// 建立VLC執行個體
instance = libvlc_new (0, nullptr);
}
RTSPServer::~RTSPServer()
{
// 停止推流
libvlc_vlm_stop_media(instance, VIDEO_MEDIA_NAME);
// 釋放VLC執行個體
libvlc_vlm_release(instance);
instance = nullptr;
}
bool RTSPServer::pushVideo(const QString &ip, int port, const QString &filePath)
{
// 轉碼參數:#transcode{vcodec=h264,acodec=mpga,ab=128,channels=2,samplerate=44100,scodec=none}
QString convertPara = "#transcode{vcodec=h264,acodec=mpga,ab=128,channels=2,samplerate=44100,scodec=none}";
// 網絡參數:rtp{sdp=rtsp://xx.xx.xx.xx:yyyy/}
// 表示本機ip時,可省略ip,隻寫端口,如rtp{sdp=rtsp://:8554/}
QString netPara = "rtp{sdp=rtsp://" + ip + ":" + QString::number(port) + "/}";
// 如sout = "#transcode{vcodec=h264,acodec=mpga,ab=128,channels=2,samplerate=44100,scodec=none}:rtp{sdp=rtsp://127.0.0.1:8554/}"
QString sout = convertPara + ":" + netPara;
// 将推流視訊路徑轉換為本地系統風格,win下"a\\b\\c",linux下"a/b/c"
QString path = QDir::toNativeSeparators(filePath);
// 添加名為VIDEO_MEDIA_NAME的廣播
int ret = libvlc_vlm_add_broadcast(instance, VIDEO_MEDIA_NAME,
path.toStdString().c_str(),
sout.toStdString().c_str(),
0, nullptr, true, false);
if (ret != 0)
{
return false;
}
// 播放該廣播
ret = libvlc_vlm_play_media(instance, VIDEO_MEDIA_NAME);
return (ret == 0);
}
三、測試代碼
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "RTSPServer.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
RTSPServer rtspServer;
};
#endif // MAINWINDOW_H
mainwindow.cpp
void MainWindow::on_pushButton_clicked()
{
bool ret = rtspServer.pushVideo("127.0.0.1", 8554, qApp->applicationDirPath() + "/video.mp4");
if (ret)
{
QMessageBox::information(nullptr, "Info", "Push streaming video successfully");
}
else
{
QMessageBox::information(nullptr, "Info", "Failed to push streaming video");
}
ui->pushButton->setEnabled(false);
}
代碼非常簡單,将程式所在目錄下的video.mp4視訊檔案,推流到127.0.0.1,8554端口上。
注意:運作時,依賴的動态庫有,libvlc.dll、libvlccore.dll、plugins插件,如下:
程式啟動後,點選“Push Video”按鈕啟動推流;然後在VLC播放器中,打開如下串流位址
運作效果如下:
左邊播放器正常播放推流視訊。
本文涉及工程代碼:
https://gitee.com/bailiyang/cdemo/tree/master/Qt/63VLCTest/RTSPVideo
若對你有幫助,歡迎點贊、收藏、評論,你的支援就是我的最大動力!!!
同時,阿超為大家準備了豐富的學習資料,歡迎關注公衆号“超哥學程式設計”,即可領取。