| 43 | |
| 44 | |
| 45 | def process_video( |
| 46 | clip_path: os.PathLike, target_dir: os.PathLike, force_fps: int=None, target_size: int=None, |
| 47 | broken_clips_dir: os.PathLike=None, compute_fps_only: bool=False) -> int: |
| 48 | |
| 49 | clip_name = os.path.basename(clip_path) |
| 50 | clip_name = clip_name[:clip_name.rfind('.')] |
| 51 | |
| 52 | try: |
| 53 | clip = VideoFileClip(clip_path) |
| 54 | except KeyboardInterrupt: |
| 55 | raise |
| 56 | except Exception as e: |
| 57 | print(f'Coudnt process clip: {clip_path}') |
| 58 | if not broken_clips_dir is None: |
| 59 | Path(os.path.join(broken_clips_dir, clip_name)).touch() |
| 60 | return 0 |
| 61 | |
| 62 | if compute_fps_only: |
| 63 | return clip.fps |
| 64 | |
| 65 | fps = clip.fps if force_fps is None else force_fps |
| 66 | clip_target_dir = os.path.join(target_dir, clip_name) |
| 67 | clip_target_dir = clip_target_dir.replace('#', '_') |
| 68 | os.makedirs(clip_target_dir, exist_ok=True) |
| 69 | |
| 70 | frame_idx = 0 |
| 71 | for frame in clip.iter_frames(fps=fps): |
| 72 | frame = Image.fromarray(frame) |
| 73 | h, w = frame.size[0], frame.size[1] |
| 74 | min_size = min(h, w) |
| 75 | if not target_size is None: |
| 76 | # frame = TVF.resize(frame, size=target_size, interpolation=Image.LANCZOS) |
| 77 | # frame = TVF.center_crop(frame, output_size=(target_size, target_size)) |
| 78 | frame = TVF.center_crop(frame, output_size=(min_size, min_size)) |
| 79 | frame = TVF.resize(frame, size=target_size, interpolation=Image.LANCZOS) |
| 80 | frame.save(os.path.join(clip_target_dir, f'{frame_idx:06d}.jpg'), q=95) |
| 81 | frame_idx += 1 |
| 82 | |
| 83 | return clip.fps |
| 84 | |
| 85 | |
| 86 | def listdir_full_paths(d) -> List[os.PathLike]: |