I want to know what's currently playing on VLC media player (Windows 7) using Python 2.7. Just knowing the details of the track/video is sufficient.
Research: I came to know that VLC media player has python-bindings. I also found out a way to play some track using a Python script. But I couldn't find a suitable way to check what's being played on VLC media player.
Clarification: I have the script running. I play a audio track on my VLC media player manually on my machine, and I want that script to detect and show the details of the track present for the file playing.
Install the Python VLC bindings with pip in a virtual environment. Create a Python file and import the VLC bindings. Instantiate a player object to play a file. Play that file.
VLC runs on all versions of Windows, from Windows XP SP3 to the last version of Windows 11.
You can also use CTRL+I hotkey to quickly open up the media information window. It basically displays general tags, statistics, codec information, and some extra metadata of the media file.
As per my understanding of your question:
I have the script running. I play a audio track on my VLC manually on my machine and I want that script to detect and show the details of the track present for the file playing.
There could be two possibilities to retrieve the information of the current item being played in the VLC player.
First method: Tested on Windows 7 OS and VLC v2.1.3
You have to first setup the web interface in the VLC player and then activate it. Then you can access all the information of currently playing track from a XML file by visiting this link http://localhost:8080/requests/status.xml
. You can create a simple script in python that will visit the link above and fetch the information.
Dummy code: This code will simply display the complete information of the file currently being played in the VLC player, you can try to extract what you need:
import requests
def getInfo():
s = requests.Session()
s.auth = ('', 'password')# Username is blank, just provide the password
r = s.get('http://localhost:8080/requests/status.xml', verify=False)
print r.text
getInfo()
You cab get Requests lib here.
Activating web interface: Open VLC goto Tools--->Preferences-----> Main Interface
as shown below tick mark the web
option.
Then click on Lua
option on the left pane. Enter the password in the password
field and enter C:\Program Files\VideoLAN\VLC\lua\http
in the source directory
field as shown below. Verify that you have status.xml file at the location that you provided in the source directory
.
Testing: Start the VLC player and play some file. Visit http://localhost:8080/requests/status.xml
and you shall see a login page, leave the username field blank and enter the password that you entered in the VLC. If you logged in successfully you shall see the XML file!
If you don't see any thing then do the following as shown in the image below: goto View--->Add Interface----->Select web
If everything works fine now run the script that I provided above. You shall see the information of the file in the console, now you can modify the script to get only the information that you desire. PS: The username field is blank, just enter your password in the script.
Second method: (I think this is not what you desired!) This case is when you'll play the file using python binding itself. Here you can find many different methods that can be used to get the information of the current playing item in the VLC.
For an instance: get_length(self) given length of the current item being played and get_title() to get the title number and get_state(self) to know if the player is playing something or is it paused/stopped. get_mrl()
gives the location of the track and also contains the track name at last, so you can figure out how to get track name from the location string using python.
Dummy code:
import vlc
def setup_player(filename):
vlc_instance = vlc.Instance()
media = vlc_instance.media_new(filename)
player = vlc_instance.media_player_new()
player.set_media(media)
print media.get_mrl()# File location to get title ;)
print player.get_length()#Time duration of file
print player.get_state()#Player's state
setup_player(filename)#Put file name/location here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With