如何在已经打开的情况下使用Python控制VLC

How to control VLC with Python when already open(如何在已经打开的情况下使用Python控制VLC)

本文介绍了如何在已经打开的情况下使用Python控制VLC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,可以在一行中成功打开VLC、全屏显示、添加媒体等--如下所示(摘自较长的代码):

p = subprocess.Popen(["C:/Program Files (x86)/VideoLAN/VLC/vlc.exe","--one-instance","--fullscreen","--playlist-enqueue",clips[selection],speed[speed_selection]])

该特定行位于循环内。我想在循环之外打开VLC,然后添加媒体并在循环内选择速度,如下所示(摘录):

p = subprocess.Popen(["C:/Program Files (x86)/VideoLAN/VLC/vlc.exe","--fullscreen"])

timer = 0
while timer <= 19:
        selection = randint(1, 11)
        speed_selection = randint(1, 4)
        p = subprocess.Popen(#???? is this even the right command?)([#Something here to refer to vlc??,"--playlist-enqueue",clips[selection],speed[speed_selection]])
        timer += 1

我似乎找不到或找不出与已经打开的VLC播放器通信的语法。每次我尝试任何操作时,都会收到错误消息‘FileNotFoundError:[WinError 2]The System Cannot For the Files to be the Speciated’,这是有道理的,因为我没有正确地告诉POpen在什么地方打开这些文件!

可能是p=subcess.Popen()语法不正确,或者可能是在括号内,我需要以某种方式引用VLC而不打开另一个实例?

如有任何建议,欢迎光临。

完整代码:

clips = {1: "file:///C:/Users/Dan/Pictures/Bouchra UNI/Studio Practice 3/Moving drawing mp4s/for PYTHON short clips as mp4/clip 1.mp4", 2: "file:///C:/Users/Dan/Pictures/Bouchra UNI/Studio Practice 3/Moving drawing mp4s/for PYTHON short clips as mp4/clip 2.mp4", 3: "file:///C:/Users/Dan/Pictures/Bouchra UNI/Studio Practice 3/Moving drawing mp4s/for PYTHON short clips as mp4/clip 3.mp4", 4: "file:///C:/Users/Dan/Pictures/Bouchra UNI/Studio Practice 3/Moving drawing mp4s/for PYTHON short clips as mp4/clip 4.mp4", 5: "file:///C:/Users/Dan/Pictures/Bouchra UNI/Studio Practice 3/Moving drawing mp4s/for PYTHON short clips as mp4/clip 5.mp4", 6: "file:///C:/Users/Dan/Pictures/Bouchra UNI/Studio Practice 3/Moving drawing mp4s/for PYTHON short clips as mp4/clip 6.mp4", 7: "file:///C:/Users/Dan/Pictures/Bouchra UNI/Studio Practice 3/Moving drawing mp4s/for PYTHON short clips as mp4/clip 7.mp4", 8: "file:///C:/Users/Dan/Pictures/Bouchra UNI/Studio Practice 3/Moving drawing mp4s/for PYTHON short clips as mp4/clip 8.mp4", 9: "file:///C:/Users/Dan/Pictures/Bouchra UNI/Studio Practice 3/Moving drawing mp4s/for PYTHON short clips as mp4/clip 9.mp4", 10: "file:///C:/Users/Dan/Pictures/Bouchra UNI/Studio Practice 3/Moving drawing mp4s/for PYTHON short clips as mp4/clip 10.mp4", 11: "file:///C:/Users/Dan/Pictures/Bouchra UNI/Studio Practice 3/Moving drawing mp4s/for PYTHON short clips as mp4/clip 11.mp4"}

speed = {1: "--rate=1.0", 2: "--rate=1.5", 3: "--rate=2.0", 4: "--rate=0.5"}

import subprocess
from random import randint
p = subprocess.Popen(["C:/Program Files (x86)/VideoLAN/VLC/vlc.exe","--fullscreen"])

timer = 0
while timer <= 19:
        selection = randint(1, 11)
        speed_selection = randint(1, 4)
        p = subprocess.Popen(["--playlist-enqueue",clips[selection],speed[speed_selection]])
        timer += 1

推荐答案

我做了一个python模块来解决这个问题;televlc

    ~$ pip install televlc

创建VLC实例和接口,然后连接

    import televlc

    # Initialize the vlc object
    vlc = televlc.VLC()

    # Open a VLC instance and create the telnet interface
    vlc.start_telnet_interface()

    # Connect to the telnet interface
    vlc.connect_to_telnet_interface()

    # Run a command (volume up)
    vlc.do(["volup", "50"])

    # Disconnect from telnet interfac
    vlc.disconnect_from_telnet_interface()
    # or
    vlc.do(["exit"])
    # or end VLC instance and telnet interface
    vlc.do(["shutdown"])

连接到现有VLC实例和接口

这是在脚本外部创建现有实例和接口的命令

    ~$ vlc --extraintf --telnet --telnet-password myPassword --telnet-host myHost --telnet-port myPort
    import televlc

    # Set the interface data
    PASSWORD = myPassword
    HOST = myHost
    PORT = myPort

    # Initialize the vlc object
    vlc = televlc.VLC(PASSWORD, HOST, PORT)

    # Run a command
    command = ["volup", "50"]
    vlc.do(command)

    # Quit
    vlc.disconnect_from_telnet_interface()

这篇关于如何在已经打开的情况下使用Python控制VLC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在已经打开的情况下使用Python控制VLC

基础教程推荐