current position:Home>Python+vlc for video playback

Python+vlc for video playback

2022-06-24 08:26:32Qredsun

Before using openCV Realize the video playing , Unfortunately, the audio information is not processed , Only picture . This time try to use VLC Implement the player .

import vlc
import platform

class Player:
    ''' args: Set up  options '''

    def __init__(self, *args):
        if args:
            self.instance = vlc.Instance(*args)
            self.instance = vlc.Instance()
        self.mediaplayer = self.instance.media_player_new()
        self.uri = None

    #  Set the to be played url Address or local file path , Each call reloads the resource 
    def set_uri(self, uri):
        self.uri = uri
        # self.mediaplayer.set_mrl(self.uri) 
        #  Replace with  set_media  Method , To get progress 
        m = self.instance.media_new(uri)

    #  Play   Successfully returns 0, Failure to return -1
    def play(self, path=None):
        if path:
            if path.endswith('h264'):
                logger.debug(' take h264 Set the playback speed to  0.4')
                logger.debug(' Set the playback speed to  1')

    #  Pause 
    def pause(self):

    #  recovery 
    def resume(self):

    #  stop it 
    def stop(self):

    #  Release resources 
    def release(self):
        return self.mediaplayer.release()

    #  Is it playing 
    def is_playing(self):
        return self.mediaplayer.is_playing()

    #  Played for , Returns the value of milliseconds 
    def get_time(self):
        return self.mediaplayer.get_time()

    #  Drag the specified millisecond value to play . Successfully returns 0, Failure to return -1 ( We need to pay attention to , Only the current multimedia format or streaming media protocol support will take effect )
    def set_time(self, ms):
        return self.mediaplayer.get_time()

    #  Total length of audio and video , Returns the value of milliseconds 
    def get_length(self):
        return self.mediaplayer.get_length()

    #  Gets the current volume (0~100)
    def get_volume(self):
        return self.mediaplayer.audio_get_volume()

    #  set volume (0~100)
    def set_volume(self, volume):
        return self.mediaplayer.audio_set_volume(volume)

    #  Return to the current state : Playing ; Suspended ; other 
    def get_state(self):
        state = self.mediaplayer.get_state()
        if state == vlc.State.Playing:
            return 1
        elif state == vlc.State.Paused:
            return 0
            return -1

    #  Current playback progress . return 0.0~1.0 The floating point number between 
    def get_position(self):
        return self.mediaplayer.get_position()

    #  Drag the current progress , Pass in 0.0~1.0 The floating point number between ( We need to pay attention to , Only the current multimedia format or streaming media protocol support will take effect )
    def set_position(self, float_val):
        return self.mediaplayer.set_position(float_val)

    #  Get the current file playback rate 
    def get_rate(self):
        return self.mediaplayer.get_rate()

    #  Set the playback rate ( Such as :1.2, Express acceleration 1.2 Double play )
    def set_rate(self, rate):
        return self.mediaplayer.set_rate(rate)

    #  Set aspect ratio ( Such as "16:9","4:3")
    def set_ratio(self, ratio):
        self.mediaplayer.video_set_scale(0)  #  Must be set to 0, Otherwise, the screen width and height cannot be modified 

    #  Set window handle 
    def set_window(self, wm_id):
        if platform.system() == 'Windows':
        elif platform.system() == 'Linux':
        else:  # MacOS

    #  Register listener 
    def add_callback(self, event_type, callback):
        self.mediaplayer.event_manager().event_attach(event_type, callback)

    #  Remove the monitor 
    def remove_callback(self, event_type, callback):
        self.mediaplayer.event_manager().event_detach(event_type, callback)

copyright notice
author[Qredsun],Please bring the original link to reprint, thank you.

Random recommended