Skip to content

collect_video_frame_data

CollectTraypublisherVideoFrameData

Bases: ContextPlugin, AYONPyblishPluginMixin

Collect video families.

Source code in client/ayon_traypublisher/plugins/publish/collect_video_frame_data.py
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
class CollectTraypublisherVideoFrameData(
    pyblish.api.ContextPlugin, AYONPyblishPluginMixin
):
    """Collect video families."""

    label = "Collect Video Families"
    order = pyblish.api.CollectorOrder - 0.25
    hosts = ["traypublisher"]
    optional = True

    @classmethod
    def get_attr_defs_for_instance(
        cls, create_context: "CreateContext", instance: "CreatedInstance"  # noqa: F821
    ):
        if not cls.instance_supported(create_context, instance):
            return []
        return [
            BoolDef(
                "collect_video_framerange",
                label="Collect Original Video Frame Data",
                default=True,
                visible=cls.optional,
            )
        ]

    @classmethod
    def instance_supported(
        cls, create_context: "CreateContext", instance: "CreatedInstance"  # noqa: F821
    ):
        # Show only for instances from settings based create plugins
        if instance.creator_identifier in {
            "io.ayon.creators.traypublisher.online",
            "render_movie_batch",
            "editorial_plate",
        }:
            return True

        if not instance.data.get("settings_creator"):
            return False

        # Get extensions from settings creator
        plugin = create_context.creators[instance.creator_identifier]
        extensions = {
            ext.lower().lstrip(".")
            for ext in plugin.extensions
        }
        return bool(extensions & _VIDEO_EXTENSIONS)

    def process(self, context):
        for instance in context:
            data = self.get_attr_values_from_data(instance.data)
            if data.get("collect_video_framerange"):
                # add the collector to collect video frame data
                instance.data["families"].append("collect.video.framerange")

CollectVideoData

Bases: InstancePlugin

Collect Original Video Frame Data

If the representation includes video files then set frameStart and frameEnd for the instance to the start and end frame respectively from the video's timecode.

Source code in client/ayon_traypublisher/plugins/publish/collect_video_frame_data.py
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
class CollectVideoData(pyblish.api.InstancePlugin):
    """Collect Original Video Frame Data

    If the representation includes video files then set `frameStart` and
    `frameEnd` for the instance to the start and end frame respectively from
    the video's timecode.
    """

    order = pyblish.api.CollectorOrder + 0.4905
    label = "Collect Original Video Frame Data"
    families = ["collect.video.framerange"]

    def process(self, instance):
        if all(
            key in instance.data 
            for key in ("frameStart", "frameEnd", "fps")
        ):
            self.log.debug(
                "Skipping video frame range collection because"
                " instance already has frame range data."
            )
            return

        frame_data = self.get_frame_data_from_representations(instance)
        if not frame_data:
            return

        for key, value in frame_data.items():
            if key not in instance.data:
                instance.data[key] = value
                self.log.debug(f"Collected video data '{key}': {value}")

    def get_frame_data_from_representations(self, instance: pyblish.api.Instance) -> dict:
        """Get frame data from a representation sequence.

        Args:
            instance (pyblish.api.Instance): The instance to extract frame data from.

        Returns:
            dict: A dictionary containing the frame data.
        """
        repres = instance.data.get("representations")
        if not repres:
            return {}

        # Iterate through all representations to find a valid video
        for repre in repres:
            extension: str = repre["ext"]
            if extension not in _VIDEO_EXTENSIONS:
                self.log.debug(
                    f"Representation extension '{extension}' is not a video"
                    " extension. Skipping this representation.")
                continue

            video_filename = repre["files"]
            if isinstance(video_filename, list):
                if len(video_filename) > 1:
                    self.log.debug(
                        "More than one video file found in representation."
                        " Skipping this representation."
                    )
                    continue
                video_filename: str = video_filename[0]
            video_filepath = os.path.join(repre["stagingDir"],
                                          video_filename)
            if not os.path.isfile(video_filepath):
                self.log.debug(
                    f"Video file '{video_filepath}' does not exist."
                    " Skipping this representation."
                )
                continue

            video_data = self.get_video_data(video_filepath)
            if video_data is None:
                continue

            return {
                "frameStart": video_data.frame_start,
                "frameEnd": video_data.frame_end,
                "handleStart": 0,
                "handleEnd": 0,
                "fps": video_data.fps
            }

        # No valid video representation found
        return {}

    def get_video_data(self, video_filepath: str) -> VideoData | None:
        """Get video data from a video file.

        Args:
            video_filepath (str): video filepath to extract data from

        Returns:
            VideoData | None: Video data extracted from the video file.
                If critical video data (fps) is not found, returns None.
        """
        info = get_video_info_metadata(video_filepath, self.log)
        num_frames: int = int(info.get("nb_frames", 0))
        fps: float = info.get("framerate")
        timecode: Union[str, None] = info.get("timecode")

        # Skip if fps is not available - it's essential for frame calculations
        if fps is None:
            self.log.warning(
                f"Could not extract framerate from '{video_filepath}'."
                " Skipping collecting of video data."
            )
            return None

        # TODO: Should this align with the folder/task entity frame start
        #  by default instead?
        start_frame: int = 0
        if timecode:
            # Parse timecode and use it as start frame
            start_frame = timecode_to_frame(timecode, fps)

        return VideoData(
            frame_start=start_frame,
            frame_end=start_frame+num_frames+1,
            fps=fps,
            # TODO: Also capture resolution?
            #width=info["width"],
            #height=info["height"],
        )

get_frame_data_from_representations(instance)

Get frame data from a representation sequence.

Parameters:

Name Type Description Default
instance Instance

The instance to extract frame data from.

required

Returns:

Name Type Description
dict dict

A dictionary containing the frame data.

Source code in client/ayon_traypublisher/plugins/publish/collect_video_frame_data.py
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
def get_frame_data_from_representations(self, instance: pyblish.api.Instance) -> dict:
    """Get frame data from a representation sequence.

    Args:
        instance (pyblish.api.Instance): The instance to extract frame data from.

    Returns:
        dict: A dictionary containing the frame data.
    """
    repres = instance.data.get("representations")
    if not repres:
        return {}

    # Iterate through all representations to find a valid video
    for repre in repres:
        extension: str = repre["ext"]
        if extension not in _VIDEO_EXTENSIONS:
            self.log.debug(
                f"Representation extension '{extension}' is not a video"
                " extension. Skipping this representation.")
            continue

        video_filename = repre["files"]
        if isinstance(video_filename, list):
            if len(video_filename) > 1:
                self.log.debug(
                    "More than one video file found in representation."
                    " Skipping this representation."
                )
                continue
            video_filename: str = video_filename[0]
        video_filepath = os.path.join(repre["stagingDir"],
                                      video_filename)
        if not os.path.isfile(video_filepath):
            self.log.debug(
                f"Video file '{video_filepath}' does not exist."
                " Skipping this representation."
            )
            continue

        video_data = self.get_video_data(video_filepath)
        if video_data is None:
            continue

        return {
            "frameStart": video_data.frame_start,
            "frameEnd": video_data.frame_end,
            "handleStart": 0,
            "handleEnd": 0,
            "fps": video_data.fps
        }

    # No valid video representation found
    return {}

get_video_data(video_filepath)

Get video data from a video file.

Parameters:

Name Type Description Default
video_filepath str

video filepath to extract data from

required

Returns:

Type Description
VideoData | None

VideoData | None: Video data extracted from the video file. If critical video data (fps) is not found, returns None.

Source code in client/ayon_traypublisher/plugins/publish/collect_video_frame_data.py
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
def get_video_data(self, video_filepath: str) -> VideoData | None:
    """Get video data from a video file.

    Args:
        video_filepath (str): video filepath to extract data from

    Returns:
        VideoData | None: Video data extracted from the video file.
            If critical video data (fps) is not found, returns None.
    """
    info = get_video_info_metadata(video_filepath, self.log)
    num_frames: int = int(info.get("nb_frames", 0))
    fps: float = info.get("framerate")
    timecode: Union[str, None] = info.get("timecode")

    # Skip if fps is not available - it's essential for frame calculations
    if fps is None:
        self.log.warning(
            f"Could not extract framerate from '{video_filepath}'."
            " Skipping collecting of video data."
        )
        return None

    # TODO: Should this align with the folder/task entity frame start
    #  by default instead?
    start_frame: int = 0
    if timecode:
        # Parse timecode and use it as start frame
        start_frame = timecode_to_frame(timecode, fps)

    return VideoData(
        frame_start=start_frame,
        frame_end=start_frame+num_frames+1,
        fps=fps,
        # TODO: Also capture resolution?
        #width=info["width"],
        #height=info["height"],
    )

get_video_info_metadata(path_to_file, logger)

Get flattened metadata from video file using ffprobe.

Parameters:

Name Type Description Default
path_to_file str

Path to image file.

required
logger Logger

Logger used for logging.

required
Source code in client/ayon_traypublisher/plugins/publish/collect_video_frame_data.py
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
def get_video_info_metadata(
    path_to_file,
    logger,
):
    """Get flattened metadata from video file using ffprobe.

    Args:
        path_to_file (str): Path to image file.
        logger (logging.Logger): Logger used for logging.
    """

    def _ffprobe_metadata_conversion(metadata):
        """Convert ffprobe metadata unified format."""
        output = {}
        for key, val in metadata.items():
            if key in ("tags", "disposition"):
                output.update(val)
            else:
                output[key] = val
        return output

    def _get_video_metadata_from_ffprobe(ffprobe_stream):
        """Extract video metadata from ffprobe stream.

        Args:
            ffprobe_stream (dict): Stream data obtained from ffprobe.

        Returns:
            dict: Video metadata extracted from the ffprobe stream.
        """
        video_stream = None
        for stream in ffprobe_stream["streams"]:
            if stream["codec_type"] == "video":
                video_stream = stream
                break
        return _ffprobe_metadata_conversion(video_stream)

    ffprobe_stream = get_ffprobe_data(path_to_file, logger)
    if not ffprobe_stream.get("streams"):
        logger.warning("Failed to get metadata from video file.")
        return {}

    metadata_stream = _get_video_metadata_from_ffprobe(ffprobe_stream)

    # create framerate key from available ffmpeg:r_frame_rate
    # evaluate its string expression value into float value
    if (
        "r_frame_rate" in metadata_stream
    ):
        rate_info = metadata_stream.get("r_frame_rate")
        # calculate framerate from string expression
        if "/" in str(rate_info):
            time, frame = str(rate_info).split("/")
            rate_info = float(time) / float(frame)

        try:
            metadata_stream["framerate"] = float(str(rate_info))
        except Exception as e:
            logger.warning(
                "Failed to evaluate '{}' value to framerate. Error: {}".format(
                    rate_info, e
                )
            )

    return metadata_stream

timecode_to_frame(timecode, fps)

Convert a timecode with fps to a frame number.

Parameters:

Name Type Description Default
timecode str

The timecode HH:MM:SS:FF format to be converted, like "00:01:00:00".

required
fps float

The frames per second to convert to frames with.

required

Returns:

Name Type Description
int int

The frame number.

Source code in client/ayon_traypublisher/plugins/publish/collect_video_frame_data.py
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
def timecode_to_frame(timecode: str, fps: float) -> int:
    """Convert a timecode with fps to a frame number.

    Args:
        timecode (str): The timecode HH:MM:SS:FF format to be converted,
            like "00:01:00:00".
        fps (float): The frames per second to convert to frames with.

    Returns:
         int: The frame number.
    """
    hours, minutes, seconds, frames = (int(t) for t in timecode.split(":"))
    frames += seconds * fps
    frames += minutes * 60 * fps
    frames += hours * 3600 * fps
    return int(frames)