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 | |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 21 | import logging |
| 22 | import os.path |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 23 | import re |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 24 | import subprocess |
Rucha Katakwar | 31a5450 | 2022-08-10 16:32:30 -0700 | [diff] [blame] | 25 | import error_util |
leslieshaw | 21c49e51 | 2023-08-25 13:47:28 -0700 | [diff] [blame] | 26 | import image_processing_utils |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 27 | |
| 28 | |
Kailiang Chen | 6526b3a | 2024-01-25 11:28:58 -0800 | [diff] [blame] | 29 | COLORSPACE_HDR = 'bt2020' |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 30 | HR_TO_SEC = 3600 |
Kailiang Chen | 6526b3a | 2024-01-25 11:28:58 -0800 | [diff] [blame] | 31 | INDEX_FIRST_SUBGROUP = 1 |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 32 | MIN_TO_SEC = 60 |
| 33 | |
Rucha Katakwar | 089e609 | 2022-03-30 11:19:26 -0700 | [diff] [blame] | 34 | ITS_SUPPORTED_QUALITIES = ( |
Rucha Katakwar | 41133f9 | 2022-03-22 13:44:09 -0700 | [diff] [blame] | 35 | 'HIGH', |
| 36 | '2160P', |
| 37 | '1080P', |
| 38 | '720P', |
| 39 | '480P', |
| 40 | 'CIF', |
| 41 | 'QCIF', |
| 42 | 'QVGA', |
| 43 | 'LOW', |
| 44 | 'VGA' |
Rucha Katakwar | 222c531 | 2022-03-21 14:17:01 -0700 | [diff] [blame] | 45 | ) |
Rucha Katakwar | 0ced2c3 | 2022-08-01 14:54:42 -0700 | [diff] [blame] | 46 | |
leslieshaw | 6fb2c07 | 2023-04-06 16:38:54 -0700 | [diff] [blame] | 47 | LOW_RESOLUTION_SIZES = ( |
| 48 | '176x144', |
| 49 | '192x144', |
| 50 | '352x288', |
| 51 | '384x288', |
| 52 | '320x240', |
| 53 | ) |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 54 | |
leslieshaw | 6fb2c07 | 2023-04-06 16:38:54 -0700 | [diff] [blame] | 55 | LOWEST_RES_TESTED_AREA = 640*360 |
leslieshaw | 99fd75b | 2023-01-09 15:01:19 -0800 | [diff] [blame] | 56 | |
leslieshaw | 541e297 | 2023-02-28 11:06:07 -0800 | [diff] [blame] | 57 | VIDEO_QUALITY_SIZE = { |
Clemenz Portmann | 703010e | 2023-07-21 16:39:18 -0700 | [diff] [blame] | 58 | # '480P', '1080P', HIGH' & 'LOW' are not included as they are DUT-dependent |
leslieshaw | 541e297 | 2023-02-28 11:06:07 -0800 | [diff] [blame] | 59 | '2160P': '3840x2160', |
leslieshaw | 541e297 | 2023-02-28 11:06:07 -0800 | [diff] [blame] | 60 | '720P': '1280x720', |
leslieshaw | 541e297 | 2023-02-28 11:06:07 -0800 | [diff] [blame] | 61 | 'VGA': '640x480', |
| 62 | 'CIF': '352x288', |
| 63 | 'QVGA': '320x240', |
| 64 | 'QCIF': '176x144', |
| 65 | } |
| 66 | |
| 67 | |
Clemenz Portmann | ba0de52 | 2024-04-30 08:01:20 -0700 | [diff] [blame] | 68 | def get_lowest_common_preview_video_size( |
leslieshaw | 541e297 | 2023-02-28 11:06:07 -0800 | [diff] [blame] | 69 | supported_preview_sizes, supported_video_qualities, min_area): |
| 70 | """Returns the common, smallest size above minimum in preview and video. |
| 71 | |
| 72 | Args: |
| 73 | supported_preview_sizes: str; preview size (ex. '1920x1080') |
| 74 | supported_video_qualities: str; video recording quality and id pair |
| 75 | (ex. '480P:4', '720P:5'') |
| 76 | min_area: int; filter to eliminate smaller sizes (ex. 640*480) |
| 77 | Returns: |
| 78 | smallest_common_size: str; smallest, common size between preview and video |
| 79 | smallest_common_video_quality: str; video recording quality such as 480P |
| 80 | """ |
| 81 | |
| 82 | # Make dictionary on video quality and size according to compatibility |
| 83 | supported_video_size_to_quality = {} |
| 84 | for quality in supported_video_qualities: |
| 85 | video_quality = quality.split(':')[0] |
| 86 | if video_quality in VIDEO_QUALITY_SIZE: |
| 87 | video_size = VIDEO_QUALITY_SIZE[video_quality] |
| 88 | supported_video_size_to_quality[video_size] = video_quality |
| 89 | logging.debug( |
| 90 | 'Supported video size to quality: %s', supported_video_size_to_quality) |
| 91 | |
| 92 | # Use areas of video sizes to find the smallest, common size |
| 93 | size_to_area = lambda s: int(s.split('x')[0])*int(s.split('x')[1]) |
| 94 | smallest_common_size = '' |
| 95 | smallest_area = float('inf') |
| 96 | for size in supported_preview_sizes: |
| 97 | if size in supported_video_size_to_quality: |
| 98 | area = size_to_area(size) |
| 99 | if smallest_area > area >= min_area: |
| 100 | smallest_area = area |
| 101 | smallest_common_size = size |
| 102 | logging.debug('Lowest common size: %s', smallest_common_size) |
| 103 | |
| 104 | # Find video quality of resolution with resolution as key |
| 105 | smallest_common_video_quality = ( |
| 106 | supported_video_size_to_quality[smallest_common_size]) |
| 107 | logging.debug( |
| 108 | 'Lowest common size video quality: %s', smallest_common_video_quality) |
| 109 | |
| 110 | return smallest_common_size, smallest_common_video_quality |
| 111 | |
| 112 | |
Clemenz Portmann | 6e00e05 | 2023-02-14 16:39:57 -0800 | [diff] [blame] | 113 | def log_ffmpeg_version(): |
| 114 | """Logs the ffmpeg version being used.""" |
Rucha Katakwar | 31a5450 | 2022-08-10 16:32:30 -0700 | [diff] [blame] | 115 | |
| 116 | ffmpeg_version_cmd = ('ffmpeg -version') |
| 117 | p = subprocess.Popen(ffmpeg_version_cmd, shell=True, stdout=subprocess.PIPE) |
| 118 | output, _ = p.communicate() |
| 119 | if p.poll() != 0: |
| 120 | raise error_util.CameraItsError('Error running ffmpeg version cmd.') |
| 121 | decoded_output = output.decode('utf-8') |
Clemenz Portmann | 6e00e05 | 2023-02-14 16:39:57 -0800 | [diff] [blame] | 122 | logging.debug('ffmpeg version: %s', decoded_output.split(' ')[2]) |
Rucha Katakwar | 31a5450 | 2022-08-10 16:32:30 -0700 | [diff] [blame] | 123 | |
| 124 | |
Rucha Katakwar | 932bded | 2022-04-04 14:29:52 -0700 | [diff] [blame] | 125 | def extract_key_frames_from_video(log_path, video_file_name): |
Clemenz Portmann | 8d40e42 | 2022-04-28 10:29:01 -0700 | [diff] [blame] | 126 | """Returns a list of extracted key frames. |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 127 | |
| 128 | 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] | 129 | os.path.join(log_path, video_file_name). |
| 130 | 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] | 131 | suffix to identify the frames for video of each quality. Since there can be |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 132 | multiple key frames, each key frame image will be differentiated with it's |
leslieshaw | 3e0fb3c | 2024-03-31 23:56:04 -0700 | [diff] [blame] | 133 | 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] | 134 | at the same path as the video file. |
| 135 | |
Rucha Katakwar | 05167b7 | 2022-05-25 13:42:32 -0700 | [diff] [blame] | 136 | The run time flag '-loglevel quiet' hides the information from terminal. |
| 137 | In order to see the detailed output of ffmpeg command change the loglevel |
| 138 | option to 'info'. |
| 139 | |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 140 | Args: |
leslieshaw | 3e0fb3c | 2024-03-31 23:56:04 -0700 | [diff] [blame] | 141 | log_path: path for video file directory. |
Rucha Katakwar | 932bded | 2022-04-04 14:29:52 -0700 | [diff] [blame] | 142 | video_file_name: name of the video file. |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 143 | Returns: |
leslieshaw | 3e0fb3c | 2024-03-31 23:56:04 -0700 | [diff] [blame] | 144 | key_frame_files: a sorted list of files which contains a name per key |
| 145 | frame. Ex: VID_20220325_050918_0_preview_1920x1440_key_frame_0001.png |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 146 | """ |
Clemenz Portmann | 787a70c | 2024-05-06 13:44:32 -0700 | [diff] [blame] | 147 | 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] | 148 | ffmpeg_image_file_path = os.path.join( |
leslieshaw | 3e0fb3c | 2024-03-31 23:56:04 -0700 | [diff] [blame] | 149 | log_path, ffmpeg_image_name + '_%04d.png') |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 150 | cmd = ['ffmpeg', |
Clemenz Portmann | 8d40e42 | 2022-04-28 10:29:01 -0700 | [diff] [blame] | 151 | '-skip_frame', |
| 152 | 'nokey', |
| 153 | '-i', |
| 154 | os.path.join(log_path, video_file_name), |
| 155 | '-vsync', |
| 156 | 'vfr', |
| 157 | '-frame_pts', |
| 158 | 'true', |
| 159 | ffmpeg_image_file_path, |
Rucha Katakwar | 05167b7 | 2022-05-25 13:42:32 -0700 | [diff] [blame] | 160 | '-loglevel', |
| 161 | 'quiet', |
Clemenz Portmann | 8d40e42 | 2022-04-28 10:29:01 -0700 | [diff] [blame] | 162 | ] |
| 163 | logging.debug('Extracting key frames from: %s', video_file_name) |
Jonathan Liu | 98b9981 | 2023-07-14 13:20:38 -0700 | [diff] [blame] | 164 | _ = subprocess.call(cmd, |
| 165 | stdin=subprocess.DEVNULL, |
| 166 | stdout=subprocess.DEVNULL, |
| 167 | stderr=subprocess.DEVNULL) |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 168 | arr = os.listdir(os.path.join(log_path)) |
| 169 | key_frame_files = [] |
| 170 | for file in arr: |
| 171 | if '.png' in file and not os.path.isdir(file) and ffmpeg_image_name in file: |
| 172 | key_frame_files.append(file) |
leslieshaw | 3e0fb3c | 2024-03-31 23:56:04 -0700 | [diff] [blame] | 173 | key_frame_files.sort() |
Rucha Katakwar | 2e8ab09 | 2022-05-26 10:31:40 -0700 | [diff] [blame] | 174 | logging.debug('Extracted key frames: %s', key_frame_files) |
| 175 | logging.debug('Length of key_frame_files: %d', len(key_frame_files)) |
Clemenz Portmann | 00c6ef3 | 2022-06-02 16:21:19 -0700 | [diff] [blame] | 176 | if not key_frame_files: |
Rucha Katakwar | 2c49c45 | 2022-05-19 14:29:03 -0700 | [diff] [blame] | 177 | raise AssertionError('No key frames extracted. Check source video.') |
| 178 | |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 179 | return key_frame_files |
| 180 | |
| 181 | |
| 182 | def get_key_frame_to_process(key_frame_files): |
Clemenz Portmann | 8d40e42 | 2022-04-28 10:29:01 -0700 | [diff] [blame] | 183 | """Returns the key frame file from the list of key_frame_files. |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 184 | |
| 185 | If the size of the list is 1 then the file in the list will be returned else |
| 186 | the file with highest frame_index will be returned for further processing. |
| 187 | |
| 188 | Args: |
| 189 | key_frame_files: A list of key frame files. |
| 190 | Returns: |
| 191 | key_frame_file to be used for further processing. |
| 192 | """ |
Rucha Katakwar | 312d64e | 2022-05-25 10:24:25 -0700 | [diff] [blame] | 193 | if not key_frame_files: |
| 194 | raise AssertionError('key_frame_files list is empty.') |
Rucha Katakwar | b6847db | 2022-03-24 16:49:13 -0700 | [diff] [blame] | 195 | key_frame_files.sort() |
| 196 | return key_frame_files[-1] |
Avichal Rakesh | f82d526 | 2022-04-22 16:24:18 -0700 | [diff] [blame] | 197 | |
| 198 | |
| 199 | def extract_all_frames_from_video(log_path, video_file_name, img_format): |
Clemenz Portmann | 8d40e42 | 2022-04-28 10:29:01 -0700 | [diff] [blame] | 200 | """Extracts and returns a list of all extracted frames. |
Avichal Rakesh | f82d526 | 2022-04-22 16:24:18 -0700 | [diff] [blame] | 201 | |
| 202 | Ffmpeg tool is used to extract all frames from the video at path |
| 203 | <log_path>/<video_file_name>. The extracted key frames will have the name |
| 204 | video_file_name with "_frame" suffix to identify the frames for video of each |
| 205 | size. Each frame image will be differentiated with its frame index. All |
| 206 | extracted key frames will be available in the provided img_format format at |
| 207 | the same path as the video file. |
| 208 | |
Rucha Katakwar | 05167b7 | 2022-05-25 13:42:32 -0700 | [diff] [blame] | 209 | The run time flag '-loglevel quiet' hides the information from terminal. |
| 210 | In order to see the detailed output of ffmpeg command change the loglevel |
| 211 | option to 'info'. |
| 212 | |
Avichal Rakesh | f82d526 | 2022-04-22 16:24:18 -0700 | [diff] [blame] | 213 | Args: |
| 214 | log_path: str; path for video file directory |
| 215 | video_file_name: str; name of the video file. |
| 216 | img_format: str; type of image to export frames into. ex. 'png' |
| 217 | Returns: |
| 218 | key_frame_files: An ordered list of paths for each frame extracted from the |
| 219 | video |
| 220 | """ |
| 221 | logging.debug('Extracting all frames') |
| 222 | ffmpeg_image_name = f"{video_file_name.split('.')[0]}_frame" |
| 223 | logging.debug('ffmpeg_image_name: %s', ffmpeg_image_name) |
| 224 | ffmpeg_image_file_names = ( |
Dipen Patel | c4c10e9 | 2024-05-07 16:35:35 -0700 | [diff] [blame^] | 225 | f'{os.path.join(log_path, ffmpeg_image_name)}_%04d.{img_format}') |
Avichal Rakesh | f82d526 | 2022-04-22 16:24:18 -0700 | [diff] [blame] | 226 | cmd = [ |
| 227 | 'ffmpeg', '-i', os.path.join(log_path, video_file_name), |
Dipen Patel | c4c10e9 | 2024-05-07 16:35:35 -0700 | [diff] [blame^] | 228 | '-vsync', 'passthrough', # prevents frame drops during decoding |
Rucha Katakwar | 05167b7 | 2022-05-25 13:42:32 -0700 | [diff] [blame] | 229 | ffmpeg_image_file_names, '-loglevel', 'quiet' |
Avichal Rakesh | f82d526 | 2022-04-22 16:24:18 -0700 | [diff] [blame] | 230 | ] |
Jonathan Liu | 98b9981 | 2023-07-14 13:20:38 -0700 | [diff] [blame] | 231 | _ = subprocess.call(cmd, |
| 232 | stdin=subprocess.DEVNULL, |
| 233 | stdout=subprocess.DEVNULL, |
| 234 | stderr=subprocess.DEVNULL) |
Avichal Rakesh | f82d526 | 2022-04-22 16:24:18 -0700 | [diff] [blame] | 235 | |
| 236 | file_list = sorted( |
| 237 | [_ for _ in os.listdir(log_path) if (_.endswith(img_format) |
| 238 | and ffmpeg_image_name in _)]) |
Clemenz Portmann | 00c6ef3 | 2022-06-02 16:21:19 -0700 | [diff] [blame] | 239 | if not file_list: |
Rucha Katakwar | 2c49c45 | 2022-05-19 14:29:03 -0700 | [diff] [blame] | 240 | raise AssertionError('No frames extracted. Check source video.') |
| 241 | |
Avichal Rakesh | f82d526 | 2022-04-22 16:24:18 -0700 | [diff] [blame] | 242 | return file_list |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 243 | |
| 244 | |
leslieshaw | 21c49e51 | 2023-08-25 13:47:28 -0700 | [diff] [blame] | 245 | def extract_last_key_frame_from_recording(log_path, file_name): |
leslieshaw | 5b54ee5 | 2023-08-29 13:22:30 -0700 | [diff] [blame] | 246 | """Extract last key frame from recordings. |
leslieshaw | 21c49e51 | 2023-08-25 13:47:28 -0700 | [diff] [blame] | 247 | |
| 248 | Args: |
| 249 | log_path: str; file location |
| 250 | file_name: str file name for saved video |
| 251 | |
| 252 | Returns: |
Clemenz Portmann | 25529eb | 2023-09-17 21:13:15 -0700 | [diff] [blame] | 253 | numpy image of last key frame |
leslieshaw | 21c49e51 | 2023-08-25 13:47:28 -0700 | [diff] [blame] | 254 | """ |
| 255 | key_frame_files = extract_key_frames_from_video(log_path, file_name) |
| 256 | logging.debug('key_frame_files: %s', key_frame_files) |
| 257 | |
| 258 | # Get the last_key_frame file to process. |
| 259 | last_key_frame_file = get_key_frame_to_process(key_frame_files) |
| 260 | logging.debug('last_key_frame: %s', last_key_frame_file) |
| 261 | |
Clemenz Portmann | 25529eb | 2023-09-17 21:13:15 -0700 | [diff] [blame] | 262 | # Convert last_key_frame to numpy array |
leslieshaw | 21c49e51 | 2023-08-25 13:47:28 -0700 | [diff] [blame] | 263 | np_image = image_processing_utils.convert_image_to_numpy_array( |
| 264 | os.path.join(log_path, last_key_frame_file)) |
| 265 | logging.debug('last key frame image shape: %s', np_image.shape) |
| 266 | |
| 267 | return np_image |
| 268 | |
| 269 | |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 270 | def get_average_frame_rate(video_file_name_with_path): |
| 271 | """Get average frame rate assuming variable frame rate video. |
| 272 | |
| 273 | Args: |
| 274 | video_file_name_with_path: path to the video to be analyzed |
| 275 | Returns: |
| 276 | Float. average frames per second. |
| 277 | """ |
| 278 | |
Jonathan Liu | fb3f603 | 2023-08-21 11:02:40 -0700 | [diff] [blame] | 279 | cmd = ['ffprobe', |
| 280 | '-v', |
| 281 | 'quiet', |
| 282 | '-show_streams', |
| 283 | '-select_streams', |
| 284 | 'v:0', # first video stream |
| 285 | video_file_name_with_path |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 286 | ] |
| 287 | logging.debug('Getting frame rate') |
| 288 | raw_output = '' |
| 289 | try: |
Jonathan Liu | 98b9981 | 2023-07-14 13:20:38 -0700 | [diff] [blame] | 290 | raw_output = subprocess.check_output(cmd, |
| 291 | stdin=subprocess.DEVNULL, |
| 292 | stderr=subprocess.STDOUT) |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 293 | except subprocess.CalledProcessError as e: |
| 294 | raise AssertionError(str(e.output)) from e |
| 295 | if raw_output: |
| 296 | output = str(raw_output.decode('utf-8')).strip() |
Jonathan Liu | fb3f603 | 2023-08-21 11:02:40 -0700 | [diff] [blame] | 297 | logging.debug('ffprobe command %s output: %s', ' '.join(cmd), output) |
| 298 | average_frame_rate_data = ( |
Kailiang Chen | 6526b3a | 2024-01-25 11:28:58 -0800 | [diff] [blame] | 299 | re.search(r'avg_frame_rate=*([0-9]+/[0-9]+)', output) |
| 300 | .group(INDEX_FIRST_SUBGROUP) |
Jonathan Liu | fb3f603 | 2023-08-21 11:02:40 -0700 | [diff] [blame] | 301 | ) |
| 302 | average_frame_rate = (int(average_frame_rate_data.split('/')[0]) / |
| 303 | int(average_frame_rate_data.split('/')[1])) |
| 304 | logging.debug('Average FPS: %.4f', average_frame_rate) |
| 305 | return average_frame_rate |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 306 | else: |
Jonathan Liu | fb3f603 | 2023-08-21 11:02:40 -0700 | [diff] [blame] | 307 | raise AssertionError('ffprobe failed to provide frame rate data') |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 308 | |
| 309 | |
| 310 | def get_frame_deltas(video_file_name_with_path, timestamp_type='pts'): |
| 311 | """Get list of time diffs between frames. |
| 312 | |
| 313 | Args: |
| 314 | video_file_name_with_path: path to the video to be analyzed |
| 315 | timestamp_type: 'pts' or 'dts' |
| 316 | Returns: |
| 317 | List of floats. Time diffs between frames in seconds. |
| 318 | """ |
| 319 | |
| 320 | cmd = ['ffprobe', |
| 321 | '-show_entries', |
| 322 | f'frame=pkt_{timestamp_type}_time', |
| 323 | '-select_streams', |
| 324 | 'v', |
| 325 | video_file_name_with_path |
| 326 | ] |
| 327 | logging.debug('Getting frame deltas') |
| 328 | raw_output = '' |
| 329 | try: |
Jonathan Liu | 98b9981 | 2023-07-14 13:20:38 -0700 | [diff] [blame] | 330 | raw_output = subprocess.check_output(cmd, |
| 331 | stdin=subprocess.DEVNULL, |
| 332 | stderr=subprocess.STDOUT) |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 333 | except subprocess.CalledProcessError as e: |
| 334 | raise AssertionError(str(e.output)) from e |
| 335 | if raw_output: |
| 336 | output = str(raw_output.decode('utf-8')).strip().split('\n') |
| 337 | deltas = [] |
Jonathan Liu | 61b36cf | 2023-06-14 10:30:06 -0700 | [diff] [blame] | 338 | prev_time = None |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 339 | for line in output: |
| 340 | if timestamp_type not in line: |
| 341 | continue |
Kailiang Chen | 6526b3a | 2024-01-25 11:28:58 -0800 | [diff] [blame] | 342 | curr_time = float(re.search(r'time= *([0-9][0-9\.]*)', line) |
| 343 | .group(INDEX_FIRST_SUBGROUP)) |
Jonathan Liu | 61b36cf | 2023-06-14 10:30:06 -0700 | [diff] [blame] | 344 | if prev_time is not None: |
| 345 | deltas.append(curr_time - prev_time) |
Jonathan Liu | 76f5168 | 2022-11-12 00:27:47 +0000 | [diff] [blame] | 346 | prev_time = curr_time |
| 347 | logging.debug('Frame deltas: %s', deltas) |
| 348 | return deltas |
| 349 | else: |
| 350 | raise AssertionError('ffprobe failed to provide frame delta data') |
Kailiang Chen | 6526b3a | 2024-01-25 11:28:58 -0800 | [diff] [blame] | 351 | |
| 352 | |
| 353 | def get_video_colorspace(log_path, video_file_name): |
| 354 | """Get the video colorspace. |
| 355 | |
| 356 | Args: |
| 357 | log_path: path for video file directory |
| 358 | video_file_name: name of the video file |
| 359 | Returns: |
| 360 | video colorspace, e.g. BT.2020 or BT.709 |
| 361 | """ |
| 362 | |
| 363 | cmd = ['ffprobe', |
| 364 | '-show_streams', |
| 365 | '-select_streams', |
| 366 | 'v:0', |
| 367 | '-of', |
| 368 | 'json', |
| 369 | '-i', |
| 370 | os.path.join(log_path, video_file_name) |
| 371 | ] |
| 372 | logging.debug('Get the video colorspace') |
| 373 | raw_output = '' |
| 374 | try: |
| 375 | raw_output = subprocess.check_output(cmd, |
| 376 | stdin=subprocess.DEVNULL, |
| 377 | stderr=subprocess.STDOUT) |
| 378 | except subprocess.CalledProcessError as e: |
| 379 | raise AssertionError(str(e.output)) from e |
| 380 | |
| 381 | logging.debug('raw_output: %s', raw_output) |
| 382 | if raw_output: |
| 383 | colorspace = '' |
| 384 | output = str(raw_output.decode('utf-8')).strip().split('\n') |
| 385 | logging.debug('output: %s', output) |
| 386 | for line in output: |
| 387 | logging.debug('line: %s', line) |
| 388 | metadata = re.search(r'"color_space": ("[a-z0-9]*")', line) |
| 389 | if metadata: |
| 390 | colorspace = metadata.group(INDEX_FIRST_SUBGROUP) |
| 391 | logging.debug('Colorspace: %s', colorspace) |
| 392 | return colorspace |
| 393 | else: |
| 394 | raise AssertionError('ffprobe failed to provide color space') |