Page 1 of 1

Stream ouput in python using vlc

Posted: 11 Apr 2009 21:29
by jimmykarily
I want to create a python script that takes a stream from a unique ip and streams it to multiple others. I've built the vlc.so file and succesfully imported it. I used the sample code from videolan site :

Code: Select all

#! /usr/bin/python """VLC Widget classes. This module provides two helper classes, to ease the embedding of a VLC component inside a pygtk application. VLCWidget is a simple VLC widget. DecoratedVLCWidget provides simple player controls. $Id$ """ import gtk import sys import vlc from gettext import gettext as _ class VLCWidget(gtk.DrawingArea): """Simple VLC widget. Its player can be controlled through the 'player' attribute, which is a MediaControl instance. """ def __init__(self, *p): gtk.DrawingArea.__init__(self) self.player=vlc.MediaControl(*p) def handle_embed(*p): if sys.platform == 'win32': xidattr='handle' else: xidattr='xid' self.player.set_visual(getattr(self.window, xidattr)) return True self.connect("map-event", handle_embed) self.set_size_request(320, 200) class DecoratedVLCWidget(gtk.VBox): """Decorated VLC widget. VLC widget decorated with a player control toolbar. Its player can be controlled through the 'player' attribute, which is a MediaControl instance. """ def __init__(self, *p): gtk.VBox.__init__(self) self._vlc_widget=VLCWidget(*p) self.player=self._vlc_widget.player self.pack_start(self._vlc_widget, expand=True) self._toolbar = self.get_player_control_toolbar() self.pack_start(self._toolbar, expand=False) def get_player_control_toolbar(self): """Return a player control toolbar#! /usr/bin/python """VLC Widget classes. This module provides two helper classes, to ease the embedding of a VLC component inside a pygtk application. VLCWidget is a simple VLC widget. DecoratedVLCWidget provides simple player controls. $Id$ """ import gtk import sys import vlc from gettext import gettext as _ class VLCWidget(gtk.DrawingArea): """Simple VLC widget. Its player can be controlled through the 'player' attribute, which is a MediaControl instance. """ def __init__(self, *p): gtk.DrawingArea.__init__(self) self.player=vlc.MediaControl(*p) def handle_embed(*p): if sys.platform == 'win32': xidattr='handle' else: xidattr='xid' self.player.set_visual(getattr(self.window, xidattr)) return True self.connect("map-event", handle_embed) self.set_size_request(320, 200) class DecoratedVLCWidget(gtk.VBox): """Decorated VLC widget. VLC widget decorated with a player control toolbar. Its player can be controlled through the 'player' attribute, which is a MediaControl instance. """ def __init__(self, *p): gtk.VBox.__init__(self) self._vlc_widget=VLCWidget(*p) self.player=self._vlc_widget.player self.pack_start(self._vlc_widget, expand=True) self._toolbar = self.get_player_control_toolbar() self.pack_start(self._toolbar, expand=False) def get_player_control_toolbar(self): """Return a player control toolbar """ tb=gtk.Toolbar() tb.set_style(gtk.TOOLBAR_ICONS) def on_play(b): self.player.start(0) return True def on_stop(b): self.player.stop(0) return True def on_pause(b): self.player.pause(0) return True tb_list = ( (_("Play"), _("Play"), gtk.STOCK_MEDIA_PLAY, on_play), (_("Pause"), _("Pause"), gtk.STOCK_MEDIA_PAUSE, on_pause), (_("Stop"), _("Stop"), gtk.STOCK_MEDIA_STOP, on_stop), ) for text, tooltip, stock, callback in tb_list: b=gtk.ToolButton(stock) b.connect("clicked", callback) tb.insert(b, -1) tb.show_all() return tb class VideoPlayer: """Example video player. """ def __init__(self): self.vlc = DecoratedVLCWidget() def main(self, fname): self.vlc.player.set_mrl(fname) self.popup() gtk.main() def popup(self): w=gtk.Window() w.add(self.vlc) w.show_all() w.connect("destroy", gtk.main_quit) return w if __name__ == '__main__': if not sys.argv[1:]: print "You must provide a movie filename" sys.exit(1) p=VideoPlayer() p.main(sys.argv[1]) """ tb=gtk.Toolbar() tb.set_style(gtk.TOOLBAR_ICONS) def on_play(b): self.player.start(0) return True def on_stop(b): self.player.stop(0) return True def on_pause(b): self.player.pause(0) return True tb_list = ( (_("Play"), _("Play"), gtk.STOCK_MEDIA_PLAY, on_play), (_("Pause"), _("Pause"), gtk.STOCK_MEDIA_PAUSE, on_pause), (_("Stop"), _("Stop"), gtk.STOCK_MEDIA_STOP, on_stop), ) for text, tooltip, stock, callback in tb_list: b=gtk.ToolButton(stock) b.connect("clicked", callback) tb.insert(b, -1) tb.show_all() return tb class VideoPlayer: """Example video player. """ def __init__(self): self.vlc = DecoratedVLCWidget() def main(self, fname): self.vlc.player.set_mrl(fname) self.popup() gtk.main() def popup(self): w=gtk.Window() w.add(self.vlc) w.show_all() w.connect("destroy", gtk.main_quit) return w if __name__ == '__main__': if not sys.argv[1:]: print "You must provide a movie filename" sys.exit(1) p=VideoPlayer() p.main(sys.argv[1])


The script runs with no errors and if I use a stream ip as a parameter it actually does half the job (it takes a stream as an input). The problem is that
I don't want a window at all (So gtk stuff is not needed) but I just want the script to create an other stream as ouput. The concept is this :
"original stream" ----> python script ------> "new stream (multiple addresses)". I know this is possible using the ui so I think there must be a way to do the same using the library. Anyone?

Re: Stream ouput in python using vlc

Posted: 11 Apr 2009 22:01
by RĂ©mi Denis-Courmont
The native library won't start the UI unless asked explicitly. The native library does not create a video window either, unless it is told to play a video file instead of streaming it.

Your problem: you're trying to use a GTK-centric Python binding. Of course, that's not going to work.

Re: Stream ouput in python using vlc

Posted: 11 Apr 2009 22:33
by jimmykarily
Thanks for the answer and for pointing out the problem but I would really appreciate a direction to a solution. I tried to read the documentation but most of it is outdated. To tell you the truth I didn't know there was an other binding for vlc (Is there?). Python bindings are very little documented and this example is all I found and made to work.

Re: Stream ouput in python using vlc

Posted: 16 Apr 2009 21:56
by wlourf
Thanks for the answer and for pointing out the problem but I would really appreciate a direction to a solution
Did you try the subprocess module in Python :

Code: Select all

p = subprocess.Popen(['vlc', yourstream, option1,option2]
If you don't want any window, use the option --intf=dummy