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)
        else:
            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)
        self.mediaplayer.set_media(m)

    #  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')
                self.mediaplayer.set_rate(0.4)
            else:
                logger.debug(' Set the playback speed to  1')
                self.mediaplayer.set_rate(1)
            self.set_uri(path)
            return self.mediaplayer.play()
        else:
            return self.mediaplayer.play()

    #  Pause 
    def pause(self):
        self.mediaplayer.pause()

    #  recovery 
    def resume(self):
        self.mediaplayer.set_pause(0)

    #  stop it 
    def stop(self):
        self.mediaplayer.stop()

    #  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
        else:
            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 
        self.mediaplayer.video_set_aspect_ratio(ratio)

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

    #  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.
https://en.pythonmana.com/2022/175/202206240520433267.html

Random recommended