Search code examples
pythonvlclibvlcpython-vlc

VLC trigger action when media reaches timestamp


I'd like to the ability to run some code when the timestamp of a file has been reached (i.e. trigger an alert)

How can this be achieved?

I was looking at this https://www.geeksforgeeks.org/python-vl ... edia-time/

However, I feel like I'm really looking for some kind of daemon (i.e. start the media file with VLC and then have this "listener" perform an action when timestamp on vlc has been reached)

I'm not opposed to pulling down the code, modifying changes to add a hook, and re-compiling to get what I need. I just need some pointers on where in the codebase this would all live.

What direction may I be pointed to to achieve this? Thanks!


Solution

  • You want the EventManager class, the documentation for which can be found here. Here's an example of how to use it to notify you when a video stops playing.

    import vlc
    
    def callback(event, player):
        print('FPS:', player.get_fps())
        print('Time(ms):', player.get_time())
        print('Frame:', .001 * player.get_time() * player.get_fps())
    
    # Create new media
    inst = vlc.Instance()
    media = inst.media_new('path/to/video')
    
    # Create new instance of vlc player
    player = inst.media_player_new()
    
    # Add a callback
    em = player.event_manager()
    em.event_attach(vlc.EventType.MediaPlayerStopped, \
                    callback, player)
    
    # Load video into vlc player instance
    player.set_media(media)
    
    # Play media
    player.play()
    

    Edit: Could you try this code?

    import vlc
    
    def callback(player):
        print('FPS:', player.get_fps())
        print('Time(ms):', player.get_time())
        print('Frame:', .001 * player.get_time() * player.get_fps())
        
        media_player.stop()
    
    # creating vlc media player object
    media_player = vlc.MediaPlayer()
    
    # media object
    media = vlc.Media("src/backtalk/default/oscar.mp4")
    
    # setting media to the media player
    media_player.set_media(media)
    
    # start playing video
    media_player.play()
    
    while True:
        if media_player.get_state() == vlc.State.Ended:
            callback(media_player)