기계는 거짓말하지 않는다

Python 특정 프레임 수 이하 영상 데이터 삭제 본문

Python

Python 특정 프레임 수 이하 영상 데이터 삭제

KillinTime 2024. 6. 30. 15:52

아래 예시는 실제 프레임 수가 15개 이하인 MP4 파일을 삭제한다.

opencv-python 라이브러리가 필요하며, 각 비디오 파일의 총 프레임 수를 확인하여 삭제한다.

import os
import glob
import cv2
import time
import datetime

# 디렉터리 경로 변경 필요
directory = '/path/directory'
count = 0
delete_count = 0

start_time = time.time()
mp4_files = glob.glob(os.path.join(directory, '*.mp4'))

for file in mp4_files:
    try:
        count += 1
        # 비디오 파일 읽기
        video = cv2.VideoCapture(file)
        # 총 프레임 수 가져옴
        total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
        video.release()
        
        if total_frames <= 16:
            os.remove(file)
            print(f"{file} file deleted. frame count: {total_frames}")
            delete_count += 1
            print(f"delete count: {delete_count}")
        if count % 10 == 0:
            print(f"[{count} / {len(mp4_files)}] processing...")
    except Exception as e:
        print(f"{file} error: {e}")

total_time_str = str(datetime.timedelta(seconds=int(time.time() - start_time)))
print('\nTotal Time: {}'.format(total_time_str))
Comments