anrayliu / pyvidplayer2

Reliable, easy, and fast video playing in Python
MIT License
35 stars 10 forks source link

videoplayer: Use code to control playback progress, play and pause #26

Closed EvenYeah closed 5 months ago

EvenYeah commented 6 months ago

using class videoplayer, how to control playback progress, play and pause by coding

anrayliu commented 6 months ago

When you create a VideoPlayer and pass it a Video object, you can save the Video object and use it directly manipulate the VideoPlayer. For example, to pause the video, just use video.pause() as normal.

EvenYeah commented 6 months ago

thanks!and there's another problem. to display two windows simultaneously, I used two threads and played the video in one thread,and when i tried to use method video.seek(time=1) in the main thread, it showed 'Assertion fctx->async_lock failed at libavcodec/pthread_frame.c:173', i tried the mutex way, but it didn't work out

EvenYeah commented 6 months ago

plus video.toggle_pause( ) worked fine,

anrayliu commented 6 months ago

You don't have to use threading to play videos in parallel. Here is the code from many_videos_demo.py found in the examples folder.

import pygame
from pyvidplayer2 import Video, VideoPlayer

win = pygame.display.set_mode((1066, 744))
pygame.display.set_caption("many videos demo")

videos = [VideoPlayer(Video(r"resources\billiejean.mp4"), (0, 0, 426, 240),  interactable=False),
          VideoPlayer(Video(r"resources\trailer1.mp4"), (426, 0, 256, 144), interactable=False),
          VideoPlayer(Video(r"resources\medic.mov"), (682, 0, 256, 144), interactable=False),
          VideoPlayer(Video(r"resources\trailer2.mp4"), (426, 144, 640, 360), interactable=False),
          VideoPlayer(Video(r"resources\clip.mp4"), (0, 240, 256, 144), interactable=False),
          VideoPlayer(Video(r"resources\birds.avi"), (0, 384, 426, 240), interactable=False),
          VideoPlayer(Video(r"resources\ocean.mkv"), (426, 504, 426, 240), interactable=False)]

while True:
    key = None
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            [video.close() for video in videos]
            pygame.quit()
            exit()
        elif event.type == pygame.KEYDOWN:
            key = pygame.key.name(event.key)

    pygame.time.wait(16)

    win.fill("white")

    [video.update() for video in videos]
    [video.draw(win) for video in videos]

    pygame.display.update()

As you can see, as long as every video is updated each frame, they will all play seperately.