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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
# (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!
"""
Demonstrates a 'display only' video editor for Qt5+.
Please refer to the `VideoEditor API docs`_ for further information.
.. _VideoEditor API docs: https://docs.enthought.com/traitsui/api/traitsui.editors.video_editor.html#traitsui.editors.video_editor.VideoEditor
"""
import numpy as np
from PIL import Image
from pyface.qt import is_qt5
from pyface.qt.QtGui import QImage
from traits.api import (
Bool,
Button,
Callable,
Float,
HasTraits,
Range,
Str,
observe,
)
from traitsui.api import ButtonEditor, ContextValue, HGroup, Item, UItem, View
from traitsui.editors.video_editor import MediaStatus, PlayerState, VideoEditor
def QImage_from_np(image):
assert np.max(image) <= 255
image8 = image.astype(np.uint8, order='C', casting='unsafe')
height, width, colors = image8.shape
bytesPerLine = 4 * width
image = QImage(
image8.data, width, height, bytesPerLine, QImage.Format.Format_RGB32
)
return image
def np_from_QImage(qimage):
# Creates a numpy array from a pyqt(5) QImage object
width, height = qimage.width(), qimage.height()
channels = qimage.pixelFormat().channelCount()
return (
np.array(qimage.bits().asarray(width * height * channels))
.reshape(height, width, channels)
.astype('u1')
)
def qimage_function(antialiasing=True):
def antialias_func(image_func):
"""
Turns an image function into a QImage function,
bound within the viewing frames box.
"""
def qimage_conv_func(image, box_dims):
_np_image = image_func(np_from_QImage(image))
pil_image = Image.fromarray(_np_image)
if antialiasing:
pil_image.thumbnail(box_dims, Image.ANTIALIAS)
else:
pil_image.thumbnail(box_dims)
_np_image = np.array(pil_image)
image = QImage_from_np(_np_image)
return image, _np_image
return qimage_conv_func
return antialias_func
@qimage_function(antialiasing=False)
def test_image_func(image):
return image.transpose(1, 0, 2)
class VideoEditorDemo(HasTraits):
"""Defines the main VideoEditor demo class."""
#: The URL that holds the video data.
video_url = Str()
#: A button that plays/pauses
play_pause_button = Button()
button_label = Str('Play')
state = PlayerState
duration = Float
position = Range(low=0.0, high='duration')
error = Str
status = MediaStatus
buffer = Range(0, 100)
muted = Bool(True)
volume = Range(0.0, 100.0, value=100.0)
playback_rate = Float(1.0)
image_func = Callable()
@observe('state')
def _state_update(self, event):
if event.new == 'stopped' or event.new == 'paused':
self.button_label = 'Play'
elif event.new == 'playing':
self.button_label = 'Pause'
@observe('play_pause_button')
def _play_pause_button_click(self, event):
if self.state == 'stopped' or self.state == 'paused':
self.state = 'playing'
else:
self.state = 'paused'
# Demo view
traits_view = View(
UItem('video_url'),
UItem(
'video_url',
editor=VideoEditor(
state=ContextValue('state'),
position=ContextValue('position'),
duration=ContextValue('duration'),
video_error=ContextValue('error'),
media_status=ContextValue('status'),
buffer=ContextValue('buffer'),
muted=ContextValue('muted'),
volume=ContextValue('volume'),
playback_rate=ContextValue('playback_rate'),
image_func=ContextValue('image_func'),
notify_interval=0.5,
),
),
HGroup(
UItem(
'play_pause_button',
editor=ButtonEditor(label_value='button_label'),
enabled_when='not bool(error)',
width=100,
),
UItem('position'),
),
HGroup(
Item('playback_rate'),
Item('muted', label='Mute'),
Item('volume'),
),
HGroup(
Item('state', style='readonly'),
Item('status', style='readonly'),
Item('buffer', style='readonly'),
Item('error', visible_when="bool(error)", style='readonly'),
),
title='VideoEditor',
buttons=['OK'],
width=800,
height=600,
resizable=True,
)
url = "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerEscapes.mp4" # noqa: E501
# Create the demo:
if is_qt5:
demo = VideoEditorDemo(image_func=test_image_func, video_url=url)
else:
# Qt 6 can't do on-the-fly image manipulation
demo = VideoEditorDemo(video_url=url)
# Run the demo (if invoked from the command line):
if __name__ == '__main__':
demo.configure_traits()
|