Rucha Katakwar | 222c531 | 2022-03-21 14:17:01 -0700 | [diff] [blame] | 1 | # Copyright 2022 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | """Utility functions for processing video recordings. |
| 15 | """ |
| 16 | # Each item in this list corresponds to quality levels defined per |
| 17 | # CamcorderProfile. For Video ITS, we will currently test below qualities |
| 18 | # only if supported by the camera device. |
leslieshaw | 5b54ee5 | 2023-08-29 13:22:30 -0700 | [diff] [blame] | 19 | |
| 20 | |
Carlos Chan | fb27a5a | 2024-07-18 18:04:56 +0000 | [diff] [blame] | 21 | import dataclasses |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 22 | import logging |
Leslie Shaw | 5755b4d | 2024-08-30 16:21:01 -0700 | [diff] [blame] | 23 | import math |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 24 | import os.path |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 25 | import re |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 26 | import subprocess |
Rucha Katakwar | 31a5450 | 2022-08-10 16:32:30 -0700 | [diff] [blame] | 27 | import error_util |
leslieshaw | 21c49e51 | 2023-08-25 13:47:28 -0700 | [diff] [blame] | 28 | import image_processing_utils |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 29 | |
| 30 | |
Kailiang Chen | 6526b3a | 2024-01-25 11:28:58 -0800 | [diff] [blame] | 31 | COLORSPACE_HDR = 'bt2020' |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 32 | HR_TO_SEC = 3600 |
Kailiang Chen | 6526b3a | 2024-01-25 11:28:58 -0800 | [diff] [blame] | 33 | INDEX_FIRST_SUBGROUP = 1 |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 34 | MIN_TO_SEC = 60 |
| 35 | |
Rucha Katakwar | 089e609 | 2022-03-30 11:19:26 -0700 | [diff] [blame] | 36 | ITS_SUPPORTED_QUALITIES = ( |
Rucha Katakwar | 41133f9 | 2022-03-22 13:44:09 -0700 | [diff] [blame] | 37 | 'HIGH', |
| 38 | '2160P', |
| 39 | '1080P', |
| 40 | '720P', |
| 41 | '480P', |
| 42 | 'CIF', |
| 43 | 'QCIF', |
| 44 | 'QVGA', |
| 45 | 'LOW', |
| 46 | 'VGA' |
Rucha Katakwar | 222c531 | 2022-03-21 14:17:01 -0700 | [diff] [blame] | 47 | ) |
Rucha Katakwar | 0ced2c3 | 2022-08-01 14:54:42 -0700 | [diff] [blame] | 48 | |
leslieshaw | 6fb2c07 | 2023-04-06 16:38:54 -0700 | [diff] [blame] | 49 | LOW_RESOLUTION_SIZES = ( |
| 50 | '176x144', |
| 51 | '192x144', |
| 52 | '352x288', |
| 53 | '384x288', |
| 54 | '320x240', |
| 55 | ) |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 56 | |
leslieshaw | 6fb2c07 | 2023-04-06 16:38:54 -0700 | [diff] [blame] | 57 | LOWEST_RES_TESTED_AREA = 640*360 |
leslieshaw | 99fd75b | 2023-01-09 15:01:19 -0800 | [diff] [blame] | 58 | |
leslieshaw | 541e297 | 2023-02-28 11:06:07 -0800 | [diff] [blame] | 59 | VIDEO_QUALITY_SIZE = { |
Clemenz Portmann | 703010e | 2023-07-21 16:39:18 -0700 | [diff] [blame] | 60 | # '480P', '1080P', HIGH' & 'LOW' are not included as they are DUT-dependent |
leslieshaw | 541e297 | 2023-02-28 11:06:07 -0800 | [diff] [blame] | 61 | '2160P': '3840x2160', |
leslieshaw | 541e297 | 2023-02-28 11:06:07 -0800 | [diff] [blame] | 62 | '720P': '1280x720', |
leslieshaw | 541e297 | 2023-02-28 11:06:07 -0800 | [diff] [blame] | 63 | 'VGA': '640x480', |
| 64 | 'CIF': '352x288', |
| 65 | 'QVGA': '320x240', |
| 66 | 'QCIF': '176x144', |
| 67 | } |
| 68 | |
| 69 | |
Carlos Chan | fb27a5a | 2024-07-18 18:04:56 +0000 | [diff] [blame] | 70 | @dataclasses.dataclass |
| 71 | class CommonPreviewSizeData: |
| 72 | """Class to store smallest and largest common sizes of preview and video.""" |
| 73 | smallest_size: str |
| 74 | smallest_quality: str |
| 75 | largest_size: str |
| 76 | largest_quality: str |
| 77 | |
| 78 | |
| 79 | def get_preview_video_sizes_union(cam, camera_id, min_area=0): |
| 80 | """Returns largest and smallest common size and quality of preview and video. |
Clemenz Portmann | fb23635 | 2024-06-10 10:13:40 -0700 | [diff] [blame] | 81 | |
| 82 | Args: |
| 83 | cam: camera object. |
| 84 | camera_id: str; camera ID. |
Carlos Chan | fb27a5a | 2024-07-18 18:04:56 +0000 | [diff] [blame] | 85 | min_area: int; Optional filter to eliminate smaller sizes (ex. 640*480). |
Clemenz Portmann | fb23635 | 2024-06-10 10:13:40 -0700 | [diff] [blame] | 86 | |
| 87 | Returns: |
Carlos Chan | fb27a5a | 2024-07-18 18:04:56 +0000 | [diff] [blame] | 88 | common_size_quality, CommonPreviewSizeData class |
Clemenz Portmann | fb23635 | 2024-06-10 10:13:40 -0700 | [diff] [blame] | 89 | """ |
Carlos Chan | fb27a5a | 2024-07-18 18:04:56 +0000 | [diff] [blame] | 90 | supported_preview_sizes = set(cam.get_all_supported_preview_sizes(camera_id)) |
Clemenz Portmann | fb23635 | 2024-06-10 10:13:40 -0700 | [diff] [blame] | 91 | supported_video_qualities = cam.get_supported_video_qualities(camera_id) |
| 92 | logging.debug('Supported video profiles & IDs: %s', supported_video_qualities) |
| 93 | |
leslieshaw | 541e297 | 2023-02-28 11:06:07 -0800 | [diff] [blame] | 94 | # Make dictionary on video quality and size according to compatibility |
| 95 | supported_video_size_to_quality = {} |
| 96 | for quality in supported_video_qualities: |
| 97 | video_quality = quality.split(':')[0] |
| 98 | if video_quality in VIDEO_QUALITY_SIZE: |
| 99 | video_size = VIDEO_QUALITY_SIZE[video_quality] |
| 100 | supported_video_size_to_quality[video_size] = video_quality |
| 101 | logging.debug( |
Carlos Chan | fb27a5a | 2024-07-18 18:04:56 +0000 | [diff] [blame] | 102 | 'Supported video size to quality: %s', supported_video_size_to_quality |
| 103 | ) |
| 104 | # Find the intersection of supported preview sizes and video sizes |
| 105 | common_sizes = supported_preview_sizes.intersection( |
| 106 | supported_video_size_to_quality.keys() |
| 107 | ) |
| 108 | if not common_sizes: |
| 109 | raise AssertionError('No common size between Preview and Video!') |
| 110 | # Filter common sizes based on min_area |
leslieshaw | 541e297 | 2023-02-28 11:06:07 -0800 | [diff] [blame] | 111 | size_to_area = lambda s: int(s.split('x')[0])*int(s.split('x')[1]) |
Carlos Chan | fb27a5a | 2024-07-18 18:04:56 +0000 | [diff] [blame] | 112 | common_sizes = ( |
| 113 | [size for size in common_sizes if size_to_area(size) >= min_area] |
| 114 | ) |
| 115 | if not common_sizes: |
| 116 | raise AssertionError( |
| 117 | 'No common size above min_area between Preview and Video!' |
| 118 | ) |
| 119 | # Use areas of video sizes to find the smallest and largest common size |
| 120 | smallest_common_size = min(common_sizes, key=size_to_area) |
| 121 | largest_common_size = max(common_sizes, key=size_to_area) |
| 122 | logging.debug('Smallest common size: %s', smallest_common_size) |
| 123 | logging.debug('Largest common size: %s', largest_common_size) |
leslieshaw | 541e297 | 2023-02-28 11:06:07 -0800 | [diff] [blame] | 124 | # Find video quality of resolution with resolution as key |
Carlos Chan | fb27a5a | 2024-07-18 18:04:56 +0000 | [diff] [blame] | 125 | smallest_common_quality = ( |
| 126 | supported_video_size_to_quality[smallest_common_size] |
| 127 | ) |
| 128 | logging.debug('Smallest common quality: %s', smallest_common_quality) |
| 129 | largest_common_quality = supported_video_size_to_quality[largest_common_size] |
| 130 | logging.debug('Largest common quality: %s', largest_common_quality) |
| 131 | common_size_quality = CommonPreviewSizeData( |
| 132 | smallest_size=smallest_common_size, |
| 133 | smallest_quality=smallest_common_quality, |
| 134 | largest_size=largest_common_size, |
| 135 | largest_quality=largest_common_quality |
| 136 | ) |
| 137 | return common_size_quality |
leslieshaw | 541e297 | 2023-02-28 11:06:07 -0800 | [diff] [blame] | 138 | |
| 139 | |
Leslie Shaw | 5755b4d | 2024-08-30 16:21:01 -0700 | [diff] [blame] | 140 | def clamp_preview_sizes(preview_sizes, min_area=0, max_area=math.inf): |
| 141 | """Returns a list of preview_sizes with areas between min/max_area. |
| 142 | |
| 143 | Args: |
| 144 | preview_sizes: list; sizes to be filtered (ex. "1280x720") |
| 145 | min_area: int; optional filter to eliminate sizes <= to the specified |
| 146 | area (ex. 640*480). |
| 147 | max_area: int; optional filter to eliminate sizes >= to the specified |
| 148 | area (ex. 3840*2160). |
| 149 | Returns: |
| 150 | preview_sizes: list; filtered preview sizes clamped by min/max_area. |
| 151 | """ |
| 152 | size_to_area = lambda size: int(size.split('x')[0])*int(size.split('x')[1]) |
| 153 | filtered_preview_sizes = [ |
| 154 | size for size in preview_sizes |
| 155 | if max_area >= size_to_area(size) >= min_area] |
| 156 | logging.debug('Filtered preview sizes: %s', filtered_preview_sizes) |
| 157 | if not filtered_preview_sizes: |
| 158 | raise AssertionError(f'No preview sizes between {min_area} and {max_area}') |
| 159 | return filtered_preview_sizes |
| 160 | |
| 161 | |
Clemenz Portmann | 6e00e05 | 2023-02-14 16:39:57 -0800 | [diff] [blame] | 162 | def log_ffmpeg_version(): |
| 163 | """Logs the ffmpeg version being used.""" |
Rucha Katakwar | 31a5450 | 2022-08-10 16:32:30 -0700 | [diff] [blame] | 164 | |
| 165 | ffmpeg_version_cmd = ('ffmpeg -version') |
| 166 | p = subprocess.Popen(ffmpeg_version_cmd, shell=True, stdout=subprocess.PIPE) |
| 167 | output, _ = p.communicate() |
| 168 | if p.poll() != 0: |
| 169 | raise error_util.CameraItsError('Error running ffmpeg version cmd.') |
| 170 | decoded_output = output.decode('utf-8') |
Clemenz Portmann | 6e00e05 | 2023-02-14 16:39:57 -0800 | [diff] [blame] | 171 | logging.debug('ffmpeg version: %s', decoded_output.split(' ')[2]) |
Rucha Katakwar | 31a5450 | 2022-08-10 16:32:30 -0700 | [diff] [blame] | 172 | |
| 173 | |
Rucha Katakwar | 932bded | 2022-04-04 14:29:52 -0700 | [diff] [blame] | 174 | def extract_key_frames_from_video(log_path, video_file_name): |
Clemenz Portmann | 8d40e42 | 2022-04-28 10:29:01 -0700 | [diff] [blame] | 175 | """Returns a list of extracted key frames. |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 176 | |
| 177 | Ffmpeg tool is used to extract key frames from the video at path |
Rucha Katakwar | 932bded | 2022-04-04 14:29:52 -0700 | [diff] [blame] | 178 | os.path.join(log_path, video_file_name). |
| 179 | The extracted key frames will have the name video_file_name with "_key_frame" |
leslieshaw | 3e0fb3c | 2024-03-31 23:56:04 -0700 | [diff] [blame] | 180 | suffix to identify the frames for video of each quality. Since there can be |
Shuzhen Wang | 838577f | 2024-08-13 09:27:41 -0700 | [diff] [blame] | 181 | multiple key frames, each key frame image will be differentiated with its |
leslieshaw | 3e0fb3c | 2024-03-31 23:56:04 -0700 | [diff] [blame] | 182 | frame index. All the extracted key frames will be available in jpeg format |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 183 | at the same path as the video file. |
| 184 | |
Rucha Katakwar | 05167b7 | 2022-05-25 13:42:32 -0700 | [diff] [blame] | 185 | The run time flag '-loglevel quiet' hides the information from terminal. |
| 186 | In order to see the detailed output of ffmpeg command change the loglevel |
| 187 | option to 'info'. |
| 188 | |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 189 | Args: |
leslieshaw | 3e0fb3c | 2024-03-31 23:56:04 -0700 | [diff] [blame] | 190 | log_path: path for video file directory. |
Rucha Katakwar | 932bded | 2022-04-04 14:29:52 -0700 | [diff] [blame] | 191 | video_file_name: name of the video file. |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 192 | Returns: |
leslieshaw | 3e0fb3c | 2024-03-31 23:56:04 -0700 | [diff] [blame] | 193 | key_frame_files: a sorted list of files which contains a name per key |
| 194 | frame. Ex: VID_20220325_050918_0_preview_1920x1440_key_frame_0001.png |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 195 | """ |
Clemenz Portmann | 787a70c | 2024-05-06 13:44:32 -0700 | [diff] [blame] | 196 | ffmpeg_image_name = f'{os.path.splitext(video_file_name)[0]}_key_frame' |
Clemenz Portmann | 8d40e42 | 2022-04-28 10:29:01 -0700 | [diff] [blame] | 197 | ffmpeg_image_file_path = os.path.join( |
leslieshaw | 3e0fb3c | 2024-03-31 23:56:04 -0700 | [diff] [blame] | 198 | log_path, ffmpeg_image_name + '_%04d.png') |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 199 | cmd = ['ffmpeg', |
Clemenz Portmann | 8d40e42 | 2022-04-28 10:29:01 -0700 | [diff] [blame] | 200 | '-skip_frame', |
| 201 | 'nokey', |
| 202 | '-i', |
| 203 | os.path.join(log_path, video_file_name), |
| 204 | '-vsync', |
| 205 | 'vfr', |
| 206 | '-frame_pts', |
| 207 | 'true', |
| 208 | ffmpeg_image_file_path, |
Rucha Katakwar | 05167b7 | 2022-05-25 13:42:32 -0700 | [diff] [blame] | 209 | '-loglevel', |
| 210 | 'quiet', |
Clemenz Portmann | 8d40e42 | 2022-04-28 10:29:01 -0700 | [diff] [blame] | 211 | ] |
| 212 | logging.debug('Extracting key frames from: %s', video_file_name) |
Jonathan Liu | 98b9981 | 2023-07-14 13:20:38 -0700 | [diff] [blame] | 213 | _ = subprocess.call(cmd, |
| 214 | stdin=subprocess.DEVNULL, |
| 215 | stdout=subprocess.DEVNULL, |
| 216 | stderr=subprocess.DEVNULL) |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 217 | arr = os.listdir(os.path.join(log_path)) |
| 218 | key_frame_files = [] |
| 219 | for file in arr: |
| 220 | if '.png' in file and not os.path.isdir(file) and ffmpeg_image_name in file: |
| 221 | key_frame_files.append(file) |
leslieshaw | 3e0fb3c | 2024-03-31 23:56:04 -0700 | [diff] [blame] | 222 | key_frame_files.sort() |
Rucha Katakwar | 2e8ab09 | 2022-05-26 10:31:40 -0700 | [diff] [blame] | 223 | logging.debug('Extracted key frames: %s', key_frame_files) |
| 224 | logging.debug('Length of key_frame_files: %d', len(key_frame_files)) |
Clemenz Portmann | 00c6ef3 | 2022-06-02 16:21:19 -0700 | [diff] [blame] | 225 | if not key_frame_files: |
Rucha Katakwar | 2c49c45 | 2022-05-19 14:29:03 -0700 | [diff] [blame] | 226 | raise AssertionError('No key frames extracted. Check source video.') |
| 227 | |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 228 | return key_frame_files |
| 229 | |
| 230 | |
| 231 | def get_key_frame_to_process(key_frame_files): |
Clemenz Portmann | 8d40e42 | 2022-04-28 10:29:01 -0700 | [diff] [blame] | 232 | """Returns the key frame file from the list of key_frame_files. |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 233 | |
| 234 | If the size of the list is 1 then the file in the list will be returned else |
| 235 | the file with highest frame_index will be returned for further processing. |
| 236 | |
| 237 | Args: |
| 238 | key_frame_files: A list of key frame files. |
| 239 | Returns: |
| 240 | key_frame_file to be used for further processing. |
| 241 | """ |
Rucha Katakwar | 312d64e | 2022-05-25 10:24:25 -0700 | [diff] [blame] | 242 | if not key_frame_files: |
| 243 | raise AssertionError('key_frame_files list is empty.') |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 244 | key_frame_files.sort() |
| 245 | return key_frame_files[-1] |
Avichal Rakesh | f82d526 | 2022-04-22 16:24:18 -0700 | [diff] [blame] | 246 | |
| 247 | |
Leslie Shaw | ca6c763 | 2024-07-10 18:41:10 -0700 | [diff] [blame] | 248 | def extract_all_frames_from_video( |
| 249 | log_path, video_file_name, img_format, video_fps=None): |
| 250 | """Extracts and returns a list of frames from a video using FFmpeg. |
Avichal Rakesh | f82d526 | 2022-04-22 16:24:18 -0700 | [diff] [blame] | 251 | |
Leslie Shaw | ca6c763 | 2024-07-10 18:41:10 -0700 | [diff] [blame] | 252 | Extract all frames from the video at path <log_path>/<video_file_name>. |
| 253 | The extracted frames will have the name video_file_name with "_frame" |
| 254 | suffix to identify the frames for video of each size. Each frame image |
| 255 | will be differentiated with its frame index. All extracted rames will be |
| 256 | available in the provided img_format format at the same path as the video. |
Avichal Rakesh | f82d526 | 2022-04-22 16:24:18 -0700 | [diff] [blame] | 257 | |
Rucha Katakwar | 05167b7 | 2022-05-25 13:42:32 -0700 | [diff] [blame] | 258 | The run time flag '-loglevel quiet' hides the information from terminal. |
| 259 | In order to see the detailed output of ffmpeg command change the loglevel |
| 260 | option to 'info'. |
| 261 | |
Avichal Rakesh | f82d526 | 2022-04-22 16:24:18 -0700 | [diff] [blame] | 262 | Args: |
Leslie Shaw | ca6c763 | 2024-07-10 18:41:10 -0700 | [diff] [blame] | 263 | log_path: str; directory containing video file. |
Avichal Rakesh | f82d526 | 2022-04-22 16:24:18 -0700 | [diff] [blame] | 264 | video_file_name: str; name of the video file. |
Leslie Shaw | ca6c763 | 2024-07-10 18:41:10 -0700 | [diff] [blame] | 265 | img_format: str; desired image format for export frames. ex. 'png' |
| 266 | video_fps: str; fps of imported video. |
Avichal Rakesh | f82d526 | 2022-04-22 16:24:18 -0700 | [diff] [blame] | 267 | Returns: |
Leslie Shaw | ca6c763 | 2024-07-10 18:41:10 -0700 | [diff] [blame] | 268 | an ordered list of paths to the extracted frame images. |
Avichal Rakesh | f82d526 | 2022-04-22 16:24:18 -0700 | [diff] [blame] | 269 | """ |
| 270 | logging.debug('Extracting all frames') |
| 271 | ffmpeg_image_name = f"{video_file_name.split('.')[0]}_frame" |
| 272 | logging.debug('ffmpeg_image_name: %s', ffmpeg_image_name) |
| 273 | ffmpeg_image_file_names = ( |
Dipen Patel | c4c10e9 | 2024-05-07 16:35:35 -0700 | [diff] [blame] | 274 | f'{os.path.join(log_path, ffmpeg_image_name)}_%04d.{img_format}') |
Leslie Shaw | ca6c763 | 2024-07-10 18:41:10 -0700 | [diff] [blame] | 275 | if video_fps: |
| 276 | cmd = [ |
| 277 | 'ffmpeg', '-i', os.path.join(log_path, video_file_name), |
| 278 | '-r', video_fps, # force a constant frame rate for reliability |
| 279 | ffmpeg_image_file_names, '-loglevel', 'quiet' |
| 280 | ] |
| 281 | else: |
| 282 | cmd = [ |
| 283 | 'ffmpeg', '-i', os.path.join(log_path, video_file_name), |
| 284 | '-vsync', 'passthrough', # prevents frame drops during decoding |
| 285 | ffmpeg_image_file_names, '-loglevel', 'quiet' |
| 286 | ] |
| 287 | subprocess.call(cmd, |
| 288 | stdin=subprocess.DEVNULL, |
| 289 | stdout=subprocess.DEVNULL, |
| 290 | stderr=subprocess.DEVNULL) |
Avichal Rakesh | f82d526 | 2022-04-22 16:24:18 -0700 | [diff] [blame] | 291 | |
Leslie Shaw | ca6c763 | 2024-07-10 18:41:10 -0700 | [diff] [blame] | 292 | files = sorted( |
| 293 | [file for file in os.listdir(log_path) if |
| 294 | (file.endswith(img_format) and ffmpeg_image_name in file)]) |
| 295 | if not files: |
Rucha Katakwar | 2c49c45 | 2022-05-19 14:29:03 -0700 | [diff] [blame] | 296 | raise AssertionError('No frames extracted. Check source video.') |
| 297 | |
Leslie Shaw | ca6c763 | 2024-07-10 18:41:10 -0700 | [diff] [blame] | 298 | return files |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 299 | |
| 300 | |
leslieshaw | 21c49e51 | 2023-08-25 13:47:28 -0700 | [diff] [blame] | 301 | def extract_last_key_frame_from_recording(log_path, file_name): |
leslieshaw | 5b54ee5 | 2023-08-29 13:22:30 -0700 | [diff] [blame] | 302 | """Extract last key frame from recordings. |
leslieshaw | 21c49e51 | 2023-08-25 13:47:28 -0700 | [diff] [blame] | 303 | |
| 304 | Args: |
| 305 | log_path: str; file location |
| 306 | file_name: str file name for saved video |
| 307 | |
| 308 | Returns: |
Clemenz Portmann | 25529eb | 2023-09-17 21:13:15 -0700 | [diff] [blame] | 309 | numpy image of last key frame |
leslieshaw | 21c49e51 | 2023-08-25 13:47:28 -0700 | [diff] [blame] | 310 | """ |
| 311 | key_frame_files = extract_key_frames_from_video(log_path, file_name) |
| 312 | logging.debug('key_frame_files: %s', key_frame_files) |
| 313 | |
| 314 | # Get the last_key_frame file to process. |
| 315 | last_key_frame_file = get_key_frame_to_process(key_frame_files) |
| 316 | logging.debug('last_key_frame: %s', last_key_frame_file) |
| 317 | |
Clemenz Portmann | 25529eb | 2023-09-17 21:13:15 -0700 | [diff] [blame] | 318 | # Convert last_key_frame to numpy array |
leslieshaw | 21c49e51 | 2023-08-25 13:47:28 -0700 | [diff] [blame] | 319 | np_image = image_processing_utils.convert_image_to_numpy_array( |
| 320 | os.path.join(log_path, last_key_frame_file)) |
| 321 | logging.debug('last key frame image shape: %s', np_image.shape) |
| 322 | |
| 323 | return np_image |
| 324 | |
| 325 | |
Shuzhen Wang | 9140e45 | 2024-08-01 13:41:52 -0700 | [diff] [blame] | 326 | def get_avg_frame_rate(video_file_name_with_path): |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 327 | """Get average frame rate assuming variable frame rate video. |
| 328 | |
| 329 | Args: |
| 330 | video_file_name_with_path: path to the video to be analyzed |
| 331 | Returns: |
| 332 | Float. average frames per second. |
| 333 | """ |
| 334 | |
Jonathan Liu | fb3f603 | 2023-08-21 11:02:40 -0700 | [diff] [blame] | 335 | cmd = ['ffprobe', |
| 336 | '-v', |
| 337 | 'quiet', |
| 338 | '-show_streams', |
| 339 | '-select_streams', |
| 340 | 'v:0', # first video stream |
| 341 | video_file_name_with_path |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 342 | ] |
| 343 | logging.debug('Getting frame rate') |
| 344 | raw_output = '' |
| 345 | try: |
Jonathan Liu | 98b9981 | 2023-07-14 13:20:38 -0700 | [diff] [blame] | 346 | raw_output = subprocess.check_output(cmd, |
| 347 | stdin=subprocess.DEVNULL, |
| 348 | stderr=subprocess.STDOUT) |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 349 | except subprocess.CalledProcessError as e: |
| 350 | raise AssertionError(str(e.output)) from e |
| 351 | if raw_output: |
| 352 | output = str(raw_output.decode('utf-8')).strip() |
Jonathan Liu | fb3f603 | 2023-08-21 11:02:40 -0700 | [diff] [blame] | 353 | logging.debug('ffprobe command %s output: %s', ' '.join(cmd), output) |
| 354 | average_frame_rate_data = ( |
Kailiang Chen | 6526b3a | 2024-01-25 11:28:58 -0800 | [diff] [blame] | 355 | re.search(r'avg_frame_rate=*([0-9]+/[0-9]+)', output) |
| 356 | .group(INDEX_FIRST_SUBGROUP) |
Jonathan Liu | fb3f603 | 2023-08-21 11:02:40 -0700 | [diff] [blame] | 357 | ) |
| 358 | average_frame_rate = (int(average_frame_rate_data.split('/')[0]) / |
| 359 | int(average_frame_rate_data.split('/')[1])) |
| 360 | logging.debug('Average FPS: %.4f', average_frame_rate) |
| 361 | return average_frame_rate |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 362 | else: |
Jonathan Liu | fb3f603 | 2023-08-21 11:02:40 -0700 | [diff] [blame] | 363 | raise AssertionError('ffprobe failed to provide frame rate data') |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 364 | |
| 365 | |
| 366 | def get_frame_deltas(video_file_name_with_path, timestamp_type='pts'): |
| 367 | """Get list of time diffs between frames. |
| 368 | |
| 369 | Args: |
| 370 | video_file_name_with_path: path to the video to be analyzed |
Jonathan Liu | 1c465e8 | 2025-02-18 16:24:21 -0800 | [diff] [blame] | 371 | timestamp_type: 'pts' or 'pkt_dts' |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 372 | Returns: |
| 373 | List of floats. Time diffs between frames in seconds. |
| 374 | """ |
| 375 | |
Jonathan Liu | 1c465e8 | 2025-02-18 16:24:21 -0800 | [diff] [blame] | 376 | cmd = [ |
| 377 | 'ffprobe', '-show_entries', f'frame={timestamp_type}_time', |
| 378 | '-select_streams', 'v', video_file_name_with_path |
| 379 | ] |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 380 | logging.debug('Getting frame deltas') |
| 381 | raw_output = '' |
| 382 | try: |
Jonathan Liu | 98b9981 | 2023-07-14 13:20:38 -0700 | [diff] [blame] | 383 | raw_output = subprocess.check_output(cmd, |
| 384 | stdin=subprocess.DEVNULL, |
| 385 | stderr=subprocess.STDOUT) |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 386 | except subprocess.CalledProcessError as e: |
| 387 | raise AssertionError(str(e.output)) from e |
| 388 | if raw_output: |
| 389 | output = str(raw_output.decode('utf-8')).strip().split('\n') |
| 390 | deltas = [] |
Jonathan Liu | 61b36cf | 2023-06-14 10:30:06 -0700 | [diff] [blame] | 391 | prev_time = None |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 392 | for line in output: |
| 393 | if timestamp_type not in line: |
| 394 | continue |
Kailiang Chen | 6526b3a | 2024-01-25 11:28:58 -0800 | [diff] [blame] | 395 | curr_time = float(re.search(r'time= *([0-9][0-9\.]*)', line) |
| 396 | .group(INDEX_FIRST_SUBGROUP)) |
Jonathan Liu | 61b36cf | 2023-06-14 10:30:06 -0700 | [diff] [blame] | 397 | if prev_time is not None: |
| 398 | deltas.append(curr_time - prev_time) |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 399 | prev_time = curr_time |
| 400 | logging.debug('Frame deltas: %s', deltas) |
| 401 | return deltas |
| 402 | else: |
| 403 | raise AssertionError('ffprobe failed to provide frame delta data') |
Kailiang Chen | 6526b3a | 2024-01-25 11:28:58 -0800 | [diff] [blame] | 404 | |
| 405 | |
| 406 | def get_video_colorspace(log_path, video_file_name): |
| 407 | """Get the video colorspace. |
| 408 | |
| 409 | Args: |
| 410 | log_path: path for video file directory |
| 411 | video_file_name: name of the video file |
| 412 | Returns: |
| 413 | video colorspace, e.g. BT.2020 or BT.709 |
| 414 | """ |
| 415 | |
| 416 | cmd = ['ffprobe', |
| 417 | '-show_streams', |
| 418 | '-select_streams', |
| 419 | 'v:0', |
| 420 | '-of', |
| 421 | 'json', |
| 422 | '-i', |
| 423 | os.path.join(log_path, video_file_name) |
| 424 | ] |
| 425 | logging.debug('Get the video colorspace') |
| 426 | raw_output = '' |
| 427 | try: |
| 428 | raw_output = subprocess.check_output(cmd, |
| 429 | stdin=subprocess.DEVNULL, |
| 430 | stderr=subprocess.STDOUT) |
| 431 | except subprocess.CalledProcessError as e: |
| 432 | raise AssertionError(str(e.output)) from e |
| 433 | |
| 434 | logging.debug('raw_output: %s', raw_output) |
| 435 | if raw_output: |
| 436 | colorspace = '' |
| 437 | output = str(raw_output.decode('utf-8')).strip().split('\n') |
| 438 | logging.debug('output: %s', output) |
| 439 | for line in output: |
| 440 | logging.debug('line: %s', line) |
| 441 | metadata = re.search(r'"color_space": ("[a-z0-9]*")', line) |
| 442 | if metadata: |
| 443 | colorspace = metadata.group(INDEX_FIRST_SUBGROUP) |
| 444 | logging.debug('Colorspace: %s', colorspace) |
| 445 | return colorspace |
| 446 | else: |
| 447 | raise AssertionError('ffprobe failed to provide color space') |