File: video_editor.py

package info (click to toggle)
python-traitsui 8.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,232 kB
  • sloc: python: 58,982; makefile: 113
file content (97 lines) | stat: -rw-r--r-- 3,446 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

"""Traits UI 'display only' video editor."""

from traits.api import Bool, Callable, Enum, Float, Instance, Property, Range

from traitsui.context_value import ContextValue, CVType
from traitsui.basic_editor_factory import BasicEditorFactory
from traitsui.toolkit import toolkit_object

AspectRatio = Enum('keep', 'ignore', 'expand')
PlayerState = Enum('stopped', 'playing', 'paused')
MediaStatus = Enum(
    'unknown',
    'no_media',
    'loading',
    'loaded',
    'stalled',
    'buffering',
    'buffered',
    'end',
    'invalid',
)


class VideoEditor(BasicEditorFactory):
    """Traits UI 'display only' video editor.

    This editor only displays the video stream, and does not attempt to
    provide UI elements for controlling playback.  It does provide a
    rich set of trait references that can be synchronised with the internal
    state of the video player.
    """

    #: The editor class to be created:
    klass = Property()

    #: The behaviour of the video display when the widget aspect ratio
    #: doesn't match the aspect ratio of the video stream.
    aspect_ratio = AspectRatio()

    #: True if the audio is muted, False otherwise
    muted = CVType(Bool, default_value=False, sync_value='from')

    #: Audio volume on a logarithmic scale
    volume = CVType(Range(0.0, 100.0), default_value=75.0, sync_value='from')

    #: The playback speed of the video. Negative values are allowed but may not
    #: be supported by the underlying implementation.
    playback_rate = CVType(Float, default_value=1.0, sync_value='from')

    #: The state (stopped, playing, paused) of the player
    state = CVType(PlayerState, default_value='stopped', sync_value='both')

    #: The current position, in seconds, in the video.
    position = CVType(Float, default_value=0.0, sync_value='both')

    #: Duration of the loaded video in seconds
    duration = Instance(
        ContextValue, args=('',), allow_none=False, sync_value='to'
    )

    #: The status of the loaded video (see ``MediaStatus``)
    media_status = Instance(
        ContextValue, args=('',), allow_none=False, sync_value='to'
    )

    #: An integer percentage representing how much of the player's buffer
    #: is filled.
    buffer = Instance(
        ContextValue, args=('',), allow_none=False, sync_value='to'
    )

    #: A string describing an error encountered by the player
    video_error = Instance(
        ContextValue, args=('',), allow_none=False, sync_value='to'
    )

    #: Callable to apply to video frames. Takes ref to new frame and a size
    #: tuple. Must return a QImage and a numpy array.
    image_func = CVType(Callable, sync_value='from')

    #: The name of a trait to synchronise with the player's notify interval.
    #: The referenced trait should be a Float representing time in seconds.
    notify_interval = CVType(Float, default_value=1.0, sync_value='from')

    def _get_klass(self):
        """Returns the editor class to be instantiated."""
        return toolkit_object('video_editor:VideoEditor')