add log file system.

This commit is contained in:
JunHo Kim committed 2021-07-30 19:05:07 +09:00
1 parent 9b0a69fb2b
commit 001a1d9544
1 file changed
+141 -66
+141 -66
View File
@@ -6,12 +6,14 @@ import re
import subprocess
import argparse
import copy
import datetime
import time
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--source_dir", default="./", help="Input source vid directory")
parser.add_argument("-i", "--ivtc_dir", default="./ivtc", help="Input ivtc vid directory")
parser.add_argument("-i", "--input_dir", default="./", help="Input source vid directory")
# parser.add_argument("-i", "--ivtc_dir", default="./0729_work/ivtc", help="Input ivtc vid directory")
parser.add_argument("-o", "--output_dir", default="./out", help="Output vid directory")
parser.add_argument("-m", "--manual_mode", default=False, action="store_true", help="Manual cropping without using log file")
parser.add_argument("-d", "--debug", default=False, action="store_true", help="for debugging. short output")
args = parser.parse_args()
@@ -66,58 +68,95 @@ def resizeCropInfo(key, cropPos, frame_width, frame_height):
return cropPos
def getTime():
dt_now = datetime.datetime.now()
dt_str = dt_now.strftime('[%Y-%m-%d %H:%M:%S]')
# print(dt_str)
return dt_str
def main():
vid_types = ('*.avi', '*.mov', '*.mp4', '*.mxf')
source_dir = args.source_dir
ivtc_dir = args.ivtc_dir
manual_mode = args.manual_mode
input_dir = args.input_dir
# ivtc_dir = args.ivtc_dir
output_dir = args.output_dir
source_list = []
ivtc_list = []
t = time.time
input_list = []
# ivtc_list = []
for types in vid_types :
source_list += sorted(glob.glob(os.path.join(source_dir, types)))
input_list += sorted(glob.glob(os.path.join(input_dir, types)))
ivtc_list += sorted(glob.glob(os.path.join(ivtc_dir, "*.mp4")))
"""
Compare "args.source_dir", "args.ivtc_dir"
check difference of two input, make ivtc version of source_dir videos.
----- NOT USED CODE [07/30] -----
"""
# ivtc_list += sorted(glob.glob(os.path.join(ivtc_dir, "*.mp4")))
if len(ivtc_list) > 0:
ivtc_input_list = []
for i, fpath in enumerate(source_list):
# if len(ivtc_list) > 0:
# ivtc_input_list = []
# for i, fpath in enumerate(source_list):
# vid_name = os.path.basename(fpath)[:-4]
# ivtc_name_list = [os.path.basename(x)[:-4] for x in ivtc_list]
# if not vid_name + "_ivtc" in ivtc_name_list :
# ivtc_input_list += glob.glob(fpath)
# print(f"[INFO]\t Seems \"{os.path.basename(fpath)}\" is stil telecine... add to ivtc processing list")
# else :
# ivtc_input_list = source_list
# for i, ivtc_input in enumerate(ivtc_input_list) :
# ivtc_input_num = len(ivtc_input_list)
# ivtc_input_name = os.path.basename(ivtc_input)
# ivtc_output_path = os.path.join(ivtc_dir, ivtc_input_name[:-4]) + "_ivtc.mp4"
# if not os.path.exists(ivtc_dir) :
# os.mkdir(ivtc_dir)
# print(f'[INFO]\t {ivtc_dir} created')
# print(f"[INFO]\t Work ivtc on \"{ivtc_input_name}\" ===> \"{ivtc_output_path}\" ... [{i + 1}/{ivtc_input_num}]")
# print("[INFO]\t Please wait...")
# start_time = time.time()
# subprocess.run(["ffmpeg", "-ss", "10", "-i", ivtc_input, "-vf", "fieldmatch=mode=pc_n:combmatch=full,bwdif=0:-1:1,decimate", "-c:v", "libx264", "-crf", "0",
# "-preset", "ultrafast","-loglevel", "quiet", "-y", ivtc_output_path])
# t = time.time() - start_time
# print("[INFO]\t Work done. [It takes %.2f seconds.]" % t)
# ivtc_dir = []
# ivtc_dir += sorted(glob.glob(os.path.join(ivtc_dir, "*.mp4")))
# target_ratio = 16 / 9
# print("[INFO]\t Target ratio : %.7f" %target_ratio)
# print("[INFO]\t Input ivtc dir :", ivtc_dir)
print("[INFO]\t Input dir :", input_dir)
for i, fpath in enumerate(input_list):
vid_name = os.path.basename(fpath)[:-4]
ivtc_name_list = [os.path.basename(x)[:-4] for x in ivtc_list]
vid_num = len(input_list)
logs = ''
if not vid_name + "_ivtc" in ivtc_name_list :
ivtc_input_list += glob.glob(fpath)
print(f"[INFO]\t Seems \"{os.path.basename(fpath)}\" is stil telecine... add to ivtc processing list")
if not manual_mode:
if not os.path.exists(f"./logs/{vid_name}.txt"):
print(f"[INFO]\t Can`t find log file. [./logs/{vid_name}.txt]")
else :
ivtc_input_list = source_list
print(f"[INFO]\t Loading log file... [./logs/{vid_name}.txt]")
f = open(f"./logs/{vid_name}.txt", "r")
logs = f.read()
for i, ivtc_input in enumerate(ivtc_input_list) :
ivtc_input_num = len(ivtc_input_list)
ivtc_input_name = os.path.basename(ivtc_input)
ivtc_output_path = os.path.join(ivtc_dir, ivtc_input_name[:-4]) + "_ivtc.mp4"
if not os.path.exists(ivtc_dir) :
os.mkdir(ivtc_dir)
print(f'[INFO]\t {ivtc_dir} created')
print(f"[INFO]\t Work ivtc on \"{ivtc_input_name}\" ===> \"{ivtc_output_path}\" ... [{i + 1}/{ivtc_input_num}]")
print("[INFO]\t Please wait...")
start_time = time.time()
subprocess.run(["ffmpeg", "-ss", "10", "-i", ivtc_input, "-vf", "fieldmatch=mode=pc_n:combmatch=full,bwdif=0:-1:1,decimate", "-c:v", "libx264", "-crf", "0",
"-preset", "ultrafast","-loglevel", "quiet", "-y", ivtc_output_path])
t = time.time() - start_time
print("[INFO]\t Work done. [It takes %.2f seconds.]" % t)
ivtc_list = []
ivtc_list += sorted(glob.glob(os.path.join(ivtc_dir, "*.mp4")))
target_ratio = 16 / 9
print("[INFO]\t Target ratio : %.7f" %target_ratio)
print("[INFO]\t Input ivtc dir :", ivtc_dir)
for i, fpath in enumerate(ivtc_list):
vid_name = os.path.basename(fpath)[:-4]
vid_num = len(ivtc_list)
if logs is not '':
# Delete "crop=" in list "crop"
allCrops = re.findall("crop=\S+", logs)
cropPos = allCrops[-1].split(":")
cropPos[0] = cropPos[0][5:]
print(f"[INFO]\t Crop area from log file: [w : {cropPos[0]}, h : {cropPos[1]}, x : {cropPos[2]}, y : {cropPos[3]}]")
cropPos = [int(x) for x in cropPos]
else :
print(f"[!!! WARNNIG !!!]\t Empty log file. [./logs/{vid_name}.txt]")
if (logs is '') or manual_mode:
# Korean title text bug fix (work in progress)
# vid_name = vid_name.encode('utf-8').decode('cp949')
@@ -257,33 +296,52 @@ def main():
# cropPos[3] = 38
# pad_arg = ""
dt_str = getTime()
if not os.path.exists("./logs"):
print(f"[INFO]\t Make log directory [./logs/{vid_name}.txt]")
os.makedirs("./logs")
if not os.path.exists(f"./logs/{vid_name}.txt"):
print(f"[INFO]\t Save log file in [./logs/{vid_name}.txt]")
f = open(f"./logs/{vid_name}.txt", 'w')
else :
print(f"[INFO]\t Add line in log file [./logs/{vid_name}.txt]")
f = open(f"./logs/{vid_name}.txt", 'a')
f.write(f"{dt_str}\tcrop={cropPos[0]}:{cropPos[1]}:{cropPos[2]}:{cropPos[3]}\n")
f.close
# ffmpeg filter args
crop_arg = f"crop={cropPos[0]}:{cropPos[1]}:{cropPos[2]}:{cropPos[3]},"
print(f"[INFO]\t [CropInfo] (W : {cropPos[0]}), (H : {cropPos[1]}), (X : {cropPos[2]}), (Y : {cropPos[3]})")
# 2021_07_28 : Delete padding argument
crop_arg = f"crop={cropPos[0]}:{cropPos[1]}:{cropPos[2]}:{cropPos[3]}"
crop_ratio = cropPos[0] / cropPos[1]
scale_arg = ""
pad_arg = ""
# Set padding(letter box) argument
# variable "offset" for 16:9 output display ratio
if target_ratio < crop_ratio:
offset = (9 * cropPos[0] - 16 * cropPos[1]) / 16
tmp_height = cropPos[1] + offset
tmp_height = int(tmp_height)
if (tmp_height % 4) != 0:
tmp_height -= (tmp_height % 4)
# scale_arg = f"scale=w={cropPos[0]}:h={tmp_height},"
pad_arg = f"pad={cropPos[0]}:{tmp_height}:(ow-iw)/2:(ih-oh)/2"
print(f"[INFO]\t [PadInfo] (W : {cropPos[0]}), (H : {tmp_height})")
else:
offset = (16 * cropPos[1] - 9 * cropPos[0]) / 9
tmp_width = cropPos[0] + offset
tmp_width = int(tmp_width)
if (tmp_width % 4) != 0:
tmp_width -= (tmp_width % 4)
# scale_arg = f"scale=w={tmp_width}:h={cropPos[1]},"
pad_arg = f"pad={tmp_width}:{cropPos[1]}:(ow-iw)/2:(ih-oh)/2"
print(f"[INFO]\t [PadInfo] (W : {tmp_width}), (H : {cropPos[1]})")
# if target_ratio < crop_ratio:
# offset = (9 * cropPos[0] - 16 * cropPos[1]) / 16
# tmp_height = cropPos[1] + offset
# tmp_height = int(tmp_height)
# if (tmp_height % 4) != 0:
# tmp_height -= (tmp_height % 4)
# # scale_arg = f"scale=w={cropPos[0]}:h={tmp_height},"
# pad_arg = f"pad={cropPos[0]}:{tmp_height}:(ow-iw)/2:(ih-oh)/2"
# print(f"[INFO]\t [PadInfo] (W : {cropPos[0]}), (H : {tmp_height})")
# else:
# offset = (16 * cropPos[1] - 9 * cropPos[0]) / 9
# tmp_width = cropPos[0] + offset
# tmp_width = int(tmp_width)
# if (tmp_width % 4) != 0:
# tmp_width -= (tmp_width % 4)
# # scale_arg = f"scale=w={tmp_width}:h={cropPos[1]},"
# pad_arg = f"pad={tmp_width}:{cropPos[1]}:(ow-iw)/2:(ih-oh)/2"
# print(f"[INFO]\t [PadInfo] (W : {tmp_width}), (H : {cropPos[1]})")
print("[INFO]\t Encoding \"%s\" ..." % vid_name)
@@ -293,17 +351,34 @@ def main():
output_path = os.path.join(output_dir, f"{vid_name}_box.mp4")
if args.debug:
# for Debug
# for Debug (short vid encoding)
if (i + 1) == vid_num :
subprocess.run(["ffmpeg", "-t", "15", "-ss", "30", "-i", fpath, "-vf", crop_arg + scale_arg + pad_arg, "-y",
"-pix_fmt", "yuv420p", "-c:v", "libx264", "-crf", "0", "-c:a", "mp3", "-b:a", "320k", "-preset", "medium", "-loglevel", "error", f"{output_path}"])
else :
p = subprocess.Popen(["ffmpeg", "-t", "15", "-ss", "30", "-i", fpath, "-vf", crop_arg + scale_arg + pad_arg, "-y",
"-pix_fmt", "yuv420p", "-c:v", "libx264", "-crf", "0", "-preset", "medium", "-loglevel", "error", f"{output_path}"])
else:
"-pix_fmt", "yuv420p", "-c:v", "libx264", "-crf", "0", "-c:a", "mp3", "-b:a", "320k", "-preset", "medium", "-loglevel", "error", f"{output_path}"])
elif (logs is '') or manual_mode:
# Manual Mode
# Hold process when encoding last video
if (i + 1) == vid_num :
subprocess.run(["ffmpeg", "-i", fpath, "-vf", crop_arg + scale_arg + pad_arg, "-y", "-pix_fmt",
"yuv420p", "-c:v", "libx264", "-crf", "0", "-c:a", "mp3", "-b:a", "320k", "-preset", "medium", "-loglevel", "error", f"{output_path}"])
else :
p = subprocess.Popen(["ffmpeg", "-i", fpath, "-vf", crop_arg + scale_arg + pad_arg, "-y", "-pix_fmt",
"yuv420p", "-c:v", "libx264", "-crf", "0", "-preset", "medium", "-loglevel", "error", f"{output_path}"])
# print("Encoding Done.")
"yuv420p", "-c:v", "libx264", "-crf", "0", "-c:a", "mp3", "-b:a", "320k", "-preset", "medium", "-loglevel", "error", f"{output_path}"])
else:
# Log file mode (encoding simultaneously 5 videos)
if (i % 4 == 0) and (i != 0):
subprocess.run(["ffmpeg", "-i", fpath, "-vf", crop_arg + scale_arg + pad_arg, "-y", "-pix_fmt",
"yuv420p", "-c:v", "libx264", "-crf", "0", "-c:a", "mp3", "-b:a", "320k", "-preset", "medium", "-loglevel", "error", f"{output_path}"])
else :
p = subprocess.Popen(["ffmpeg", "-i", fpath, "-vf", crop_arg + scale_arg + pad_arg, "-y", "-pix_fmt",
"yuv420p", "-c:v", "libx264", "-crf", "0", "-c:a", "mp3", "-b:a", "320k", "-preset", "medium", "-loglevel", "error", f"{output_path}"])
# debugging
# return
if __name__ == "__main__":
main()
print("[INFO]\t Encoding Done.")