在VLC中的gtk中实现搜寻器的最佳方法

时间:2018-07-17 06:51:50

标签: python gtk pygtk vlc libvlc

我正在使用vlc和gtk在python中制作视频流应用程序。我尝试使用gtk.scale来查找视频的位置,并相应地更新其位置,但是似乎该比例是为了设置值,而不是在平滑动画中显示时间。我觉得有更好的方法可以做到这一点,我只是不知道如何。

show_titles = ['']


base_url = 'https://www.watchcartoononline.com/anime/'


chrome_options = Options()  
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=chrome_options);

# uses random.choice to pick a random episode from a random title in show_titles[]
def get_src_of_rand_video():
    _url = base_url+random.choice(show_titles)
    uClient = uReq(_url) 
    page_soup = soup(uClient.read(), "html.parser")
    uClient.close()
    episodes = page_soup.select("div.cat-eps a.sonra")
    link = random.choice(episodes).get('href')
    #get video source
    driver.get(link)
    iframes = driver.find_elements_by_tag_name('iframe')
    driver.switch_to.frame(1)
    src = driver.find_element_by_id('video-js').find_element_by_tag_name('source').get_attribute('src')
    return src

class ApplicationWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Python-Vlc Media Player")
        self.player_paused=False
        self.is_player_active = False
        self.connect("destroy",Gtk.main_quit)
        self.isFullscreen = False

    def show(self):
        self.show_all()

    def position_changed(self, adj):
        self.player.set_position(0.01*self.positioner.get_value())

    def setup_objects_and_events(self):
        self.connect("key-press-event",self._key_press_event)
        self.playback_button = Gtk.Button()
        self.stop_button = Gtk.Button()
        self.update_button = Gtk.Button()
        #scale for seeking video position
        self.positioner = Gtk.Scale(orientation=Gtk.Orientation.HORIZONTAL,
         adjustment=Gtk.Adjustment(0, 0, 100, 1, 5, 0))
        self.positioner.set_digits(0)
        self.positioner.set_hexpand(True)
        self.positioner.set_valign(Gtk.Align.END)
        self.positioner.connect("value-changed", self.position_changed)

        self.play_image = Gtk.Image.new_from_icon_name(
                "gtk-media-play",
                Gtk.IconSize.MENU
            )
        self.pause_image = Gtk.Image.new_from_icon_name(
                "gtk-media-pause",
                Gtk.IconSize.MENU
            )
        self.stop_image = Gtk.Image.new_from_icon_name(
                "gtk-media-stop",
                Gtk.IconSize.MENU
            )
        self.update_image = Gtk.Image.new_from_icon_name(
                "gtk-media-update",
                Gtk.IconSize.MENU
            )
        self.playback_button.set_image(self.play_image)
        self.stop_button.set_image(self.stop_image)
        self.update_button.set_image(self.update_image)


        self.playback_button.connect("clicked", self.toggle_player_playback)
        self.stop_button.connect("clicked", self.stop_player)
        self.update_button.connect("clicked", self.update)

        self.draw_area = Gtk.DrawingArea()
        self.draw_area.set_size_request(300,300)

        self.draw_area.connect("realize",self._realized)

        self.hbox = Gtk.Box(spacing=6)
        self.hbox.pack_start(self.playback_button, True, True, 0)
        self.hbox.pack_start(self.stop_button, True, True, 0)
        self.hbox.pack_start(self.update_button, True, True, 0)
        self.vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        self.add(self.vbox)
        self.vbox.pack_start(self.draw_area, True, True, 0)
        self.vbox.pack_start(self.positioner,True,True,0)
        self.vbox.pack_start(self.hbox, False, False, 0)

    def toggle_player_playback(self, widget, data=None):

        """
        Handler for Player's Playback Button (Play/Pause).
        """

        if self.is_player_active == False and self.player_paused == False:
            self.player.play()
            self.playback_button.set_image(self.pause_image)
            self.is_player_active = True

        elif self.is_player_active == True and self.player_paused == True:
            self.player.play()
            self.playback_button.set_image(self.pause_image)
            self.player_paused = False

        elif self.is_player_active == True and self.player_paused == False:
            self.player.pause()
            self.playback_button.set_image(self.play_image)
            self.player_paused = True
        else:
            pass

    def _key_press_event(self,widget,event):
        keyval = event.keyval
        keyval_name = Gdk.keyval_name(keyval)
        state = event.state
        print(keyval_name)
        if  keyval_name == 'space':
            self.toggle_player_playback(self)
        elif keyval_name == 'Tab':
            self.update(self)
        elif keyval_name == "s":
            self.stop_player(self)
        elif keyval_name == "Escape":
            self.close_window()
        elif keyval_name == "f":
            self.toggle_fullscreen(self)
        elif keyval_name == "Up":
            if self.volume < 100:
                self.volume +=1
                print(self.volume)
                self.player.audio_set_volume(self.volume)
        elif keyval_name == "Down":
            if self.volume > 0:
                self.volume -=1
                print(self.volume)
                self.player.audio_set_volume(self.volume)
        else:
            return False
        return True

    def update(self,widget, data=None):
        self.stop_player(self)
        self.src = get_src_of_rand_video()
        self.set_my_media(self.player, self.vlcInstance, self.src)
        self.player.play()

    def toggle_fullscreen(self, widget, data=None):
        if self.isFullscreen == True:
            self.isFullscreen = False
            self.hbox.show()
            self.set_decorated(True)
            self.unmaximize()
            self.unfullscreen()

        elif self.isFullscreen == False: 
            self.isFullscreen = True
            self.set_decorated(False)
            self.hbox.hide()
            self.maximize()
            self.fullscreen()

    def stop_player(self, widget, data=None):
        self.player.stop()
        self.is_player_active = False
        self.playback_button.set_image(self.play_image)

    def set_my_media(self, _player, _instance, _source):
        _media = _instance.media_new(_source)
        _media.get_mrl
        _player.set_media(_media)
        return _player

    def _realized(self, widget, data=None):
        self.vlcInstance = vlc.Instance("--no-xlib")
        self.src = get_src_of_rand_video()

        self.player = self.vlcInstance.media_player_new()
        # uncomment to use vlc in window
        win_id = widget.get_window().get_xid()
        self.player.set_xwindow(win_id)
        self.player.set_fullscreen(True)
        self.player = self.set_my_media(self.player, self.vlcInstance, self.src)
        self.player.play()
        self.playback_button.set_image(self.pause_image)
        self.is_player_active = True

        #not sure if this is a good place for this loop
        self.running = True
        while self.running:
            self.positioner.set_value(self.player.get_position())
            time.sleep(1)

    def close_window():
        self.running = False
        self.destroy()

if __name__ == '__main__': 
        window = ApplicationWindow()
        window.setup_objects_and_events()
        window.show()
        Gtk.main()
        window.player.stop()
        window.vlcInstance.release()

1 个答案:

答案 0 :(得分:1)

  

这不是很好。

将来,请提供更多有关它为什么不起作用的详细信息,以便人们可以更好地帮助您。

  

原始的vlc播放器做到了

仅供参考,VLC使用Qt。

收听libvlc_MediaPlayerPositionChangedlibvlc_MediaPlayerTimeChanged,然后查询媒体播放器的值并将其设置在UI控件上(反之亦然,以供用户设置时间)。

这将是有趣的:https://code.videolan.org/videolan/npapi-vlc/blob/6eae0ffb9cbaf8f6e04423de2ff38daabdf7cae3/npapi/vlcplugin_gtk.cpp#L305