Roberto
Roberto

Reputation: 2194

How to add to VLC playlist queue using Python subprocess

I am trying to use the Python 2.7 subprocess library to programmatically add songs to the VLC player queue.

From here and here, I am able to launch VLC Player and play a song (or queue songs from the outset);

from subprocess import Popen
vlcpath = r'C:\Program Files (x86)\VideoLAN\VLC\vlc.exe'
musicpath1 = r'path\to\song1.mp3'
musicpath2 = r'path\to\song2.mp3'
p = Popen([vlcpath,musicpath1]) # launch VLC and play song
p = Popen([vlcpath,musicpath1,musicpath2]) # launch VLC and play/queue songs

The problem is that I do not know the entire queue playlist at launch. I want to be able to add songs to the queue of the VLC process already running. How do I accomplish this, please?

From here, I think the appropriate command line entry is:

vlc.exe --started-from-file --playlist-enqueue "2.wmv"

But I do not know the syntax to execute this in subprocess. I tried a couple of things, but couldn't get either to work:

Upvotes: 2

Views: 3853

Answers (1)

jfs
jfs

Reputation: 414475

To run the command: vlc.exe --started-from-file --playlist-enqueue "2.wmv" using subprocess module on Windows:

from subprocess import Popen

cmd = 'vlc.exe --started-from-file --playlist-enqueue "2.wmv"'
p = Popen(cmd) # start and forget
assert not p.poll() # assert that it is started successfully

To wait for the command to finish:

from subprocess import check_call

check_call(cmd) # start, wait until it is done, raise on non-zero exit status

But how do I run that command a second time on the same p process? Your code starts a new instance of VLC, rather than running that on top of the p that was already open. I found that if I run the vlc.exe --started-from-file --playlist-enqueue "2.wmv" command multiple times manually (in a command prompt window), it correctly launches vlc (the first time) and then adds to queue (on subsequent calls). So I think I just need to be able to run the code you suggested multiple times "on top of itself"

Each Popen() starts a new process. Each time you run the command manually in the command-line it starts a new process. It might be upto the current vlc configuration on your system whether it keeps multiple vlc instances or you are running a different command (different command-line arguments).

Upvotes: 1

Related Questions