Media List in Python VLC
Question:
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.
- How to add a video to a playlist in Python’s binding for LibVLC?
- 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?
Answers:
Use while/for loop which will iterate through media list one by one. May be in this case pointer is pointing to only first video.
Edit 1:
[Use of For Loop] (Python VLC binding- playing a playlist) Refer answer section of this question. For loop is used to iterate through url’s (which is media list in this case).
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
I know this question is old but to whomever it may help in the future. I tried both ways, the accepted answer and a for loop. I found that using MediaList with MediaListPlayer was a lot more efficient. Almost twice as much!
For this problem, you could do something like (Python 3.4+):
import time
from pathlib import Path
import vlc
def create_media_list(vlc_instance, filenames):
media_list = vlc_instance.media_list_new()
for f in filenames:
media_list.add_media(f)
return media_list
filenames = list(Path("dir_of_music").iterdir())
instance = vlc.Instance()
player = instance.media_list_player_new()
media_list = create_media_list(instance, filenames)
player.set_media_list(media_list)
player.play()
while player.is_playing():
time.sleep(1)
I’m sure it could be made more efficient. But this would would great and be more memory efficient than the accepted answer. This will also play through all the media in the directory.
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.
- How to add a video to a playlist in Python’s binding for LibVLC?
- 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?
Use while/for loop which will iterate through media list one by one. May be in this case pointer is pointing to only first video.
Edit 1:
[Use of For Loop] (Python VLC binding- playing a playlist) Refer answer section of this question. For loop is used to iterate through url’s (which is media list in this case).
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
I know this question is old but to whomever it may help in the future. I tried both ways, the accepted answer and a for loop. I found that using MediaList with MediaListPlayer was a lot more efficient. Almost twice as much!
For this problem, you could do something like (Python 3.4+):
import time
from pathlib import Path
import vlc
def create_media_list(vlc_instance, filenames):
media_list = vlc_instance.media_list_new()
for f in filenames:
media_list.add_media(f)
return media_list
filenames = list(Path("dir_of_music").iterdir())
instance = vlc.Instance()
player = instance.media_list_player_new()
media_list = create_media_list(instance, filenames)
player.set_media_list(media_list)
player.play()
while player.is_playing():
time.sleep(1)
I’m sure it could be made more efficient. But this would would great and be more memory efficient than the accepted answer. This will also play through all the media in the directory.