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

标签 python python-2.7 subprocess vlc

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

来自herehere ,我能够启动 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 中执行此操作的语法。我尝试了一些方法,但都无法工作:

  • 调用 Popen再次(打开一个新进程)
  • 调用 p.communicate (我以为这是输入标准输入命令的方法)

最佳答案

运行命令: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 配置。

关于python - 如何使用Python子进程添加到VLC播放列表队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29188833/

相关文章:

python - 如何获取没有目录名的文件名

python - 如何在theano中保存/序列化训练好的模型?

Python:如何在给定数字数组的情况下绘制 cdf 函数

python - 使用 BFS 的连接组件标记

python - 是否有等效的方法从 XML 加载 GMenu?

python-3.x - 如何将检查指定可执行文件是否正在运行的子进程函数更改为同时适用于 Python 2 和 3?

python - 具有多个参数的 Flask url_for()

python - 使用变量格式化对齐?

python - 返回代码与退出状态

python - Popen subprocess.PIPE 及其用途