11

我已经设置了服务器和视频流,以便我可以使用以下命令行通过 ffplay 连接到流:

ffplay rtmp://<IP>/path

是否可以使用 QMediaPlayer QMediaContent 或其他东西连接到这个流?

或者我可以使用 ffserver 创建的任何其他类型的流。

使用与 ffplay 相同的路径会导致“不支持的 url 方案!”

通过进一步的实验,我尝试了 ffserver http 服务器流,但最后以 Qt 在 MFStreamer::doRead() 中崩溃而告终

显然它应该为 MFStreamer 调用 BeginRead,但它没有。

如何使用 QMediaPlayer 播放视频流?

编辑:这是我的代码

视频测试.cpp

#include "videotest.h"
#include <QVBoxLayout>
#include <QVideoWidget>
#include <qmediaplayer.h>
#include <QMediaContent>
#include <QNetworkAccessManager>
#include <QNetworkReply>

struct VideoTest::Private
{
    QMediaPlayer * mediaPlayer;
    QNetworkAccessManager * networkAccessManager;
    QNetworkReply * reply;
};

VideoTest::VideoTest(QWidget *parent)
    : QMainWindow(parent)
{
    d = new Private;
    d->mediaPlayer = new QMediaPlayer(this, QMediaPlayer::StreamPlayback);
    d->networkAccessManager = new QNetworkAccessManager(this);
    ui.setupUi(this);

    QVideoWidget * videoWidget = new QVideoWidget(ui.centralWidget);
    videoWidget->show();
    QPalette palette = videoWidget->palette();
    palette.setColor(QPalette::Background, QColor(0, 0, 0));
    videoWidget->setPalette(palette);

    ui.videoLayout->addWidget(videoWidget);
    d->mediaPlayer->setVideoOutput(videoWidget);

    connect(ui.playButton, SIGNAL(clicked()), d->mediaPlayer, SLOT(play()));
    connect(ui.pauseButton, SIGNAL(clicked()), d->mediaPlayer, SLOT(pause()));
    connect(ui.videoUrlEdit, SIGNAL(editingFinished()), this, SLOT(sourceChanged()));
    connect(d->mediaPlayer, SIGNAL(error()), this, SLOT(stateChanged()));
    connect(d->mediaPlayer, SIGNAL(stateChanged), this, SLOT(stateChanged()));
}

VideoTest::~VideoTest()
{
    delete d;
}

void VideoTest::sourceChanged()
{
    d->reply = d->networkAccessManager->get(QNetworkRequest(ui.videoUrlEdit->text()));
    if(d->reply)
    {
        connect(d->reply, SIGNAL(readyRead()), this, SLOT(networkRequestReady()));
    }
}

void VideoTest::stateChanged()
{
    QString text = ui.textEdit->toPlainText();
    text.append("\n").append(d->mediaPlayer->errorString()).append(" : ").append(d->mediaPlayer->mediaStatus());
    ui.textEdit->setText(text);
}

void VideoTest::networkRequestReady()
{
    d->mediaPlayer->setMedia(QMediaContent(), d->reply);
}

视频测试.h

#ifndef VIDEOTEST_H
#define VIDEOTEST_H

#include <QtWidgets/QMainWindow>
#include "ui_videotest.h"

class VideoTest : public QMainWindow
{
    Q_OBJECT

public:
    VideoTest(QWidget *parent = 0);
    ~VideoTest();

public slots:
    void sourceChanged();
    void stateChanged();
    void networkRequestReady();

private:
    Ui::VideoTestClass ui;
    struct Private;
    Private * d;
};

#endif // VIDEOTEST_H
4

2 回答 2

4

我找到了让它工作的方法。

我放弃了Qt。Qt 的人坚持认为它应该可以工作,但无法产生任何有效的配置。他们说如果你从 VLC 流式传输它应该可以工作,但我没有让它工作。我还尝试了 ffmpeg、ffserver 和 nginx rtmp 流。我让这些东西与 mplayer、ffplay、VLC 和一些甚至与 Windows Media Player 一起工作,但从来没有 QMediaPlayer。

我试图将 URL 提供给 setMedia。我尝试制作一个自定义 QIODevice 来读取流数据并将该数据提供给使用 StreamPlayback 初始化的 QMediaPlayer,但它无法成功读取数据。

最后,我所需要的只是播放流的东西,是一个 QWidget 并且没有 GPL 许可。

我使用了 libVLCvlc-qt两者都很好用。

遵循这些说明很容易,但您需要记住将头文件从vlc-qt/windows/vlc_headers/2.2/ 复制vlc/sdk/include/vlc/plugins (sic)。这很重要,如果您不这样做,您可能会在编译期间遇到错误。请注意,如果您的平台版本与我的不匹配,这些路径可能会有所不同。此外,当您阅读本文时,可能没有必要。

视频测试.h

#ifndef VIDEOTEST_H_
#define VIDEOTEST_H_

#include <QtWidgets/QMainWindow>
#include "ui_videotest.h"

class VideoTest: public QMainWindow
{
    Q_OBJECT

    public:
        VideoTest(QWidget * p_parent = 0);
        ~VideoTest();

    public slots:
        void sourceChanged();

    private:
        struct Private;
        Private * d;
        Ui::VideoTestClass ui;
};

#endif

视频测试.cpp

#include "videotest.h"

#include <vlc-qt/Common.h>
#include <vlc-qt/Instance.h>
#include <vlc-qt/Media.h>
#include <vlc-qt/MediaPlayer.h>
#include <vlc-qt/WidgetVideo.h>

struct VideoTest::Private
{
    VlcInstance * vlcInstance;
    VlcMediaPlayer * vlcMediaPlayer;
    VlcMedia * vlcMedia;
    VlcWidgetVideo * vlcVideoWidget;
};

VideoTest::VideoTest(QWidget * p_parent)
{
    d = new Private();
    ui.setupUi(this);

    d->vlcMedia = 0;
    d->vlcInstance = new VlcInstance(VlcCommon::args(), this);
    d->vlcMediaPlayer = new VlcMediaPlayer(d->vlcInstance);
    d->vlcVideoWidget = new VlcWidgetVideo(this);

    d->vlcMediaPlayer->setVideoWidget(d->vlcVideoWidget);
    d->vlcVideoWidget->setMediaPlayer(d->vlcMediaPlayer);

    ui.videoLayout->addWidget(d->vlcVideoWidget);

    connect(ui.playButton, SIGNAL(clicked()), d->vlcMediaPlayer, SLOT(play()));
    connect(ui.pauseButton, SIGNAL(clicked()), d->vlcMediaPlayer, SLOT(pause()));
    connect(ui.videoUrlEdit, SIGNAL(editingFinished()), this, SLOT(sourceChanged()));
}

VideoTest::~VideoTest()
{
    delete d->vlcInstance;
    delete d->vlcMediaPlayer;
    delete d->vlcMedia;

    delete d;
}

VideoTest::sourceChanged()
{
    QUrl url(ui.videoUrlEdit->test());
    if(url.isValid() == false)
    {
        return;
    }

    d->vlcMediaPlayer->stop();

    delete d->vlcMedia;
    d->vlcMedia = new VlcMedia(url.toString(), d->vlcInstance);
    d->vlcMediaPlayer->open(d->vlcMedia);
}

VideoTest.ui

自己做,我不为你工作:D

只需确保它具有将插入视频小部件的 pauseButton、playButton、videoUrlEdit(QLineEdit) 和 videoLayout。

于 2015-06-05T07:17:06.093 回答
0

我刚刚设法使用 C++ QMediaPlayer 在 QML VideoOutput 中播放流,setMedia(QUrl("http://127.0.0.1:8080"));
该流是由 VLC 媒体播放器使用 HTTP 到 8080 端口创建的。我还成功地将 VLC 媒体播放器创建的流播放到本地文件,方法是通过setMedia(QMediaContent(), &localFile);.

于 2019-10-10T11:27:08.720 回答