(mp4_p, n_frames=None, fps=None, return_frame_indices=False, random_sample=False)
| 134 | |
| 135 | |
| 136 | def sample_mp4_frames(mp4_p, n_frames=None, fps=None, return_frame_indices=False, random_sample=False): |
| 137 | if isinstance(mp4_p, str): |
| 138 | vr = decord.VideoReader(mp4_p, num_threads=1) |
| 139 | elif isinstance(mp4_p, decord.video_reader.VideoReader): |
| 140 | vr = mp4_p |
| 141 | video_fps = vr.get_avg_fps() # 获取视频的帧率 |
| 142 | video_duration = len(vr) / video_fps |
| 143 | if n_frames is not None: |
| 144 | if random_sample: |
| 145 | frame_indices = sorted(random.sample(range(len(vr)), n_frames)) |
| 146 | else: |
| 147 | frame_indices = np.linspace(0, len(vr)-1, n_frames, dtype=int).tolist() |
| 148 | else: |
| 149 | frame_indices = [int(i) for i in np.arange(0, len(vr)-1, video_fps/fps)] |
| 150 | frames = vr.get_batch(frame_indices).asnumpy() # 转换为 numpy 数组 |
| 151 | frames = [Image.fromarray(frame).convert("RGB") for frame in frames] |
| 152 | if not return_frame_indices: |
| 153 | return frames, video_duration |
| 154 | else: |
| 155 | return frames, video_duration, frame_indices |
| 156 | |
| 157 | |
| 158 | def sample_mp4_frames_by_indices(mp4_p, frame_indices: list): |
nothing calls this directly
no outgoing calls
no test coverage detected