valbok / QtAVPlayer

Free and open-source Qt Media Player library based on FFmpeg, for Linux, Windows, macOS, iOS and Android.
MIT License
282 stars 54 forks source link

Play video from buffer and append buffer while the video playing #486

Closed l2m2 closed 2 months ago

l2m2 commented 2 months ago

I would like to play video from buffer and append buffer while the video playing. Any ideas ?

some dirty code:

QAVPlayer p;
QSharedPointer<QIODevice> avBuffer(new QBuffer());
avBuffer->open(QIODevice::ReadWrite);
QSharedPointer<QAVIODevice> dev(new QAVIODevice(avBuffer));
p.setInputFormat(QStringLiteral("h264"));
p.setSource(QStringLiteral("buffer"), dev);

QtConcurrent::run([avBuffer](){
    QString fileName = QStringLiteral("xxx.dat");
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly)) {
        qDebug() << "open file failed.";
        return;
    }
    QByteArray buffer;
    while (1) {
        buffer.resize(64 * 1024);
        auto bytesRead = file.read(buffer.data(), buffer.size());
        if (bytesRead <= 0)
            break;
        if (bytesRead < buffer.size())
            buffer.resize(bytesRead);

        auto after = processByteArray(buffer);
        if (!after.isEmpty()) {
            avBuffer->write(after, after.size());
        }
    }
});
p.play();

Is it right?

geminixdev commented 2 months ago

Look at QIODevice, and implement your buffer within that. QIODevice has all to handle such situations.

Look also at QAVAuudioOutput, it contains a solution for exactly that, to add constantly new data (here audio data) for output.

valbok commented 2 months ago

Hi, there is an example https://github.com/valbok/QtAVPlayer/blob/master/examples/qml_video/main.cpp#L176

    QSharedPointer<QAVIODevice> qrc;
    QSharedPointer<QIODevice> io(new QFile(file));
        if (io->open(QIODevice::ReadOnly))
            qrc.reset(new QAVIODevice(io));
    p.setSource(file, qrc);

Just need to implement usual QIODevice, if need to play from a buffer.

valbok commented 2 months ago

please reopen if you have any questions