import subprocess
import sys
from pathlib import Path
import os
from time import sleep
from threading import Thread
import threading
import re

file="input.txt"

max_threads=int(input("max threads: "))
print(type(max_threads))
i=0
output_folder_name ="/bulk_convert_out/"



#droppedFile= ['test.mkv0out.mkv', "test.mkv1out.mkv","test.mkv2out.mkv","test.mkv3out.mkv","test.mkv4out.mkv","test.mkv5out.mkv"]
#get path of script location and remove the tail


#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:]
#arguments = droppedFile

#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)

def read_file(path=""):
    with open(file,'r+') as f: 
        path = f.readlines(1)
        all = f.readlines()
        f.seek(0)
        f.truncate()
        f.writelines(all)
        return path






#sleep(1)
filesize = os.path.getsize(file)
if filesize != 0:
    input_file = read_file()
    
    input_string = str(input_file[0])
    print(input_string)
    input_string = input_string.rstrip("\n")
    print(input_string)
    split_input = input_string.split(";", 1)
    print(split_input)
    basename=os.path.basename(input_string[0])
    print(input_string)

    infile = split_input[0]
    vapfile = split_input[1]
    args = 'E:/Projects/VapourSYnth/VapourSynth64Portable/VapourSynth64/vspipe.exe --arg source={infile} {vapfile} -c y4m -'.format(infile=infile, vapfile=vapfile, output_path=output_path, output_folder_name=output_folder_name,  basename=basename)
    print(args)
    conv = subprocess.Popen(args, creationflags=0x00004000, stdout=subprocess.PIPE)
    output = subprocess.check_output(('ffmpeg.exe -i pipe:0 -map 0:v -c:v libx265 -preset slow -crf 15.4 -x265-params "no-sao=1:bframes=8:psy-rd=2:psy-rdoq=1.5:aq-mode=3:ref=6:deblock=-1,-1" -pix_fmt yuv420p10le -f matroska "{output_path}{output_folder_name}test_list_{basename}"'.format(file=file, output_path=output_path, output_folder_name=output_folder_name,  basename=basename)), stdin=conv.stdout)

    #streeamdata = results.communicate()[0]
#    data = output.wait()

