Search code examples
pythonvlcpython-vlc

Start and end at specified timestamps when playing video using python-vlc


I have a list of youtube videos to be played in vlc using python.
I am using tafy and python-vlc libraries for the same.
I have managed to play the videos in order using the above libraries.
But now I want to play the videos between certain timestamps ( different for each video ).
So does an API exist in tafy or python-vlc which would enable me to play the given video from a specified start timestamp to an end timestamp?

Update: Demo code

import pafy
import vlc

url = "https://www.youtube.com/watch?v=bMt47wvK6u0"
video = pafy.new(url)
best = video.getbest()
playurl = best.url


Instance = vlc.Instance()
player = Instance.media_player_new()
Media = Instance.media_new(playurl)
Media.get_mrl()
player.set_media(Media)
player.play()
sleep(10)
while player.is_playing():
    sleep(1)

So now this plays a complete single video. I want to play it within certain ranges.


Solution

  • The Media object has both add_option and add_options functions.
    pafy falls over for me on Linux, so I can't test it but

    Media.add_option('start-time=120.0')
    Media.add_option('run-time=60.0')
    

    Should start at the 2 minute mark and run for 1 minute

    Media.add_option('start-time=120.0')
    Media.add_option('stop-time=180.0')
    

    Should achieve the same result.