所以我有这样的想法,例如通过VLC等python脚本控制媒体播放器,但是由于我是Python的新手,所以我不知道该如何实现, 所以让我解释一下我在寻找什么 例如,我想从我的Python脚本获取并设置VLC的音量值? 我并不需要完整的代码或类似的代码,而是要遵循的一些提示,并在此先感谢
答案 0 :(得分:1)
安装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()
答案 1 :(得分:0)
您可以将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的更多文档。
答案 2 :(得分:0)
从tcp套接字连接控制vlc优于vlc-ctrl。我在树莓派中尝试了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具有更多的控制选项。