hoony's web study

728x90
반응형

동영상 파일에서 metadata를 뽑아오는 python source를 공유해드립니다.

해당 소스에 대해서는 일단 설명 보다는 소스를 공유해 드리면 아마 편하실 것 같아서요. 

import os, cv2, datetime
filename = "폴더명"

# for root, dirs, files in os.walk(filename):
#      for fname in files:
#           if ('.mp4' in fname):
#             full_fname = os.path.join(fname)
#             print(full_fname)

for x in os.listdir(filename):
      if ('.MOV' in x or '.mp4' in x or '.mov' in x):
        print('-----input : ' + (x) + '-----')
        vcap = cv2.VideoCapture(filename + (x)) # 0=camera
        if vcap.isOpened(): 
            width  = vcap.get(cv2.CAP_PROP_FRAME_WIDTH)   # float
            height = vcap.get(cv2.CAP_PROP_FRAME_HEIGHT)  # float
            # print(cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT) # 3, 4

        # or
            width  = str(vcap.get(3)) # float
            height = str(vcap.get(4)) # float

            WH = "Width : " + width +" "+ "height : " +height
            # print('width, height:', width, height)

            fps = vcap.get(cv2.CAP_PROP_FPS)
            # print('fps:', fps)  # float
            # print(cv2.CAP_PROP_FPS) # 5

            fps1 = vcap.get(cv2.CAP_PROP_FRAME_COUNT)
            # print('frames count:', fps)  # float
        
            length = round(fps1/fps)
            Second1 = "Length : " + str(datetime.timedelta(seconds=length))
            # print(Second1)

            # FC = str(cv2.CAP_PROP_FRAME_COUNT) # 7

            with open(filename + 'MOVIEMeta.txt', "a") as f:
             f.write( x +" "+ WH +" "+ Second1 + '\n' )
             f.close()

동영상 metadata 추출

filename 은 폴더를 생각하시면 되구요.
아래에 Moviemeta.txt는 불러온 정보를 text 파일로 저장하는 것입니다.

필요하신 분은 사용해주세요.
저도 이거 개발한다고 너무 고생을 해서 공유해드려요. 

 

728x90
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band
loading