145 lines
4.5 KiB
Python
145 lines
4.5 KiB
Python
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
import os
|
|
from time import sleep
|
|
from threading import Thread
|
|
import threading
|
|
import re
|
|
|
|
|
|
|
|
max_threads=4
|
|
i=0
|
|
output_folder_name ="/bulk_convert_out/"
|
|
|
|
#droppedFile = sys.argv[1]
|
|
#droppedName = Path(droppedFile).name
|
|
#originalfiles = sys.orig_argv[2:]
|
|
|
|
#long path shenanigans
|
|
#joined = (' '.join(originalfiles))
|
|
#re.sub (' +', ' ', joined)
|
|
#listed = re.split(r'(.+?mkv)', joined)
|
|
#listed = list(filter(None, listed))
|
|
#listed = [i.lstrip() for i in listed]
|
|
|
|
listed= ['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
|
|
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)
|
|
|
|
org_count = len(listed)
|
|
finished_count = int()
|
|
|
|
def subproc_ffmpeg(file="",crf_value="", output_path="", output_folder_name=""):
|
|
basename = os.path.basename(file)
|
|
print(basename)
|
|
print(file)
|
|
print("printing output path: ", output_path)
|
|
print("printing folder_name: ", output_folder_name)
|
|
return 'ffmpeg.exe -i "{file}" -map 0:v -c:v libx265 -preset slow -crf "{crf_value}" -x265-params "no-sao=1:bframes=8:psy-rd=2:psy-rdoq=1.5:aq-mode=3:ref=6" -pix_fmt yuv420p10le -map 0:a -c:a libopus -b:a 192k -f matroska "{output_path}{output_folder_name}crf-{crf_value}_{basename}"'.format(file=file, crf_value=crf_value, output_path=output_path, output_folder_name=output_folder_name, basename=basename)
|
|
|
|
|
|
|
|
print("CRF granuality? 1 or 0.1: ")
|
|
while True:
|
|
crf_gran=float(input("> "))
|
|
if crf_gran == 1:
|
|
large_gran = True
|
|
print("Stepping in whole values")
|
|
break
|
|
if crf_gran == 0.1:
|
|
large_gran = False
|
|
print("stepping in decimal values")
|
|
break
|
|
print("Invalid choice, try again")
|
|
|
|
|
|
|
|
|
|
crf_range = [int(z) for z in input ("CRF range to test? Seperate with dash (eg: 15-17): ").split('-')]
|
|
|
|
|
|
class CustomThread(Thread):
|
|
# constructor
|
|
def __init__(self):
|
|
|
|
# execute the base constructor
|
|
Thread.__init__(self)
|
|
# set a default value
|
|
|
|
|
|
def run(self):
|
|
|
|
#sleep(1)
|
|
global i
|
|
global finished_count
|
|
global large_gran
|
|
global crf_range
|
|
file=listed.pop(0)
|
|
basename = os.path.basename(file)
|
|
print(file)
|
|
if large_gran == True:
|
|
while crf_range[0] <= crf_range[1]:
|
|
crf_value = crf_range[0]
|
|
for file in listed:
|
|
|
|
subprocess.run(subproc_ffmpeg(file, crf_value, output_path, output_folder_name))
|
|
crf_range[0] += 1
|
|
if large_gran == False:
|
|
while crf_range[0] < crf_range[1]:
|
|
crf_value = round(crf_range[0], 1)
|
|
for file in listed:
|
|
|
|
subprocess.run(subproc_ffmpeg(file, crf_value, output_path, output_folder_name))
|
|
crf_range[0] += 0.1
|
|
|
|
|
|
#args = 'ffmpeg.exe -i "{file}" -fps_mode passthrough -map_metadata -1 -map 0:v:0 -c:v libx265 -preset slow -crf 14.5 -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 -map 0:a -c:a libopus -ac 6 -b:a 576k -map 0:s -c:s copy -f matroska "{output_path}{output_folder_name}{basename}"'.format(file=file, output_path=output_path, output_folder_name=output_folder_name, basename=basename)
|
|
# print(args)
|
|
#results = subprocess.Popen(args, stdout=subprocess.PIPE)
|
|
#streeamdata = results.communicate()[0]
|
|
#data = results.wait()
|
|
|
|
|
|
if data == 0:
|
|
i=i-1
|
|
finished_count=finished_count+1
|
|
|
|
|
|
|
|
while listed != False:
|
|
if i < max_threads:
|
|
global self
|
|
|
|
if bool(listed) != False:
|
|
t1 = CustomThread()
|
|
t1.start()
|
|
if bool(listed) == False:
|
|
while bool(threading.active_count() > 1):
|
|
print("Finished",finished_count,"out of", org_count," ")
|
|
sleep(1)
|
|
t1.join()
|
|
print("Completed",finished_count,"out of",org_count, "inputs Successfully")
|
|
input("waiting")
|
|
exit()
|
|
|
|
print("incrementing i in loop")
|
|
i=i+1
|
|
|
|
else:
|
|
print("Finished ",finished_count,"out of ", org_count)
|
|
sleep(1)
|
|
print("continuing ")
|