1

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!

user2402616
  • 1,434
  • 4
  • 22
  • 38

1 Answers1

1

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)
MillerTime
  • 317
  • 2
  • 11
  • The script just exits immediately, I don't see/hear any playback. Unlike when I instantiate the player using... `# # media object # media = vlc.Media("foo.mp4") # # setting media to the media player # media_player.set_media(media) # # start playing video # media_player.play()` – user2402616 Jul 18 '22 at 15:41
  • I also tried, `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()) # creating vlc media player object media_player = vlc.MediaPlayer() # media object media = vlc.Media("foo.mp4") # Add a callback em = media_player.event_manager() em.event_attach(vlc.EventType.MediaPlayerStopped, \ callback, media_player) # setting media to the media player media_player.set_media(media) # start playing video media_player.play() ` – user2402616 Jul 18 '22 at 15:55
  • 1
    The code works perfectly for me. Does the callback function print out for you? Even when I use a non-existent file path, it prints for me. – MillerTime Jul 24 '22 at 16:30
  • The cb does not print anything for me. The script just runs for a sec wo any output and then finishes – user2402616 Jul 25 '22 at 13:34
  • 1
    Which version of `python-vlc` are you using? I'm using `3.0.12118`. – MillerTime Jul 25 '22 at 14:30
  • `3.0.16120`.. I'm guessing it must be a platform issue. I can try on a different machine – user2402616 Jul 25 '22 at 16:36
  • 1
    That seems most likely. I'm on a Raspberry Pi 4B, do you have something similar? – MillerTime Jul 26 '22 at 14:04
  • I got a zero and a 2. I'll try on a separate windows machine first. Fresh install of python an whatnot – user2402616 Jul 26 '22 at 14:19
  • I have same exact behavior on a windows machine. New python, pip, and vlc installations too. The script just starts and exits with no output. I'm using the same exact thing you posted (except putting in an actual file) – user2402616 Jul 26 '22 at 15:34
  • 1
    I've edited my post. Could you try the second piece of code? It remove the usage of the `EventManager` class, which may be messing you up, and the `Instance` class. – MillerTime Jul 26 '22 at 16:01
  • Excellent! That did it. Any way to change the ` if media_player.get_state() == vlc.State.Ended:` line so that we can set to a specific timestamp vs a state ? – user2402616 Jul 26 '22 at 16:19
  • 1
    There's the `media_player.get_time()` command, which returns the current time in milliseconds. However, it updates too infrequently for your purposes. See https://stackoverflow.com/questions/58090143/python-vlc-get-position-polling-rate-work-arounds. – MillerTime Jul 26 '22 at 17:16
  • Thanks for link. Yeah I tried.. `if media_player.get_time() == 3000: print("we're at 3S ts")` But am not seeing my logging. Can you think of another way to achieve this? Doesn't need to be vlc related. Ty – user2402616 Jul 26 '22 at 19:18
  • 1
    Use the `time.time()` module. Set a variable to `time.time()` when you begin the video, then constantly check to see if that variable minus the current time is equal to your timestamp. Hope your project goes well! (: – MillerTime Jul 26 '22 at 19:28