Commit 63f869c6 authored by Michael Mayr's avatar Michael Mayr

First version

parent be8f50fa
Copyright (c) 2012 Michael Mayr <michael@michfrm.net>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\ No newline at end of file
py-vlcclient
============
A VLC client written in python
\ No newline at end of file
py-vlcclient
============
This module allows to control a VLC instance using Python. This
module uses the telnet interface of VLC and has no external dependencies.
More information about the telnet interface:
http://wiki.videolan.org/Documentation:Streaming_HowTo/VLM
The clients supports some basic commands to modify playlists and control the playback.
How to Use
==========
First start VLC and enable the telnet interface. You can either enable
it when starting VLC::
$ vlc --intf telnet
or via the menus (depending on your platform, mostly View ->
Add Interface -> Telnet).
For example::
>>> from vlcclient import VLCClient
>>> vlc = VLCClient("::1")
>>> vlc.connect()
>>>
>>> r.add("/home/mitch/Music/a_song.ogg")
>>> r.volume(300)
>>> r.rewind()
>>> r.status()
'( new input: file:///.... )
( audio volume: 200 )
( state playing )'
Implemented Commands
====================
The following commands are currently implemented:
generic
-------
* help
* status
* info
playlists and controls
----------------------
* add
* enqueue
* seek
* play
* pause
* stop
* rewind
* next
* prev
* clear
volume
------
* volume (get/set)
* volup
* voldown
import os
from setuptools import setup, find_packages
setup(
name='vlcclient',
version='0.1.0',
description="Control VLC instances using python.",
long_description=open(os.path.join(os.path.dirname(__file__),
'README.rst')).read(),
license='BSD',
url="https://github.com/DerMitch/py-vlcclient",
author='Michael Mayr',
author_email='michael@michfrm.net',
py_modules=['vlcclient'],
classifiers=[
'Development Status :: 4 - Beta',
'License :: OSI Approved :: BSD License',
],
)
\ No newline at end of file
# coding: utf-8
"""
VLCClient
~~~~~~~~~
This module allows to control a VLC instance to be controlled
via telnet. You need to enable the telnet interface, e.g. start
VLC like this:
$ vlc --intf telnet
More information about the telnet interface:
http://wiki.videolan.org/Documentation:Streaming_HowTo/VLM
:author: Michael Mayr <michael@michfrm.net>
:licence: MIT License
:version: 0.1.0
"""
"""
ToDo:
- Connection error handling
"""
import sys
import telnetlib
class VLCClient(object):
"""Connection to a running VLC instance."""
def __init__(self, server, port=4212, password="admin"):
self.server = server
self.port = port
self.password = password
self.telnet = None
def connect(self):
"""Connect to VLC and login"""
assert self.telnet is None, "connect() called twice"
self.telnet = telnetlib.Telnet()
self.telnet.open(self.server, self.port)
# Login
self.telnet.read_until("Password: ")
self.telnet.write(self.password)
self.telnet.write("\n")
# Password correct?
result = self.telnet.expect([
"Password: ",
">"
])
if "Password" in result[2]:
raise WrongPasswordError()
def disconnect(self):
"""Disconnect and close connection"""
self.telnet.close()
self.telnet = None
def send_command(self, line):
"""Sends a command to VLC and returns the text reply.
This command may block."""
self.telnet.write(line + "\n")
return self.telnet.read_until(">")[1:-3]
#
# Commands
#
def help(self):
"""Returns the full command reference"""
return self.send_command("help")
def status(self):
"""current playlist status"""
return self.send_command("status")
def info(self):
"""information about the current stream"""
return self.send_command("info")
#
# Playlist
#
def add(self, filename):
"""Add a file to the playlist and play it.
This command always succeeds."""
return self.send_command("add {0}".format(filename))
def enqueue(self, filename):
"""Add a file to the playlist. This command always succeeds."""
return self.send_command("enqueue {0}".format(filename))
def seek(self, second):
"""Jump to a position at the current stream if supported."""
return self.send_command("seek {0}".format(second))
def play(self):
"""Start/Continue the current stream"""
return self.send_command("play")
def pause(self):
"""Pause playing"""
return self.send_command("pause")
def stop(self):
"""Stop stream"""
return self.send_command("stop")
def rewind(self):
"""Rewind stream"""
return self.send_command("rewind")
def next(self):
"""Play next item in playlist"""
return self.send_command("next")
def prev(self):
"""Play previous item in playlist"""
return self.send_command("prev")
def clear(self):
"""Clear all items in playlist"""
return self.send_command("clear")
#
# Volume
#
def volume(self, vol=None):
"""Get the current volume or set it"""
if vol:
return self.send_command("volume {0}".format(vol))
else:
return self.send_command("volume").strip()
def volup(self, steps=1):
"""Increase the volume"""
return self.send_command("volup {0}".format(steps))
def voldown(self, steps=1):
"""Decrease the volume"""
return self.send_command("voldown {0}".format(steps))
class WrongPasswordError(Exception):
pass
def main():
vlc = VLCClient("::1")
vlc.connect()
try:
command = getattr(vlc, sys.argv[1])
except IndexError:
print "usage: vlcclient.py command [argument]"
sys.exit(1)
attr = sys.argv[2] if len(sys.argv) > 2 else None
try:
print command(attr)
except TypeError:
print command()
if __name__ == '__main__':
main()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment