Reputation: 55
so I had this idea of controlling a media player from a python script like VLC for example, but since I am new to Python I don't know how to achieve that, so let me explain what I am looking for is for example, I want to get and set a volume value of VLC from my Python script? I am not asking for a whole code or something like that just some tips to follow and thanks in advance
Upvotes: 4
Views: 22409
Reputation: 161
Install python-vlc
pip install python-vlc
Just change the path,your good to go..
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()
Create a object
player = VLC()
Add playlist
player.addPlaylist()
Play the song
player.play()
time.sleep(9)
Play the next song
player.next()
time.sleep(9)
Pause the song
player.pause()
time.sleep(9)
Resume the song
player.play()
time.sleep(9)
Previous song
player.previous()
time.sleep(9)
Stop the song
player.stop()
Upvotes: 7
Reputation: 31
Controlling vlc from tcp socket connection is better than vlc-ctrl. I tried vlc-ctrl in my raspberry pi,i cannot reach my expectation. So i decided to control the vlc player from socket connection.
Steps:-
1) you need to run from command prompt or from shell 'vlc --intf rc --rc-host 127.0.0.1:44500'
[starting vlc player] to enable vlc player for controlling it from tcp connection.... you can start vlc like this from python using subprocess.
2) for cntrolling from python here is my sample script:
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()
if you want more command and controlling,use"SocketTest" and connect to the port of vlc and check out....
this one has more control option than vlc-ctrl.
Upvotes: 3
Reputation: 356
You can use python's module vlc-ctrl for this automation. And then use subprocess module module to execute its commands.
1) Install vlc-ctrl through pip
pip install vlc-ctrl
test.py: (To volume up)
import subprocess
subprocess.Popen(['vlc-ctrl', 'volume', '+10%'])
And run code with:
python test.py
More documentation for vlc-ctrl module here.
Upvotes: 1