import re
import subprocess
import sys
from pathlib import Path
import os
output_folder_name = "/screenshots/"
droppedFile = sys.argv[1]
droppedName = Path(droppedFile).name

#droppedFile = ['test_source.mkv']

#get path of script location and remove the tail
head_tail = os.path.split(sys.argv[0])
output_path = head_tail[0]

#skip first part of sys.argv since it points to script location
arguments = sys.argv[1:]

#check if output folder exists, otherwise create it
isExist = os.path.exists(output_path+output_folder_name)
if not isExist:
    os.makedirs(output_path+output_folder_name)

number_of_ss=int(input("Number of ss: "))
print(number_of_ss)

for i, file in enumerate(arguments):
   
    arg_list = 'ffprobe.exe -show_entries format=duration -i "{file}"'.format(file=file)
    print(arg_list)
    cmd = subprocess.Popen(arg_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='UTF-8')
        
    subprocess_std = cmd.communicate()[0]
   
    print("stdout printing: " , subprocess_std)
    video_duration = [int(s) for s in re.findall(r"[0-9]+", subprocess_std)][0]
    print("video_duration: ", video_duration, "s")
    duration_percent = video_duration / 100
    print(duration_percent)

    time = duration_percent * 15

    skip = ((duration_percent * 90) - (duration_percent* 15)) / (number_of_ss -1)
    for i in range(0, number_of_ss):
        
        basename = os.path.basename(file)
        args = 'ffmpeg.exe -ss {time} -i "{file}" -frames:v 1 -vf scale=3840:-1 -q:v 1 "{output_path}{output_folder_name}{basename}{i}.jpeg"'.format(time=skip*i+time, file=file, output_path=output_path, output_folder_name=output_folder_name, basename=basename, i=i)
        print(args)
        run = subprocess.Popen(args)
       
        



    print('\n')
    

input("press enter to exit") 
