Try different codecs, see which are available. Returns a dictionary with of available codecs with codecs as keys and file extensions as values.
()
| 166 | |
| 167 | @staticmethod |
| 168 | def get_available_codecs() -> dict[str, str]: |
| 169 | """Try different codecs, see which are available. |
| 170 | Returns a dictionary with of available codecs with codecs as keys and file extensions as values.""" |
| 171 | if not has_cv2: |
| 172 | return {} |
| 173 | all_codecs = {"mp4v": ".mp4", "X264": ".avi", "H264": ".mp4", "MP42": ".mp4", "MJPG": ".mjpeg", "DIVX": ".avi"} |
| 174 | codecs = {} |
| 175 | with SuppressStderr(): |
| 176 | with tempfile.TemporaryDirectory() as tmp_dir: |
| 177 | for codec, ext in all_codecs.items(): |
| 178 | writer = cv2.VideoWriter() |
| 179 | fname = os.path.join(tmp_dir, f"test{ext}") |
| 180 | fourcc = cv2.VideoWriter_fourcc(*codec) # type: ignore[attr-defined] |
| 181 | noviderr = writer.open(fname, fourcc, 1, (10, 10)) |
| 182 | if noviderr: |
| 183 | codecs[codec] = ext |
| 184 | writer.release() |
| 185 | return codecs |
| 186 | |
| 187 | def get_num_frames(self) -> int: |
| 188 | """ |