How to monitor VLC media player on Windows 7 using Python?
Asked Answered
E

1

5

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.

Emergent answered 12/6, 2014 at 7:41 Comment(0)
F
9

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. player1

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. player 2

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 player3

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
Faviolafavonian answered 12/6, 2014 at 10:41 Comment(9)
Hi, That's definitely the best detailed answer I have ever got. But, the link for status.xml isn't accessible from my browser. Any clue, why so?Emergent
You have to first activate the web browser interface as I mentioned in my answer, then only you can access it. Have you activated the web interface? Be sure to click on the save button after making any changes.Shingle
Yes, I followed every step you mentioned. Still not able to open the link.Emergent
Have you provided the password in lua option? What happens when you visit the link? Does the webpage keeps loading? What VLC version you are using ? and also try disabling the firewall. Do you have status.xml at C:\Program Files\VideoLAN\VLC\lua\http ?Shingle
Now the page loaded.(earlier it wasn't) I have provided a password, it asked for it and loaded. But still the error appeared: [string "C:\Program Files (x86)\VideoLAN\VLC\lua\htt..."]:121: attempt to call field 'info' (a nil value) VLC 2.1.2 I have a status.xml at address `C:\Program Files\VideoLAN\VLC\lua\http\requests`Emergent
From the error you provided I think you have installed a 32-bit vlc on a 64-bit machine. In the VLC you have to change the path that your provided earlier. In your case I guess it should be C:\Program Files (x86)\VideoLAN\VLC\lua\http instead of C:\Program Files\VideoLAN\VLC\lua\http also where do you see this error, is it displayed on the webpage?Shingle
Yes, error is displayed on the webpage. And, yeah, I understood that and already wrote Program Files (x86) instead of Program Files. Additionally, the webpage opens normally and give some details(state-stopped, etc) in form of XML when VLC is open but nothing is being played, but gives the same error when some track is playing.Emergent
Well if you see the xml it means that there is not problem with the configuration. I am not sure if the problem is with the file you are playing in the VLC. Try this: In Windows 7 goto C:\Users\Public\Music\Sample Music try playing sleepaway.mp3 file and let me know if the error persists.Shingle
What is the vlc version in your case? I used VLC v2.1.3. Try installing new version and use full install option during the installation!Shingle

© 2022 - 2024 — McMap. All rights reserved.