Page 1 of 1

Get media_player to actually read media without playing it

Posted: 19 Jan 2015 13:22
by harrisclinton
Hi,

I've been writing an application in Gtk+3/Gstreamer/Python to manage a collection of videos and was unable to figure out Gstreamer's pipeline definitions to the extent that I could consistently get every video to play irrespective of format/codec etc. Therefore I am migrating to use the vlc Python bindings instead, and so far it works great with almost 100% recognition of hundreds files of varying container/codec etc.

One thing I have been unable to do, though, is get the media_player object to recognize the video assigned to it with set_media() (i.e. be able to get_length() etc.) unless I actually run play() on it. In other words, setting the media file name just seems to tell the player to open that file the next time the play() method is executed.

What I would like to do is cue up the file in the player and show the first frame without having to get too fancy with events.

At the moment, all that seems to work is watching for the MediaPlayerPlaying event and immediately running pause() when the event fires. Then of course I have to set some flags so that this behavior only occurs when a media file is first loaded. This seems highly inelegant.

Is there a better way?

Code: Select all

#This is an excerpt just to highlight the relevant bits #As an excerpt, there may be typos and no guarantee it runs from gi.repository import Gtk import vlc #From the sample application class VLCWidget(Gtk.DrawingArea): def __init__(self, *p): Gtk.DrawingArea.__init__(self) self.player = instance.media_player_new() def handle_embed(*args): thewindow = self.get_window() if sys.platform == 'win32': self.player.set_hwnd(self.window.handle) else: #This line changed for Gtk+3 compatibility self.player.set_xwindow(thewindow.get_xid()) return True self.connect("map", handle_embed) self.set_size_request(400, 250) class videoDatabase(): def __init__(self): #Gtk+3 stuff self.gladefile = "SampleLayout.glade" self.builder = Gtk.Builder() self.builder.add_from_file(self.gladefile) self.builder.connect_signals(self) self.window = self.builder.get_object("window1") #VLC stuff self.__vlc = VLCWidget() self.window.add(self.__vlc) self.player = self.__vlc.player #Connect to the events self.event_manager = self.player.event_manager() self.event_manager.event_attach(vlc.EventType.MediaPlayerPlaying, self.player_starting, self.player) #Flag to control player behavior when media actually starts playing self.playermode = 0 self.window.show() self.player.set_media(instance.media_new("file://somevideo.mpg") self.player.play() def player_starting(self, thing1, thing2): #Still haven't bothered to check what the other two parameters being passed are exactly .... if self.playermode == 0: #So if the mode is "0" (initializing mode) then self.player.pause() #The player will be paused as soon as it starts playing self.playermode == 1 #And the mode is changed so this won't happen again until the media is changed or similar reason elif self.playermode == 1: #Mode 1 is post initialization and means person clicked a button to play video #maybe do some other stuff here pass else: pass #Other modes etc. if __name__ == "__main__": main = videoDatabase() Gtk.main()

Re: Get media_player to actually read media without playing

Posted: 20 Jan 2015 03:36
by Jean-Baptiste Kempf
Did you try to force parsing? libvlc_media_parse or libvlc_media_parse_async

Re: Get media_player to actually read media without playing

Posted: 20 Jan 2015 12:33
by harrisclinton
Did you try to force parsing? libvlc_media_parse or libvlc_media_parse_async
Yes, this worked perfectly, thanks!

I grabbed the media object from the player and was able to do a synchronous parse on it to get the length in milliseconds and other metadata.

This lets me set up a progress bar for instance (GtkScale).

I still have to do the play/pause dance to actually cue the video up in the player on the first frame. Do you know of any other way to do that?

Code: Select all

#This is an excerpt just to highlight the relevant bits #As an excerpt, there may be typos and no guarantee it runs from gi.repository import Gtk import vlc #From the sample application class VLCWidget(Gtk.DrawingArea): def __init__(self, *p): Gtk.DrawingArea.__init__(self) self.player = instance.media_player_new() def handle_embed(*args): thewindow = self.get_window() if sys.platform == 'win32': self.player.set_hwnd(self.window.handle) else: #This line changed for Gtk+3 compatibility self.player.set_xwindow(thewindow.get_xid()) return True self.connect("map", handle_embed) self.set_size_request(400, 250) class videoDatabase(): def __init__(self): #Gtk+3 stuff self.gladefile = "SampleLayout.glade" self.builder = Gtk.Builder() self.builder.add_from_file(self.gladefile) self.builder.connect_signals(self) self.window = self.builder.get_object("window1") #VLC stuff self.__vlc = VLCWidget() self.window.add(self.__vlc) self.player = self.__vlc.player #Connect to the events self.event_manager = self.player.event_manager() self.event_manager.event_attach(vlc.EventType.MediaPlayerPlaying, self.player_starting, self.player) #Flag to control player behavior when media actually starts playing self.playermode = 0 self.window.show() self.player.set_media(instance.media_new("file://somevideo.mpg") self.media = self.player.get_media() self.media.parse() video_duration = self.media.get_duration() self.player.play() def player_starting(self, thing1, thing2): #Still haven't bothered to check what the other two parameters being passed are exactly .... if self.playermode == 0: #So if the mode is "0" (initializing mode) then self.player.pause() #The player will be paused as soon as it starts playing self.playermode == 1 #And the mode is changed so this won't happen again until the media is changed or similar reason elif self.playermode == 1: #Mode 1 is post initialization and means person clicked a button to play video #maybe do some other stuff here pass else: pass #Other modes etc. if __name__ == "__main__": main = videoDatabase() Gtk.main()

Re: Get media_player to actually read media without playing

Posted: 20 Jan 2015 19:08
by Jean-Baptiste Kempf
No other way.