Qt6 QML Multimedia: How to crop video played from MediaPlayer to VideoOutput
-
I am streaming to a Qt app from an RTSP server that sends horizontally tiled 1080p video (so the incoming video has a raw size of 3840x1080). I want to be able to crop to either the left or right half of the video from QML. Currently, I have this code - it works, but it feels like I am overcomplicating it:
MediaPlayer { id: mediaplayer // Sends 3840x1080 video which is a tiled 1080p stream source: "rtsp://xxxxx" videoOutput: videoOut } // Hide a 3840x1080 VideoOutput behind a Shader widget that crops it StackLayout { anchors.fill: parent currentIndex: 1 VideoOutput { id: videoOut } ShaderEffectSource { id: cropVideo property bool viewLeft: true sourceItem: videoOut hideSource: true live: true Layout.maximumHeight: 1080 Layout.maximumWidth: 1920 Layout.preferredHeight: 1080 Layout.preferredWidth: 1920 sourceRect: viewLeft ? Qt.rect(0, 0, 1920, 1080) : Qt.rect(1920, 0, 1920, 1080) } }
It feels like there should be a way to crop the video directly from either the MediaPlayer or VideoOutput widgets.
-
VideoOutput
has asourceRect
property. -
@GrecKo It does, but it seems to be a read-only reflection of the viewport set for the video frame objects that are coming through the
videoSink
. When I attempt to assign to it, I get the QML error:Invalid property assignment: "sourceRect" is a read-only property
. The docs do not appear to reflect the fact thatsourceRect
is read-only. I am using Qt 6.3.Looking at the source for
VideoOutput
(https://github.com/qt/qtmultimedia/blob/37c2d097eb5dd8671cc752dc920da11d66105905/src/multimediaquick/qquickvideooutput_p.h#L41), we can see that theQ_PROPERTY
forsourceRect
is read-only in both the6.3
anddev
branches.I have had the thought of attempting to manually set the
viewport
of theQVideoFrameFormat
of the frames coming through theVideoOutput
'svideoSink
, but I haven't created a working solution for that strategy yet.