(Python) how to start?
Posted: 26 Jun 2012 21:57
hi, im trying to do an aplication that requiere to use a video screen. I find the libvlc api for python and i think that its just what i need. I run a example that i found:
and it works fine, but does not render the video, only the audio. also, it gave me this error:

so, the question is: what else, besides the libvlc for python do i need to get it running? i suppose i need some libraries for the video rendering in different formats. Please, if anyone can point me to the rigth direction, it would be very appreciated.
sorry for my poor english.
Code: Select all
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
# WX example for VLC Python bindings
# Copyright (C) 2009-2010 the VideoLAN team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
#
"""
A simple example for VLC python bindings using wxPython.
Author: Michele OrrĂ¹
Date: 23-11-2010
"""
# import external libraries
import wx # 2.8
import vlc
# import standard libraries
import os
import user
class Player(wx.Frame):
"""The main window has to deal with events.
"""
def __init__(self, title):
wx.Frame.__init__(self, None, -1, title,
pos=wx.DefaultPosition, size=(550, 500))
# Menu Bar
# File Menu
self.frame_menubar = wx.MenuBar()
self.file_menu = wx.Menu()
self.file_menu.Append(1, "&Open", "Open from file..")
self.file_menu.AppendSeparator()
self.file_menu.Append(2, "&Close", "Quit")
self.Bind(wx.EVT_MENU, self.OnOpen, id=1)
self.Bind(wx.EVT_MENU, self.OnExit, id=2)
self.frame_menubar.Append(self.file_menu, "File")
self.SetMenuBar(self.frame_menubar)
# Panels
# The first panel holds the video and it's all black
self.videopanel = wx.Panel(self, -1)
self.videopanel.SetBackgroundColour(wx.BLACK)
# The second panel holds controls
ctrlpanel = wx.Panel(self, -1 )
self.timeslider = wx.Slider(ctrlpanel, -1, 0, 0, 1000)
self.timeslider.SetRange(0, 1000)
pause = wx.Button(ctrlpanel, label="Pause")
play = wx.Button(ctrlpanel, label="Play")
stop = wx.Button(ctrlpanel, label="Stop")
volume = wx.Button(ctrlpanel, label="Volume")
self.volslider = wx.Slider(ctrlpanel, -1, 0, 0, 100, size=(100, -1))
# Bind controls to events
self.Bind(wx.EVT_BUTTON, self.OnPlay, play)
self.Bind(wx.EVT_BUTTON, self.OnPause, pause)
self.Bind(wx.EVT_BUTTON, self.OnStop, stop)
self.Bind(wx.EVT_BUTTON, self.OnToggleVolume, volume)
self.Bind(wx.EVT_SLIDER, self.OnSetVolume, self.volslider)
# Give a pretty layout to the controls
ctrlbox = wx.BoxSizer(wx.VERTICAL)
box1 = wx.BoxSizer(wx.HORIZONTAL)
box2 = wx.BoxSizer(wx.HORIZONTAL)
# box1 contains the timeslider
box1.Add(self.timeslider, 1)
# box2 contains some buttons and the volume controls
box2.Add(play, flag=wx.RIGHT, border=5)
box2.Add(pause)
box2.Add(stop)
box2.Add((-1, -1), 1)
box2.Add(volume)
box2.Add(self.volslider, flag=wx.TOP | wx.LEFT, border=5)
# Merge box1 and box2 to the ctrlsizer
ctrlbox.Add(box1, flag=wx.EXPAND | wx.BOTTOM, border=10)
ctrlbox.Add(box2, 1, wx.EXPAND)
ctrlpanel.SetSizer(ctrlbox)
# Put everything togheter
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.videopanel, 1, flag=wx.EXPAND)
sizer.Add(ctrlpanel, flag=wx.EXPAND | wx.BOTTOM | wx.TOP, border=10)
self.SetSizer(sizer)
self.SetMinSize((350, 300))
# finally create the timer, which updates the timeslider
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
# VLC player controls
self.Instance = vlc.Instance()
self.player = self.Instance.media_player_new()
def OnExit(self, evt):
"""Closes the window.
"""
self.Close()
def OnOpen(self, evt):
"""Pop up a new dialow window to choose a file, then play the selected file.
"""
# if a file is already running, then stop it.
self.OnStop(None)
# Create a file dialog opened in the current home directory, where
# you can display all kind of files, having as title "Choose a file".
dlg = wx.FileDialog(self, "Choose a file", user.home, "",
"*.*", wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
dirname = dlg.GetDirectory()
filename = dlg.GetFilename()
# Creation
self.Media = self.Instance.media_new(unicode(os.path.join(dirname, filename)))
self.player.set_media(self.Media)
# Report the title of the file chosen
title = self.player.get_title()
# if an error was encountred while retriving the title, then use
# filename
if title == -1:
title = filename
self.SetTitle("%s - wxVLCplayer" % title)
# set the window id where to render VLC's video output
self.player.set_xwindow(self.videopanel.GetHandle())
# FIXME: this should be made cross-platform
self.OnPlay(None)
# set the volume slider to the current volume
self.volslider.SetValue(self.player.audio_get_volume() / 2)
# finally destroy the dialog
dlg.Destroy()
def OnPlay(self, evt):
"""Toggle the status to Play/Pause.
If no file is loaded, open the dialog window.
"""
# check if there is a file to play, otherwise open a
# wx.FileDialog to select a file
if not self.player.get_media():
self.OnOpen(None)
else:
# Try to launch the media, if this fails display an error message
if self.player.play() == -1:
self.errorDialog("Unable to play.")
else:
self.timer.Start()
def OnPause(self, evt):
"""Pause the player.
"""
self.player.pause()
def OnStop(self, evt):
"""Stop the player.
"""
self.player.stop()
# reset the time slider
self.timeslider.SetValue(0)
self.timer.Stop()
def OnTimer(self, evt):
"""Update the time slider according to the current movie time.
"""
# since the self.player.get_length can change while playing,
# re-set the timeslider to the correct range.
length = self.player.get_length()
self.timeslider.SetRange(-1, length)
# update the time on the slider
time = self.player.get_time()
self.timeslider.SetValue(time)
def OnToggleVolume(self, evt):
"""Mute/Unmute according to the audio button.
"""
is_mute = self.player.audio_get_mute()
self.player.audio_set_mute(not is_mute)
# update the volume slider;
# since vlc volume range is in [0, 200],
# and our volume slider has range [0, 100], just divide by 2.
self.volslider.SetValue(self.player.audio_get_volume() / 2)
def OnSetVolume(self, evt):
"""Set the volume according to the volume sider.
"""
volume = self.volslider.GetValue() * 2
# vlc.MediaPlayer.audio_set_volume returns 0 if success, -1 otherwise
if self.player.audio_set_volume(volume) == -1:
self.errorDialog("Failed to set volume")
def errorDialog(self, errormessage):
"""Display a simple error dialog.
"""
edialog = wx.MessageDialog(self, errormessage, 'Error', wx.OK|
wx.ICON_ERROR)
edialog.ShowModal()
if __name__ == "__main__":
# Create a wx.App(), which handles the windowing system event loop
app = wx.PySimpleApp()
# Create the window containing our small media player
player = Player("Simple PyVLC Player")
# show the player window centred and run the application
player.Centre()
player.Show()
app.MainLoop()

so, the question is: what else, besides the libvlc for python do i need to get it running? i suppose i need some libraries for the video rendering in different formats. Please, if anyone can point me to the rigth direction, it would be very appreciated.
sorry for my poor english.