我如何通过 Python 脚本控制 VLC 媒体播放器

How can i Control VLC media player through a Python scripte

所以我有了从 python 脚本(例如 VLC)控制媒体播放器的想法,但是由于我是 Python 的新手,所以我不知道如何实现它, 所以让我解释一下我要找的是 例如,我想从我的 Python 脚本中获取和设置 VLC 的音量值? 我不是要完整的代码或类似的东西,只是要遵循一些提示,在此先感谢

您可以使用 python 的模块 vlc-ctrl 实现此自动化。然后使用子进程模块模块执行其命令。

1) 通过 pip

安装 vlc-ctrl
pip install vlc-ctrl

test.py:(音量增大)

import subprocess
subprocess.Popen(['vlc-ctrl',  'volume',  '+10%'])

和 运行 代码:

python test.py

vlc-ctrl 模块 here 的更多文档。

从 tcp 套接字连接控制 vlc 比 vlc-ctrl 更好。我在 raspberry pi 中尝试了 vlc-ctrl,我无法达到我的期望。所以我决定通过套接字连接来控制vlc播放器。

步骤:-

1) 您需要从命令提示符或 shell 'vlc --intf rc --rc-host 127.0.0.1:44500'[启动 vlc 播放器] 运行 来启用 vlc 播放器以通过 tcp 连接控制它....你可以使用子进程从 python 像这样启动 vlc。

2) 从 python 控制这里是我的示例脚本:


class player():
    def __init__(self):
        self.is_initiated = False
        self.SEEK_TIME = 20
        self.MAX_VOL = 512
        self.MIN_VOL = 0
        self.DEFAULT_VOL = 256
        self.VOL_STEP = 13
        self.current_vol = self.DEFAULT_VOL

    def toggle_play(self):
        if not self.is_initiated:
            self.is_initiated = True
            self.thrededreq("loop on")
            self.thrededreq("random on")
            self.thrededreq("add /home/pi/Desktop/Music")#adding the music folder
            print("Init Playing")
            return
        self.thrededreq("pause")
        print("Toggle play")


    def next(self):
        if not self.is_initiated:
            self.toggle_play()
            return
        self.thrededreq("next")
        print("Next")
        pass

    def prev(self):
        if not self.is_initiated:
            self.toggle_play()
            return
        self.thrededreq("prev")
        print("Previous")
        pass

    def volup(self):
        self.current_vol = self.current_vol + self.VOL_STEP
        self.thrededreq("volume " + str(self.current_vol))
        print("Volume up")
        pass

    def voldown(self):
        self.current_vol = self.current_vol - self.VOL_STEP
        self.thrededreq("volume " + str(self.current_vol))
        print("Volume Down")
        pass

    def seek(self, forward: bool):
        length = self._timeinfo("get_length")
        print(length)
        cur = self._timeinfo("get_time")
        print(cur)
        if (forward):
            seekable = cur + self.SEEK_TIME
        else:
            seekable = cur - self.SEEK_TIME
        if seekable > length:
            seekable = length - 5
        if seekable < 0:
            seekable = 0
        self.thrededreq("seek " + str(seekable))
        print("Seek: ",seekable," Cur: ",cur,"Len: ",length)
        pass

    def _timeinfo(self, msg):
        length = self.req(msg, True).split("\r\n")
        if (len(length) < 2):
            return None
        length = length[1].split(" ")
        if (len(length) < 2):
            return None
        try:
            num = int(length[1])
            return num
        except:
            return None

    def req(self, msg: str, full=False):
        try:
            with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
                # Connect to server and send data
                sock.settimeout(0.7)
                sock.connect(('127.0.0.1', 44500))
                response = ""
                received = ""
                sock.sendall(bytes(msg + '\n', "utf-8"))
                # if True:
                try:
                    while (True):
                        received = (sock.recv(1024)).decode()
                        response = response + received
                        if full:
                            b = response.count("\r\n")
                            if response.count("\r\n") > 1:
                                sock.close()
                                break
                        else:
                            if response.count("\r\n") > 0:
                                sock.close()
                                break
                except:
                    response = response + received
                    pass
                sock.close()
                return response
        except:
            return None
            pass

    def thrededreq(self, msg):
        Thread(target=self.req, args=(msg,)).start()

#'vlc --intf rc --rc-host 127.0.0.1:44500' you need to run the vlc player from command line to allo controlling it via TCP
Player=player()
player.toggle_play()
#player.next()
#player.prev()

如果你想要更多的命令和控制,使用"SocketTest"并连接到vlc的端口并查看....

这个比 vlc-ctrl 有更多的控制选项。

安装python-vlc

pip install python-vlc

只需更改 路径,一切顺利..

from vlc import Instance
import time
import os

class VLC:
    def __init__(self):
        self.Player = Instance('--loop')

    def addPlaylist(self):
        self.mediaList = self.Player.media_list_new()
        path = r"C:\Users\dell5567\Desktop\engsong"
        songs = os.listdir(path)
        for s in songs:
            self.mediaList.add_media(self.Player.media_new(os.path.join(path,s)))
        self.listPlayer = self.Player.media_list_player_new()
        self.listPlayer.set_media_list(self.mediaList)
    def play(self):
        self.listPlayer.play()
    def next(self):
        self.listPlayer.next()
    def pause(self):
        self.listPlayer.pause()
    def previous(self):
        self.listPlayer.previous()
    def stop(self):
        self.listPlayer.stop()

创建对象

player = VLC()

添加播放列表

player.addPlaylist()

播放歌曲

player.play()
time.sleep(9)

播放下一首歌曲

player.next()
time.sleep(9)

暂停歌曲

player.pause()
time.sleep(9)

继续播放歌曲

player.play()
time.sleep(9)

上一首歌曲

player.previous()
time.sleep(9)

停止播放歌曲

player.stop()