API Reference¶
Example player¶
-
class
play.
VideoPlayer
(dimensions, fullscreen=False, soundrenderer='pyaudio', loop=False)[source]¶ This is an example videoplayer that uses pygame+pyopengl to render a video. It uses the Decoder object to decode the video- and audiostream frame by frame. It shows each videoframe in a window and places the audioframes in a buffer (or queue) from which they are fetched by the soundrenderer object.
Methods
calc_scaled_res
(screen_res, image_res)Calculate appropriate texture size. load_media
(vidSource)Loads a video. pause
()Pauses playback. play
()Starts playback. stop
()Stops playback. -
__init__
(dimensions, fullscreen=False, soundrenderer='pyaudio', loop=False)[source]¶ Constructor.
Parameters: dimensions : tuple (width, height)
The dimension of the window in which the video should be shown. Aspect ratio is maintained.
fullscreen : bool, optional
Indicates whether the video should be displayed in fullscreen.
soundrenderer : {‘pyaudio’,’pygame’}
Designates which sound backend should render the sound.
-
calc_scaled_res
(screen_res, image_res)[source]¶ Calculate appropriate texture size.
Calculate size or required texture so that it will fill the window, but retains the movies original aspect ratio.
Parameters: screen_res : tuple
Display window size/Resolution
image_res : tuple
Image width and height
Returns: tuple
width and height of image scaled to window/screen
-
Main classes¶
Decoder¶
This is the main class that retrieves video and audio frames from MoviePy/ffmpeg. You can supply Decoder with callback functions that handle stream data once it is available.
-
class
mediadecoder.decoder.
Decoder
(mediafile=None, videorenderfunc=None, play_audio=True)[source]¶ This class loads a video file that can be played. It can be passed a callback function to which decoded video frames should be passed.
Attributes
current_frame_no
Current frame_no of video. current_playtime
Clocks current runtime in seconds. current_videoframe
Representation of current video frame as a numpy array. frame_interval
Duration in seconds of a single frame. loop
Indicates whether the playback should loop. Methods
load_media
(mediafile[, play_audio])Loads a media file to decode. pause
()Pauses or resumes the video and/or audio stream. play
()Start the playback of the video. reset
()Resets the player and discards loaded data. rewind
()Rewinds the video to the beginning. seek
(value)Seek to the specified time. set_audiorenderer
(renderer)Sets the SoundRenderer object. set_videoframerender_callback
(func)Sets the function to call when a new frame is available. stop
()Stops the video stream and resets the clock. -
__init__
(mediafile=None, videorenderfunc=None, play_audio=True)[source]¶ Constructor.
Parameters: mediafile : str, optional
The path to the mediafile to be loaded (default: None)
videorenderfunc : callable (default: None)
Callback function that takes care of the actual Rendering of the videoframe. The specified renderfunc should be able to accept the following arguments:
- frame (numpy.ndarray): the videoframe to be rendered
play_audio : bool, optional
Whether audio of the clip should be played.
-
current_frame_no
¶ Current frame_no of video.
-
current_playtime
¶ Clocks current runtime in seconds.
-
current_videoframe
¶ Representation of current video frame as a numpy array.
-
frame_interval
¶ Duration in seconds of a single frame.
-
load_media
(mediafile, play_audio=True)[source]¶ Loads a media file to decode.
If an audiostream is detected, its parameters will be stored in a dictionary in the variable audioformat. This contains the fields
Nbytes: the number of bytes in the stream (2 is 16-bit sound). Nchannels: the channels (2 for stereo, 1 for mono) Fps: the frames per sec/sampling rate of the sound (e.g. 44100 KhZ). Buffersize: the audioframes per buffer. If play_audio was set to False, or the video does not have an audiotrack, audioformat will be None.
Parameters: mediafile : str
The path to the media file to load.
play_audio : bool, optional
Indicates whether the audio of a movie should be played.
Raises: IOError
When the file could not be found or loaded.
-
loop
¶ Indicates whether the playback should loop.
-
play
()[source]¶ Start the playback of the video. The playback loop is run in a separate thread, so this function returns immediately. This allows one to implement things such as event handling loops (e.g. check for key presses) elsewhere.
-
seek
(value)[source]¶ Seek to the specified time.
Parameters: value : str or int
The time to seek to. Can be any of the following formats:
>>> 15.4 -> 15.4 # seconds >>> (1,21.5) -> 81.5 # (min,sec) >>> (1,1,2) -> 3662 # (hr, min, sec) >>> '01:01:33.5' -> 3693.5 #(hr,min,sec) >>> '01:01:33.045' -> 3693.045 >>> '01:01:33,5' #comma works too
-
set_audiorenderer
(renderer)[source]¶ Sets the SoundRenderer object. This should take care of processing the audioframes set in audioqueue.
Parameters: renderer : soundrenderers.SoundRenderer
A subclass of soundrenderers.SoundRenderer that takes care of the audio rendering.
Raises: RuntimeError
If no information about the audiostream is available. This could be because no video has been loaded yet, or because no embedded audiostream could be detected in the video, or play_sound was set to False.
-
Timer¶
-
class
mediadecoder.timer.
Timer
(fps=None, max_duration=None)[source]¶ Timer serves as a video clock that is used to determine which frame needs to be displayed at a specified time. the clock runs in its own separate thread. Say you have an instance of Timer called
clock
. The time can be polled by checking>> clock.time
and the current frame can be determined by checking
>> clock.current_frame.
Attributes
current_frame
The current frame number that should be displayed. fps
Returns the frames per second indication that is currently set. frame_interval
The duration of a single frame in seconds. max_duration
Returns the max duration the clock should run for. time
The current time of the clock. Methods
pause
()Pauses the clock to continue running later. reset
()Reset the clock to 0. start
()Starts the clock from 0. stop
()Stops the clock and resets the internal timers. -
__init__
(fps=None, max_duration=None)[source]¶ Constructor.
Parameters: fps : float, optional
The frames per second of the video for which this timer is created.
max_duration : float, optional
The maximum time in seconds the timer should run for.
-
current_frame
¶ The current frame number that should be displayed.
-
fps
¶ Returns the frames per second indication that is currently set.
-
frame_interval
¶ The duration of a single frame in seconds.
-
max_duration
¶ Returns the max duration the clock should run for. (Usually the duration of the videoclip)
-
pause
()[source]¶ Pauses the clock to continue running later. Saves the duration of the current interval in the previous_intervals list.
-
start
()[source]¶ Starts the clock from 0. Uses a separate thread to handle the timing functionalities.
-
time
¶ The current time of the clock.
-
Sound renderers¶
This module contains objects that handle the audio frames supplied by Decoder. At the moment, the only ones that are stable are the PyAudioSoundRenderer and SounddeviceSoundrenderer (which both are bindings to PortAudio.
Pygame¶
-
class
mediadecoder.soundrenderers.pygamerenderer.
SoundrendererPygame
(audioformat, queue=None)[source]¶ Uses pygame.mixer to play sound
Methods
close_stream
()Cleanup (done by pygame.quit() in main loop) run
()Main thread function. -
__init__
(audioformat, queue=None)[source]¶ Constructor. Creates a pygame sound renderer using pygame.mixer.
Parameters: audioformat : dict
A dictionary containing the properties of the audiostream
queue : Queue.queue
A queue object which serves as a buffer on which the individual audio frames are placed by the decoder.
-
Pyaudio¶
-
class
mediadecoder.soundrenderers.pyaudiorenderer.
SoundrendererPyAudio
(audioformat, queue=None)[source]¶ Uses pyaudio to play sound
Methods
close_stream
()Closes the stream. get_frame
(in_data, frame_count, time_info, ...)Callback function for the pyaudio stream. start
()Initializes the stream. -
__init__
(audioformat, queue=None)[source]¶ Constructor. Creates a pyaudio sound renderer.
Parameters: audioformat : dict
A dictionary containing the properties of the audiostream
queue : Queue.queue
A queue object which serves as a buffer on which the individual audio frames are placed by the decoder.
-
Sounddevice¶
-
class
mediadecoder.soundrenderers.sounddevicerenderer.
SoundrendererSounddevice
(audioformat, queue=None)[source]¶ Uses python-sounddevice to play sound
Methods
close_stream
()Closes the stream. get_frame
(outdata, frames, timedata, status)Callback function for the audio stream. start
()Initializes the stream. -
__init__
(audioformat, queue=None)[source]¶ Constructor. Creates a pyaudio sound renderer.
Parameters: audioformat : dict
A dictionary containing the properties of the audiostream
queue : Queue.queue
A queue object which serves as a buffer on which the individual audio frames are placed by the decoder.
-