current position:Home>Python+vlc for video playback
Python+vlc for video playback
2022-06-24 08:26:32【Qredsun】
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
The sidebar is recommended
- Writing sample code for functions in Python
- Summary of operation methods of Python set (about 20 operation methods), with sample code attached
- Python -- functions
- Anonymous and recursive functions in Python
- How to query the methods (member functions) of a class or an object in Python [using the function dir()]
- Summary of operation methods of Python Dictionary (dict) (about 18 operation methods), with sample code attached
- Collect hot search lists in Python at work, which can be called a fishing artifact
- Running Django and Vue on docker
- Data classification in pandas
- About Python: docxtpl is embedded by default when inserting pictures
guess what you like
How to solve the problem of CSV? (Language Python)
Installation and use of redis (Python)
Python implements sending mail (implements single / group mail verification code)
On the built-in object type of Python -- number (one of the differences between py2 and PY3)
Python God uses a problem to help you solve the problems of formal and actual parameters in Python functions
"Project Euler Python Mini tutorial" 001-010 solution introduction
Most beginners learn Python and web automation. In this way, they learn and give up
Python matrices and numpy arrays
Exciting challenge: Python crawler crawls the cover picture of station B
After installing python3, use the yum command to report an error?
Random recommended
- New features of python3.6, 3.7, 3.8 and 3.9
- Application of Python simplehttpserver
- Python sending mail (single / group) - yagmail module
- After learning these English words, mom doesn't have to worry that I can't learn Python any more
- 1-python+ selenium automated test (detailed tutorial) in the series of exercises of "teach you by hand"
- Cannot unmarshal array into go value of type main
- Analysis of the principle of Python import
- Python quickly saves pictures in wechat official account articles (multiple articles can be specified)
- Python error reporting series (14) -- selenium support for phantom JS has been deprecated
- Python variable data type
- Advanced Python Programming - functions and modules
- Python conditional judgment and loop statements
- Python dictionary nesting
- I want to use Python to write a census management software. I want to ask about the ideas and software involved
- I want to use Python to write a census management software. I want to consult the ideas and software involved.
- Python program has no idea
- How to set the initial position of the cursor in Python Tkinter
- The scrapy framework only gets a set of results. I don't know why (Language Python)
- Code problems in Python
- Python automation framework
- Vscode - offline extension installation tutorial (take Python plug-in installation as an example)
- _ What are the application scenarios in Python
- Python writing yaml file
- On the strange phenomenon of Python objects
- System learning Python -- unit test unittest: Test Report
- Learn Python in this way, and the boss licks back the resume in the trash can: 25K per month
- Guess the age of American mathematician Wiener
- Python machine learning day03
- Random seed()
- Programming science | you may be wrong about Python
- Is Python really worth learning
- What is the charm of python, which is warmly pursued by countless programmers?
- Python is popular for several reasons: These data tell you
- Picture to character drawing in Python, so easy!
- Data type conversion in pandas module
- Python Basics - (1) overview of Python
- Data Science Library Python -- learning of time series data
- Django project - error reporting
- [run the script framework in Django and store the data in the database]
- Complete Python exception handling in ten minutes