FMaz008
FMaz008

Reputation: 11295

How can I make a video restart (loop) after it is done playing?

I'm trying to make a video which will stay pause on the first frame for 5 second, then play until the end, and rewind (then stay on the first frame for 5 sec, then play again)

So far I have this code, which detects when the video is done playing, but it does not go back to the beginning, it just stay on the last frame of the video.

Basically the set_position(0) does not seem to work once the video is done playing.

import vlc
import time

# Path to your video file
video_path = "vid.mp4"

# Create VLC instance
vlc_instance = vlc.Instance('')

# Create media player
player = vlc_instance.media_player_new()

# Load the video file
media = vlc_instance.media_new(video_path)
player.set_media(media)
#player.PlaybackMode("loop")

# Register an event manager
event_manager = player.event_manager()

# Define a callback function for the EndReached event
def on_end_reached(event):
    restart_video(player)

# Connect the EndReached event to the callback function
event_manager.event_attach(vlc.EventType.MediaPlayerEndReached, on_end_reached)


def restart_video(player):
    print("Putting the video at the beginning")
    player.set_position(0)
    player.play()
    time.sleep(0.5)
    player.pause()  # Pause the video as the beginning
    
    time.sleep(5)
    player.play()
    
restart_video(player)

Upvotes: 0

Views: 204

Answers (0)

Related Questions