Skip to content

Instantly share code, notes, and snippets.

@PtaxLaine
Last active March 30, 2017 05:07
Show Gist options
  • Save PtaxLaine/291062f811ddda8c5064ad46e4b4701d to your computer and use it in GitHub Desktop.
Save PtaxLaine/291062f811ddda8c5064ad46e4b4701d to your computer and use it in GitHub Desktop.
VLC NowPlaying Script
Version: 1.6.0
Author: Tipher88
Contributors: AbyssHunted, Etuldan
Date: 20161711
================================================================================
This is a script that allows you to create a NowPlaying.txt file to display song
information from VLC.
While developing this script I was using VLC v2.2.4 and Python v2.7.10/v3.4.3.
The directions stated in this file assume default VLC settings for the Web
Interface before starting.
Updates:
Version 1.6.0
- Fix UnicodeEncodeError in console output
Version 1.5.0
- Unicode characters should now be supported in the meta-data
Version 1.4.0
- The script should now handle escaped characters (such as &)
Version 1.3.0
- The script now generates another file: NowPlaying_History.txt
Version 1.2.0
- Added support for the now_playing metadata tag
Version 1.1.0
- Tweaked script in order to support Python v3.x
================================================================================
Usage:
python NowPlaying.py
================================================================================
Dependencies:
Python (https://www.python.org/)
requests (http://www.python-requests.org/en/latest/)
================================================================================
In order to be able to use this script you need to do the following:
Extract the contents of the .zip to a location of your choosing
VLC:
1. Open VLC
2. Go to Tools -> Preferences
3. On the bottom left select the All radio button in the Show Settings
group
4. Click Main Interfaces item in the list on the left
5. Check the Web check-box
6. Click the drop-down arrow next to the Main Interfaces item to expand
the selection
7. Click the Lua sub-item
8. Set a password in the Lua HTTP group
9. Make sure the Source Directory in the same group is set to something
similar to: C:\Program Files (x86)\VideoLAN\VLC\lua\http
10. Click the Save button
11. Close and re-open VLC
Check to make sure Web Interface is active:
1. Open an internet browser and go to:
http://localhost:8080/requests/status.xml
2. Leave the username blank and type in the password you set in VLC
step 8
3. If the resulting page is in the form of xml (a bunch of tags with
names) then the Web Interface is working
Modify the script:
1. Search the script for the term "CUSTOM" to look at the items you can
change, they are currently as follows:
a. The separator string
b. The password to the Web Interface (needs to match VLC step 8)
c. The name of the generated file (default is NowPlaying.txt)
d. The amount of time to wait between checks
Run the script:
1. Open up your preferred console/terminal used to run python scripts
2. Change the directory to the location you extracted the .zip to
initially
3. Run the command: python NowPlaying.py
4. Start playing songs in VLC media player
5. You should see the song info show up in the console
6. You should also see a NowPlaying.txt file appear with text that
matches what is displayed in the console
7. Keep the script running while you stream
8. Enter ctrl + c to exit the script once you are done streaming
OBS:
1. Add a text source to OBS with the "Use Text From File" pointing to
the file generated from the script
#!/usr/bin/env python
#===============================================================================
# title :NowPlaying.py
# description :This script will create a NowPlaying.txt file that contains
# the info for the song that is currently being played via VLC
# author :Tipher88
# contributors :AbyssHunted, Etuldan
# date :20161711
# version :1.6.0
# usage :python NowPlaying.py
# notes :For this script to work you need to follow the instructions
# in the included README.txt file
# python_version :2.7.10 & 3.4.3
#===============================================================================
import os, sys, time, datetime, requests, codecs
import xml.etree.ElementTree as ET
try:
# Python 2.6-2.7
from HTMLParser import HTMLParser
except ImportError:
# Python 3
from html.parser import HTMLParser
# Global variable to keep track of song info being printed and check for changes
currentSongInfo = ''
def getInfo():
# CUSTOM: Separator can be changed to whatever you want
separator = ' | '
nowPlaying = 'UNKNOWN'
songTitle = 'UNKNOWN'
songArtist = 'UNKNOWN'
fileName = ''
s = requests.Session()
# CUSTOM: Username is blank, just provide the password
s.auth = ('', 'password')
# Attempt to retrieve song info from the web interface
try:
r = s.get('http://localhost:8080/requests/status.xml', verify=False)
if('401 Client error' in r.text):
print('Web Interface Error: Do the passwords match as described in the README.txt?')
return
except:
print('Web Interface Error: Is VLC running? Did you enable the Web Interface as described in the README.txt?')
return
# Okay, now we know we have a response with our xml data in it
# Save the response data
root = ET.fromstring(r.content)
# Loop through all info nodes to find relevant metadata
for info in root.iter('info'):
# Save the name attribute of the info node
name = info.get('name')
# See if the info node we are looking at is now_playing
if(name == 'now_playing'):
nowPlaying = info.text
else:
# See if the info node we are looking at is for the artist
if(name == 'artist'):
songArtist = info.text
# See if the info node we are looking at is for the title
if(name == 'title'):
songTitle = info.text
# See if the info node we are looking at is for the filename
if(name == 'filename'):
fileName = info.text
fileName = os.path.splitext(fileName)[0]
# END: for info in root.iter('info')
# If the now_playing node exists we should use that and ignore the rest
if(nowPlaying != 'UNKNOWN'):
writeSongInfoToFile(nowPlaying, separator)
else:
# Make sure a songTitle and songArtist was found in the metadata
if(songTitle != 'UNKNOWN' and
songArtist != 'UNKNOWN'):
# Both songTitle and song Artist have been set so use both
writeSongInfoToFile('%s - %s' % (songTitle, songArtist), separator)
elif( songTitle != 'UNKNOWN' ):
# Just use the songTitle
writeSongInfoToFile(songTitle, separator)
elif( fileName != '' ):
# Use the fileName as a last resort
writeSongInfoToFile(fileName, separator)
else:
# This should print 'UNKNOWN - UNKNOWN' because no relevant metadata was
# found
writeSongInfoToFile('%s - %s' % (songTitle, songArtist), separator)
# END: getInfo()
def writeSongInfoToFile( songInfo, separator ):
global currentSongInfo
htmlParser = HTMLParser()
if(currentSongInfo != songInfo):
currentSongInfo = songInfo
textToPrint = htmlParser.unescape(currentSongInfo)
textToPrint = textToPrint.encode(sys.stdout.encoding, errors='ignore')
print(textToPrint.decode(sys.stdout.encoding))
# CUSTOM: The output file name can be changed
textFile = codecs.open('NowPlaying.txt', 'w', encoding='utf-8', errors='ignore')
textFile.write(htmlParser.unescape(currentSongInfo + separator))
textFile.close()
timeStamp = '{:%H:%M:%S}:'.format(datetime.datetime.now())
# CUSTOM: The output file name can be changed
textFile = codecs.open('NowPlaying_History.txt', 'a', encoding='utf-8', errors='ignore')
textFile.write(htmlParser.unescape(('%s %s%s') % (timeStamp, currentSongInfo, os.linesep)))
textFile.close()
# END: writeSongInfoToFile( songInfo, separator )
if __name__ == '__main__':
while 1:
getInfo()
# CUSTOM: Sleep for a number of seconds before checking again
time.sleep(5)
# END: if __name__ == '__main__'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment