Detecting and removing corrupt video files from the dataset

This commit is contained in:
Vedant Dave 2023-03-25 17:36:58 +01:00
parent 25c2853ba6
commit 4515c6a6b7

View File

@ -216,3 +216,41 @@ def video_from_array(arr, high_noise, filename):
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR) frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
out.write(frame) out.write(frame)
out.release() out.release()
class CorruptVideos:
def __init__(self, dir_path):
self.dir_path = dir_path
def _is_video_corrupt(self,filepath):
"""
Check if a video file is corrupt.
Args:
filepath (str): Path to the video file.
Returns:
bool: True if the video is corrupt, False otherwise.
"""
# Open the video file
cap = cv2.VideoCapture(filepath)
if not cap.isOpened():
return True
ret, frame = cap.read()
if not ret:
return True
cap.release()
return False
def _delete_corrupt_video(self, filepath):
os.remove(filepath)
def is_video_corrupt(self, delete=False):
for filename in os.listdir(self.dir_path):
filepath = os.path.join(self.dir_path, filename)
if filepath.endswith(".mp4"):
if self._is_video_corrupt(filepath):
print(f"{filepath} is corrupt.")
if delete:
self._delete_corrupt_video(filepath)
print(f"Deleted {filepath}")