2

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:

  • calling Popen again (opens a new process)
  • calling p.communicate (I thought this is how to enter stdin commands)

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

2 Comments

thank you. 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".
Thanks! All this time I was avoiding starting new processes because I somehow assumed the command line would operate on the same one. Anyways, based on your comment, I dug around in the VLC preferences and found that everything works as expected now that I have enabled a couple of options: Allow only one instance and Enqueue items into playlist in one instance mode. Still not sure why this queueing behaviour worked natively from the command line, but not from Python subprocess - I thought they do the same thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.