如何使用 Python 子进程添加到 VLC 播放列表队列

How to add to VLC playlist queue using Python subprocess

我正在尝试使用 Python 2.7 subprocess 库以编程方式将歌曲添加到 VLC 播放器队列。

here and here 开始,我可以启动 VLC 播放器并播放歌曲(或从一开始就将歌曲加入队列);

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

问题是我不知道启动时的整个队列播放列表。我希望能够将歌曲添加到 VLC 进程的队列中 运行。请问我该怎么做?

here开始,我认为合适的命令行输入是:

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

但我不知道在 subprocess 中执行此操作的语法。我尝试了几件事,但都无法正常工作:

到 运行 命令:vlc.exe --started-from-file --playlist-enqueue "2.wmv" 使用 Windows 上的 subprocess 模块:

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

等待命令完成:

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"

每个 Popen() 启动一个新进程。每次您在命令行中手动 运行 命令时,它 都会启动一个新进程 。这可能取决于您系统上当前的 vlc 配置,无论它是否保留多个 vlc 实例,或者您正在 运行 使用不同的命令(不同的命令行参数)。