如何使用 Python 中的子进程 (VLC) 获取 运行 的热键 (ctrl + n) 3

How to get a hotkey (ctrl + n) to run with subprocess (VLC) in Python 3

我正在制作一个 python 应用程序来创建本地 DVR 的 rtsp link(基于通过输入提供的信息),它已完成 90%,只剩下一个步骤左边。 link生成后,会打开VLC media player(我已经做到了)。然后在VLC media player中,会自动按下CTRL+ n,这样就可以打开一个新的window,在其中将插入 RTSP link,以便 DVR 摄像机提要将自动在 VLC 媒体播放器中播放。 我卡在了热键部分...VLC 启动后会自动按下 CTRL+ n。如果有人知道,请帮助我。 我的代码:

#Import libaries
import os
import platform  #To identify the OS
import subprocess  #To start VLC
import keyboard  #keyboard module (not working)
from pynput import keyboard  #keyboard module (not working)
from subprocess import Popen, PIPE, STDOUT
from subprocess import DEVNULL    # To keep the script running after subprocess is called

#DVR inputs
ip_adr = input("What is your DVR's ip address?: ")
port = input("What is your DVR's RTSP port? (it should be 554 or else): ")
username = input("What is your DVR's username? (The default is admin): ")
password = input("What is your DVR's password?: ")
linkurl = "rtsp://" + username +":" + password + "@" + ip_adr + ":" + port +"/Streaming/channels/1/"

#Identify OS
nameos = platform.system()

if nameos == "Linux":  # For Linux
    cmd='vlc';
    print("Your DVR rtsp link is: " + linkurl)
    Popen([cmd], stderr=subprocess.DEVNULL, stdin=PIPE, shell=True)
else:   # For windows
    cmd='vlc';
    print("Your DVR rtsp link is: " + linkurl)
    Popen([cmd], stderr=subprocess.DEVNULL, stdin=PIPE, shell=True)

不要 运行 使用终端或命令提示符。 运行 它使用 VS Code 终端(我从这里推荐)或直接作为解释器并确保安装了 VLC。

好消息,VLC's command line args 可以通过提供 rtsp:// link:

直接启动网络流

To receive a RTSP stream, start VLC with:

% vlc rtsp://www.example.org/your_stream