Search code examples
videoffmpeglibvlc

Setting up time triggers on video player


Background:

I have a some text that I want to display in a textbox whenever the video is playing in a certain interval, say (00:02:00-00:04:00 "Hello there").

These textbox should only be displayed whenever the video is playing in the particular interval. If I rewind, or skip the video to a different time, the textbox should be generated/destroyed depending whether the current time falls within the interval or not.

I have searched the documentation for ffplay and vlc for implementing this functionality.

What I have tried:

ffplay: my strategy would be to keep polling the current time of the video which ffplay dumps in STDERR and check if the current time falls within the interval.

LibVLC: LibVLC supports asynchronous events, but not the kind which would depend on the time at which video is playing.

My question:

1.Is it advisable to keep polling the current video time or setup callbacks/trigger functions?

2.Is it possible to get the current playing time of the video as a variable and create callback functions accordingly?

NOTE: I am aware that subtitles do the same thing, however I want the text to be in a different window. The textbox should be decoupled from the video player I use.


Solution

  • If all you are interested in is the current video time or position, polling is perfectly fine. LibVLC does implement callbacks for Media Player Position Changed and Media Player Time Changed but for something that is performed very regularly (like querying the time every second), there isn't a lot of value to handling it as an async event.

    Set up a timer to query every second (or half second depending on what kind of precision you want) and call:

    libvlc_time_t current = libvlc_media_player_get_time(my_player);
    

    Note, the result is in milliseconds. And you will obviously have to keep a reference to your my_player around but I assume you will have done that. Also note, in my experience, the time reported by libvlc_media_player_get_time might not always be very precise. But for playback it should be fine.