简体   繁体   中英

How to control VLC with Python when already open

I have a code that will successfully open VLC, fullscreen it, add media, etc, all in one line -like this (excerpt from longer code):

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

That particular line is inside a loop. I want to open VLC outside of the loop and then add media and select speed within the loop, like this (excerpt):

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

I can't seem to find or figure out the syntax for communicating to the already open VLC Player. I get the error 'FileNotFoundError: [WinError 2] The system cannot find the file specified' every time I try anything, which makes sense, because I'm not correctly telling Popen where to open these things within!

It is probably p = subprocess.Popen() that is the incorrect syntax, or maybe within the brackets I need to refer to vlc somehow without opening another instance?

Any suggestions appreciated.

Full code:

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

I did a python module to solve this; televlc

    ~$ pip install televlc

Creating a vlc instance and interface, then connecting

    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"])

Connecting to an existing vlc instance and interface

this is the command to create the existing instance and interface outside the script

    ~$ 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM