| 1 |
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
|
| 11 |
""" |
| 12 |
Demonstrates a 'display only' video editor for Qt5+. |
| 13 |
|
| 14 |
Please refer to the `VideoEditor API docs`_ for further information. |
| 15 |
|
| 16 |
.. _VideoEditor API docs: https://docs.enthought.com/traitsui/api/traitsui.editors.video_editor.html#traitsui.editors.video_editor.VideoEditor |
| 17 |
""" |
| 18 |
import numpy as np |
| 19 |
from PIL import Image |
| 20 |
from pyface.qt import is_qt5 |
| 21 |
from pyface.qt.QtGui import QImage |
| 22 |
from traits.api import ( |
| 23 |
Bool, |
| 24 |
Button, |
| 25 |
Callable, |
| 26 |
Float, |
| 27 |
HasTraits, |
| 28 |
Range, |
| 29 |
Str, |
| 30 |
observe, |
| 31 |
) |
| 32 |
from traitsui.api import ButtonEditor, ContextValue, HGroup, Item, UItem, View |
| 33 |
from traitsui.editors.video_editor import MediaStatus, PlayerState, VideoEditor |
| 34 |
|
| 35 |
|
| 36 |
def QImage_from_np(image): |
| 37 |
assert np.max(image) <= 255 |
| 38 |
image8 = image.astype(np.uint8, order='C', casting='unsafe') |
| 39 |
height, width, colors = image8.shape |
| 40 |
bytesPerLine = 4 * width |
| 41 |
|
| 42 |
image = QImage( |
| 43 |
image8.data, width, height, bytesPerLine, QImage.Format.Format_RGB32 |
| 44 |
) |
| 45 |
return image |
| 46 |
|
| 47 |
|
| 48 |
def np_from_QImage(qimage): |
| 49 |
# Creates a numpy array from a pyqt(5) QImage object |
| 50 |
width, height = qimage.width(), qimage.height() |
| 51 |
channels = qimage.pixelFormat().channelCount() |
| 52 |
return ( |
| 53 |
np.array(qimage.bits().asarray(width * height * channels)) |
| 54 |
.reshape(height, width, channels) |
| 55 |
.astype('u1') |
| 56 |
) |
| 57 |
|
| 58 |
|
| 59 |
def qimage_function(antialiasing=True): |
| 60 |
def antialias_func(image_func): |
| 61 |
""" |
| 62 |
Turns an image function into a QImage function, |
| 63 |
bound within the viewing frames box. |
| 64 |
""" |
| 65 |
|
| 66 |
def qimage_conv_func(image, box_dims): |
| 67 |
_np_image = image_func(np_from_QImage(image)) |
| 68 |
pil_image = Image.fromarray(_np_image) |
| 69 |
if antialiasing: |
| 70 |
pil_image.thumbnail(box_dims, Image.ANTIALIAS) |
| 71 |
else: |
| 72 |
pil_image.thumbnail(box_dims) |
| 73 |
_np_image = np.array(pil_image) |
| 74 |
image = QImage_from_np(_np_image) |
| 75 |
return image, _np_image |
| 76 |
|
| 77 |
return qimage_conv_func |
| 78 |
|
| 79 |
return antialias_func |
| 80 |
|
| 81 |
|
| 82 |
@qimage_function(antialiasing=False) |
| 83 |
def test_image_func(image): |
| 84 |
return image.transpose(1, 0, 2) |
| 85 |
|
| 86 |
|
| 87 |
class VideoEditorDemo(HasTraits): |
| 88 |
"""Defines the main VideoEditor demo class.""" |
| 89 |
|
| 90 |
#: The URL that holds the video data. |
| 91 |
video_url = Str() |
| 92 |
|
| 93 |
#: A button that plays/pauses |
| 94 |
play_pause_button = Button() |
| 95 |
|
| 96 |
button_label = Str('Play') |
| 97 |
|
| 98 |
state = PlayerState |
| 99 |
|
| 100 |
duration = Float |
| 101 |
|
| 102 |
position = Range(low=0.0, high='duration') |
| 103 |
|
| 104 |
error = Str |
| 105 |
|
| 106 |
status = MediaStatus |
| 107 |
|
| 108 |
buffer = Range(0, 100) |
| 109 |
|
| 110 |
muted = Bool(True) |
| 111 |
|
| 112 |
volume = Range(0.0, 100.0, value=100.0) |
| 113 |
|
| 114 |
playback_rate = Float(1.0) |
| 115 |
|
| 116 |
image_func = Callable() |
| 117 |
|
| 118 |
@observe('state') |
| 119 |
def _state_update(self, event): |
| 120 |
if event.new == 'stopped' or event.new == 'paused': |
| 121 |
self.button_label = 'Play' |
| 122 |
elif event.new == 'playing': |
| 123 |
self.button_label = 'Pause' |
| 124 |
|
| 125 |
@observe('play_pause_button') |
| 126 |
def _play_pause_button_click(self, event): |
| 127 |
if self.state == 'stopped' or self.state == 'paused': |
| 128 |
self.state = 'playing' |
| 129 |
else: |
| 130 |
self.state = 'paused' |
| 131 |
|
| 132 |
# Demo view |
| 133 |
traits_view = View( |
| 134 |
UItem('video_url'), |
| 135 |
UItem( |
| 136 |
'video_url', |
| 137 |
editor=VideoEditor( |
| 138 |
state=ContextValue('state'), |
| 139 |
position=ContextValue('position'), |
| 140 |
duration=ContextValue('duration'), |
| 141 |
video_error=ContextValue('error'), |
| 142 |
media_status=ContextValue('status'), |
| 143 |
buffer=ContextValue('buffer'), |
| 144 |
muted=ContextValue('muted'), |
| 145 |
volume=ContextValue('volume'), |
| 146 |
playback_rate=ContextValue('playback_rate'), |
| 147 |
image_func=ContextValue('image_func'), |
| 148 |
notify_interval=0.5, |
| 149 |
), |
| 150 |
), |
| 151 |
HGroup( |
| 152 |
UItem( |
| 153 |
'play_pause_button', |
| 154 |
editor=ButtonEditor(label_value='button_label'), |
| 155 |
enabled_when='not bool(error)', |
| 156 |
width=100, |
| 157 |
), |
| 158 |
UItem('position'), |
| 159 |
), |
| 160 |
HGroup( |
| 161 |
Item('playback_rate'), |
| 162 |
Item('muted', label='Mute'), |
| 163 |
Item('volume'), |
| 164 |
), |
| 165 |
HGroup( |
| 166 |
Item('state', style='readonly'), |
| 167 |
Item('status', style='readonly'), |
| 168 |
Item('buffer', style='readonly'), |
| 169 |
Item('error', visible_when="bool(error)", style='readonly'), |
| 170 |
), |
| 171 |
title='VideoEditor', |
| 172 |
buttons=['OK'], |
| 173 |
width=800, |
| 174 |
height=600, |
| 175 |
resizable=True, |
| 176 |
) |
| 177 |
|
| 178 |
|
| 179 |
url = "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerEscapes.mp4" # noqa: E501 |
| 180 |
# Create the demo: |
| 181 |
if is_qt5: |
| 182 |
demo = VideoEditorDemo(image_func=test_image_func, video_url=url) |
| 183 |
else: |
| 184 |
# Qt 6 can't do on-the-fly image manipulation |
| 185 |
demo = VideoEditorDemo(video_url=url) |
| 186 |
|
| 187 |
# Run the demo (if invoked from the command line): |
| 188 |
if __name__ == '__main__': |
| 189 |
demo.configure_traits() |