# -*- coding: utf-8 -*-

import cv2
import os
import pandas as pd
import argparse

def video_frames(videospath, videoframespath):
    for file in os.listdir(videospath):
        count = 0
        file_mod = file[:-4]
        if os.path.exists(f'{videoframespath}/{file_mod}'):
          continue
          print('pass')
        print(file_mod)
        cap = cv2.VideoCapture(f'{videospath}/{file}') 
        sec = 0
        frameRate = 1
        #ret = True
        if not os.path.exists(f'{videoframespath}/{file_mod}'):
            os.makedirs(f'{videoframespath}/{file_mod}')
        while(cap.isOpened()):
            cap.set(cv2.CAP_PROP_POS_MSEC,sec*1000)
            ret, frame = cap.read()
            if not ret: 
                break
            output_path = os.path.join(videoframespath, file_mod)
            name = f'{output_path}/frame%d.jpg' % count
            count = count + 1
            cv2.imwrite(name, frame)
            sec = sec + frameRate
            sec = round(sec, 2)
        cap.release()


def main():
    parser = argparse.ArgumentParser(description = 'JOC Encoded Tensors')
        
    parser.add_argument('--input', '-i', required = True, help = 'videos_path')
    parser.add_argument('--output', '-o', required = True, help = 'videoframes_path')
    
    args = parser.parse_args()
    
    try:
        video_frames(args.input1, args.output) 
        print('success')
    except Exception as e:
        print(f'Error occured: {e}')

if __name__ == "__main__":
    main()


