jaseg / python-mpv

Python interface to the awesome mpv media player
https://git.jaseg.de/python-mpv.git
Other
532 stars 67 forks source link

create playlist from all files in folder #211

Closed arctix-tk closed 2 years ago

arctix-tk commented 2 years ago

Hi folks,

using the mpv command line you can create a playlist from all the files in a folder and loop over them using: "mpv --loop-playlist=inf path/to/directory" The advantage is, that when dynamically changing the content of the folder the playlist changes as well.

How would I implement something similar using python-mpv? Loop over all the files in a directory -> put them in a playlist -> after the playlist ended delete playlist -> repeat Is there a more elegant way to do this?

Thanks in advance.

musicfunfan commented 2 years ago

Hello i would do it like that

import os 
import mpv 

#Take the directory files on a list 
dirFilesList = []
dirFilesListPath = []
path="../media"
dirFilesList = os.listdir(path)
fileNumber=len(dirFilesList)
colum="/"
status = "yes"
#Add the file paths in the list
for x in dirFilesList:
    print(path+colum+x)
    dirFilesListPath.append(path+colum+x)

#Create a mpv object
player = mpv.MPV(ytdl=False, input_default_bindings=True, input_vo_keyboard=True)

#Add all the files in the playlist 
for x in range (fileNumber):    
    print(dirFilesListPath[x])
    player.playlist_append(dirFilesListPath[x])

#Create the loop for playing until program kill
while status == "yes":
    #Selectet the position for the player to start playing the list 
    player.playlist_pos = 0

    #Wait for the playlist to end in order to close the mpv player 
    while True:
        #print(player.playlist)
        player.wait_for_playback()

I am not a pro python dev i just know some basic stuff. This is just an idea. I know that this is not the most elegant way of doing it. :smile:

jaseg commented 2 years ago

You could do this, but you don't have to go through the trouble to manage that playlist yourself. Instead, you can 1-to-1 translate the mpv command line to python code, since the python and command line interfaces have the same features:

import mpv
player = mpv.MPV(loop_playlist='inf')
player.play('/path/to/directory')
player.wait_for_playback() # will wait forever, or until another thread stops mpv

I haven't tried if this auto-updates as well when the folder's content changes, but I would be surprised if it didn't since mpv's CLI and API both work with the same backend.

musicfunfan commented 2 years ago

Nice i did not think of that. I am noob btw. :smile: