pythonvlclibvlc

Media List in Python VLC


I need to write a program in Python that will play video files from a folder in the VLC player, using the Linux OS. These files must be in the playlist. Code:

import vlc

mrl1 = '....1.3gp'
mrl2 = '....2.3gp'

Instance = vlc.Instance('--input-repeat=-1', '--fullscreen', '--mouse-hide-timeout=0')

MediaList = Instance.media_list_new()
MediaList.add_media(Instance.media_new(mrl2))
MediaList.add_media(Instance.media_new(mrl1))

list_player = Instance.media_list_player_new()
list_player.set_media_list(MediaList)

list_player.next()

player.play()

The problem is that after running the first video, the player closes. I think that it does not add the second video to the list.

  1. How to add a video to a playlist in Python's binding for LibVLC?
  2. Is there a utility function that plays all the videos in a folder? UPD: I've created a playlist, and ran it to test in VLC player. Playing only the first video. After VLC is also closed. What is rhe problem?

Solution

  • You should to put it into a loop, which waits for each song to finish playing. For example, try this code

    import vlc
    import time
    
    mrl1 = '....1.3gp'
    mrl2 = '....2.3gp'
    
    song_list = [mrl1,mrl2]
    instance = vlc.Instance('--input-repeat=-1', '--fullscreen', '--mouse-hide-timeout=0')
    for song in song_list:
        player = instance.media_player_new()
        media = instance.media_new(song)
        
        media.get_mrl()
        player.set_media(media)
        player.play()
        playing = set([1,2,3,4])
        time.sleep(1)
        duration = player.get_length() / 1000
        mm, ss = divmod(duration, 60)
    
        while True:
            state = player.get_state()
            if state not in playing:
                break
            continue