0

I am using a QMediaPlayer to output a video that contains audio to a QVideoWidget. When I use a video without audio it plays instantly, but if the video has audio, it takes a few seconds to play after calling player.play().

Is there a way to make a video with audio play instantly after calling play()?

I've tried following this answer but I'm getting Error: "failed to seek".

I am using Ubuntu 23.04, Python 3.11 and PyQt5.

Here's an example:

from PyQt5.QtWidgets import QApplication, QMainWindow, QWizard, QWizardPage, QPushButton, QVBoxLayout
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
from PyQt5.QtCore import QUrl
from PyQt5.QtMultimediaWidgets import QVideoWidget


class VideoPage(QWizardPage):
    def __init__(self):
        super().__init__()

        layout = QVBoxLayout()
        self.setLayout(layout)

        self.player = QMediaPlayer(self)
        self.media_content = QMediaContent(QUrl.fromLocalFile('path/to/vid.mp4'))
        self.player.setMedia(self.media_content)
        self.video = QVideoWidget()
        self.player.setVideoOutput(self.video)
        
        play_button = QPushButton('Play Video')
        play_button.clicked.connect(self.play_video)
        layout.addWidget(self.video)
        layout.addWidget(play_button)

    def play_video(self):
        self.player.play()

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    wizard = QWizard()
    video_page = VideoPage()
    wizard.addPage(video_page)
    wizard.setWindowTitle('Video Wizard')
    wizard.show()
    sys.exit(app.exec_())
Netanel
  • 103
  • 3
  • There could be various reasons for that delay, but if it's always consistent and it always takes "a few seconds" to start, it probably means that the video and audio streams do not begin at the same time, and to allow proper syncing one of them (presumably the audio track) is started *before* the video. It's even possible that, due to some encoding parameters, both tracks have a starting point that is "somewhere" after the 0-time of the stream. A simple check you could make is to do some tests with a few different videos taken from different sources, and see if the delay is always consistent. – musicamante Aug 19 '23 at 22:38
  • It probably will not, and in that case there's absolutely nothing you can do, except edit the video using an advanced editor or transcoder (like ffmpeg) in order to fix it. – musicamante Aug 19 '23 at 22:39

0 Answers0