Add multiple of 4 validation

This commit is contained in:
JunHo Kim committed 2021-07-14 16:05:08 +09:00
1 parent 3cb80f9d04
commit c48ae938b6
1 file changed
+32 -15
+32 -15
View File
@@ -9,6 +9,7 @@ import re
import subprocess import subprocess
import argparse import argparse
import copy import copy
import base64
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input_dir", default="./", help="Input vid directory") parser.add_argument("-i", "--input_dir", default="./", help="Input vid directory")
@@ -29,20 +30,20 @@ def resizeCropInfo(key, cropPos, frame_width, frame_height):
w, h, x, y = cropPos w, h, x, y = cropPos
# Left (Expand width) # Left (Expand width)
if key == ord('j') or key == ord('J'): if key == ord('j') or key == ord('J'):
x -= 1 x -= 2
w += 2 w += 4
# Right (Shrink width) # Right (Shrink width)
elif key == ord('l') or key == ord('L'): elif key == ord('l') or key == ord('L'):
x += 1 x += 2
w -= 2 w -= 4
# Up (Expand height) # Up (Expand height)
elif key == ord('i') or key == ord('I'): elif key == ord('i') or key == ord('I'):
y -= 1 y -= 2
h += 2 h += 4
# Down (Shrink height) # Down (Shrink height)
elif key == ord('k') or key == ord('K'): elif key == ord('k') or key == ord('K'):
y += 1 y += 2
h -= 2 h -= 4
# Num 4 (Move left) # Num 4 (Move left)
elif key == ord('a') or key == ord('A'): elif key == ord('a') or key == ord('A'):
# w -= 1 # w -= 1
@@ -84,6 +85,7 @@ def main():
# fpath = "01. TRAX - PARADOX MV.avi" # fpath = "01. TRAX - PARADOX MV.avi"
vid_name = os.path.basename(fpath)[:-4] vid_name = os.path.basename(fpath)[:-4]
# vid_name = vid_name.encode('utf-8').decode('cp949')
print("File to detect crop: ", fpath) print("File to detect crop: ", fpath)
@@ -126,14 +128,26 @@ def main():
else: else:
cropPos[1] += 1 cropPos[1] += 1
print("Target Ratio : %.5f" %target_ratio) # Crop Area Multiple of 4 check
if (cropPos[0] % 4) != 0 :
if (cropPos[0] + 2) >= frame_width:
cropPos[0] -= 2
else:
cropPos[0] += 2
if (cropPos[1] % 4) != 0 :
if (cropPos[1] + 2) >= frame_width:
cropPos[1] -= 2
else:
cropPos[1] += 2
print("Target Ratio : %.7f" %target_ratio)
print("Input dir :", args.input_dir) print("Input dir :", args.input_dir)
key = None key = None
isPaused = False isPaused = False
pausedFrame = None pausedFrame = None
frame = None frame = None
cv2.namedWindow(f'{vid_name}') cv2.namedWindow(f'{vid_name}', cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO)
while True: while True:
key = cv2.waitKeyEx(10) key = cv2.waitKeyEx(10)
@@ -165,6 +179,9 @@ def main():
elif key == ord('q') or key == ord('Q'): elif key == ord('q') or key == ord('Q'):
if not isPaused: if not isPaused:
capture.set(cv2.CAP_PROP_POS_FRAMES, frame_pos - (fps * 10)) capture.set(cv2.CAP_PROP_POS_FRAMES, frame_pos - (fps * 10))
# Skip this Vid
elif key == ord('n') or key == ord('N'):
break;
else: else:
# Crop Area control # Crop Area control
cropPos = resizeCropInfo(key, cropPos, frame_width, frame_height) cropPos = resizeCropInfo(key, cropPos, frame_width, frame_height)
@@ -208,16 +225,16 @@ def main():
# # Set padding argument # # Set padding argument
if target_ratio < crop_ratio: if target_ratio < crop_ratio:
offset = (9 * cropPos[0] - 16 * cropPos[1]) / 16 offset = (9 * cropPos[0] - 16 * cropPos[1]) / 16
if int(offset) % 2 != 0: if (int(offset) % 4) != 0:
offset += 1 offset += (int(offset) % 4)
tmp_height = cropPos[1] + offset tmp_height = cropPos[1] + offset
# scale_arg = f"scale=w={cropPos[0]}:h={tmp_height}," # scale_arg = f"scale=w={cropPos[0]}:h={tmp_height},"
pad_arg = f"pad={cropPos[0]}:{tmp_height}:(ow-iw)/2:(ih-oh)/2," pad_arg = f"pad={cropPos[0]}:{tmp_height}:(ow-iw)/2:(ih-oh)/2,"
print(f"[PadInfo] (W : {cropPos[0]}), (H : {tmp_height})") print(f"[PadInfo] (W : {cropPos[0]}), (H : {tmp_height})")
else: else:
offset = (16 * cropPos[1] - 9 * cropPos[0]) / 9 offset = (16 * cropPos[1] - 9 * cropPos[0]) / 9
if int(offset) % 2 != 0: if (int(offset) % 4) != 0:
offset += 1 offset += (int(offset) % 4)
tmp_width = cropPos[0] + offset tmp_width = cropPos[0] + offset
# scale_arg = f"scale=w={tmp_width}:h={cropPos[1]}," # scale_arg = f"scale=w={tmp_width}:h={cropPos[1]},"
pad_arg = f"pad={tmp_width}:{cropPos[1]}:(ow-iw)/2:(ih-oh)/2," pad_arg = f"pad={tmp_width}:{cropPos[1]}:(ow-iw)/2:(ih-oh)/2,"
@@ -238,7 +255,7 @@ def main():
p = subprocess.Popen(["ffmpeg", "-i", fpath, "-vf", crop_arg + scale_arg + pad_arg + "setsar=sar=1", "-y", "-pix_fmt", p = subprocess.Popen(["ffmpeg", "-i", fpath, "-vf", crop_arg + scale_arg + pad_arg + "setsar=sar=1", "-y", "-pix_fmt",
"yuv420p", "-c:v", "libx264", "-crf", "0", "-preset", "medium", "-loglevel", "error", f"{output_path}"]) "yuv420p", "-c:v", "libx264", "-crf", "0", "-preset", "medium", "-loglevel", "error", f"{output_path}"])
print("Encoding Done.") # print("Encoding Done.")
# debugging # debugging
# return # return