Add cropInfo valid func
This commit is contained in:
1 parent
f3e4dadc39
commit
13c509c41e
1 file changed
+73
-57
+73
-57
@@ -8,99 +8,110 @@ import argparse
|
|||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("-i", "--input_dir", default="./")
|
parser.add_argument("-i", "--input_dir", default="./")
|
||||||
|
parser.add_argument("-s", "--scale", default="960:540")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
def resizeCropInfo(key, cropPos, frame_width, frame_height):
|
def resizeCropInfo(key, cropPos, frame_width, frame_height):
|
||||||
def _IsOutOfBound(w, h, x, y):
|
def _IsOutOfBound(_cropPos):
|
||||||
if (w + x) > frame_width or (h + y) > frame_height:
|
w, h, x, y = _cropPos
|
||||||
|
if (w + x) >= frame_width or (h + y) >= frame_height:
|
||||||
|
return True
|
||||||
|
if not all([a > 0 for a in _cropPos]):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# Left
|
w, h, x, y = cropPos
|
||||||
|
# Left (Expand width)
|
||||||
if key == 0x250000:
|
if key == 0x250000:
|
||||||
cropPos[2] -= 1
|
x -= 1
|
||||||
cropPos[0] += 2
|
w += 2
|
||||||
# Up
|
# Right (Shrink width)
|
||||||
elif key == 0x260000:
|
|
||||||
cropPos[3] -= 1
|
|
||||||
cropPos[1] += 2
|
|
||||||
# Right
|
|
||||||
elif key == 0x270000:
|
elif key == 0x270000:
|
||||||
cropPos[0] -= 2
|
x += 1
|
||||||
cropPos[2] += 1
|
w -= 2
|
||||||
# Down
|
# Up (Expand height)
|
||||||
|
elif key == 0x260000:
|
||||||
|
y -= 1
|
||||||
|
h += 2
|
||||||
|
# Down (Shrink height)
|
||||||
elif key == 0x280000:
|
elif key == 0x280000:
|
||||||
cropPos[1] -= 2
|
y += 1
|
||||||
cropPos[3] += 1
|
h -= 2
|
||||||
# Num 4
|
# Num 4 (Move left)
|
||||||
elif key == 52:
|
elif key == 52:
|
||||||
cropPos[2] -= 1
|
x -= 1
|
||||||
cropPos[0] -= 1
|
w -= 1
|
||||||
# Num 8
|
# Num 6 (Move right)
|
||||||
elif key == 56:
|
|
||||||
cropPos[3] -= 1
|
|
||||||
cropPos[1] -= 1
|
|
||||||
# Num 6
|
|
||||||
elif key == 54:
|
elif key == 54:
|
||||||
cropPos[0] += 1
|
w += 1
|
||||||
cropPos[2] += 1
|
x += 1
|
||||||
# Num 2
|
# Num 8 (Move upward)
|
||||||
|
elif key == 56:
|
||||||
|
h -= 1
|
||||||
|
y -= 1
|
||||||
|
# Num 2 (Move downward)
|
||||||
elif key == 50:
|
elif key == 50:
|
||||||
cropPos[1] += 1
|
h += 1
|
||||||
cropPos[3] += 1
|
y += 1
|
||||||
|
_cropPos = w, h, x, y
|
||||||
|
|
||||||
if cropPos[2] < 0 :
|
if not _IsOutOfBound(_cropPos):
|
||||||
cropPos[2] = 0
|
cropPos = _cropPos
|
||||||
if cropPos[3] < 0 :
|
|
||||||
cropPos[3] = 0
|
|
||||||
|
|
||||||
if key > 0:
|
print("Current Crop Info : ", end="")
|
||||||
print(cropPos)
|
print(cropPos, end="\r")
|
||||||
|
|
||||||
return cropPos
|
return cropPos
|
||||||
|
|
||||||
# cropdetect 에서 얻은 정보 => cv2로 그림 그리면서 confirm.
|
# TODO:
|
||||||
|
|
||||||
# confirm 안되면 w:h:x:y 를 콘솔로 정정할 수 있게 만듦 => 다시 confirm 시도
|
|
||||||
# confirm 되면 crop 진행 (resize는 일단 보류)
|
|
||||||
|
|
||||||
# cropinfo 로그로 쓸수 있게 txt로 저장
|
# cropinfo 로그로 쓸수 있게 txt로 저장
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
vid_paths = sorted(glob.glob(os.path.join(args.input_dir, '*.avi')))
|
vid_paths = sorted(glob.glob(os.path.join(args.input_dir, '*.avi')))
|
||||||
|
_scale = args.scale.split(":")
|
||||||
|
target_width, target_height = [int(x) for x in _scale]
|
||||||
|
|
||||||
for fpath in vid_paths :
|
if not (target_width > 0) and (target_height > 0) :
|
||||||
vid_name = os.path.basename(fpath)
|
print("Invalid target scale : " + target_width + target_height)
|
||||||
|
|
||||||
|
for fpath in vid_paths:
|
||||||
|
vid_name = os.path.basename(fpath)[:-4]
|
||||||
|
# Debugging
|
||||||
fpath = "01. Group S - I Swear MV.avi"
|
fpath = "01. Group S - I Swear MV.avi"
|
||||||
|
|
||||||
print ("File to detect crop: %s " % fpath)
|
print("File to detect crop: %s " % fpath)
|
||||||
p = subprocess.Popen(["ffmpeg", "-ss", "30", "-i", fpath, "-vf", "cropdetect=limit=38:reset=30", "-vframes", "1500", "-f", "rawvideo", "-y", "cropPos.mp4"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
p = subprocess.Popen(["ffmpeg", "-ss", "30", "-i", fpath, "-vf", "cropdetect=limit=38:reset=30",
|
||||||
|
"-vframes", "1500", "-f", "null", "out.null"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
infos = p.stderr.read().decode('utf-8')
|
infos = p.stderr.read().decode('utf-8')
|
||||||
# print (infos)
|
# print (infos)
|
||||||
allCrops = re.findall("crop=\S+", infos)
|
allCrops = re.findall("crop=\S+", infos)
|
||||||
# print ("Crop Infos : ")
|
# print ("Crop Infos : ")
|
||||||
# print (allCrops)
|
# print (allCrops)
|
||||||
mostCommonCrop = Counter(allCrops).most_common(1)
|
mostCommonCrop = Counter(allCrops).most_common(1)
|
||||||
print ("most common crop: %s" % mostCommonCrop)
|
print("most common detected crop info: %s" % mostCommonCrop)
|
||||||
global crop
|
global crop
|
||||||
crop = mostCommonCrop[0][0]
|
crop = mostCommonCrop[0][0]
|
||||||
|
|
||||||
cropPos = crop.split(":")
|
cropPos = crop.split(":")
|
||||||
# delete "crop="
|
# Delete "crop=" in list cropPos
|
||||||
cropPos[0] = cropPos[0][5:]
|
cropPos[0] = cropPos[0][5:]
|
||||||
cropPos = [int(x) for x in cropPos]
|
cropPos = [int(x) for x in cropPos]
|
||||||
|
|
||||||
|
# Capture vid, Get width, height
|
||||||
capture = cv2.VideoCapture(fpath)
|
capture = cv2.VideoCapture(fpath)
|
||||||
frame_width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
|
frame_width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
|
||||||
frame_height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
|
frame_height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
|
||||||
# play speed : 33ms / frame
|
|
||||||
|
# Play speed : 33ms / frame
|
||||||
while True:
|
while True:
|
||||||
key = cv2.waitKeyEx(33)
|
key = cv2.waitKeyEx(33)
|
||||||
# 27 == ESC
|
if key > 0 :
|
||||||
if key == 27:
|
# 27 == ESC
|
||||||
break
|
if key == 27:
|
||||||
|
break
|
||||||
|
|
||||||
resizeCropInfo(key, cropPos, frame_width, frame_height)
|
cropPos = resizeCropInfo(key, cropPos, frame_width, frame_height)
|
||||||
|
|
||||||
frame_pos = capture.get(cv2.CAP_PROP_POS_FRAMES)
|
frame_pos = capture.get(cv2.CAP_PROP_POS_FRAMES)
|
||||||
frame_count = capture.get(cv2.CAP_PROP_FRAME_COUNT)
|
frame_count = capture.get(cv2.CAP_PROP_FRAME_COUNT)
|
||||||
@@ -110,18 +121,17 @@ def main():
|
|||||||
|
|
||||||
ret, frame = capture.read()
|
ret, frame = capture.read()
|
||||||
|
|
||||||
cv2.rectangle(frame, (cropPos[2],cropPos[3]), (cropPos[2] + cropPos[0],cropPos[3] + cropPos[1]), (0, 0, 255), thickness=1, lineType=cv2.LINE_8)
|
cv2.rectangle(frame, (cropPos[2], cropPos[3]), (cropPos[2] + cropPos[0],
|
||||||
|
cropPos[3] + cropPos[1]), (0, 0, 255), thickness=1, lineType=cv2.LINE_8)
|
||||||
# cv2.rectangle(frame, (4,43), (717,431), (0, 0, 255), thickness=1, lineType=cv2.LINE_8)
|
# cv2.rectangle(frame, (4,43), (717,431), (0, 0, 255), thickness=1, lineType=cv2.LINE_8)
|
||||||
cv2.imshow(f'{vid_name}', frame)
|
cv2.imshow(f'{vid_name}', frame)
|
||||||
|
|
||||||
capture.release()
|
capture.release()
|
||||||
cv2.destroyAllWindows()
|
cv2.destroyAllWindows()
|
||||||
|
|
||||||
target_width = 960
|
|
||||||
target_height = 540
|
|
||||||
# ffmpeg filter args
|
# ffmpeg filter args
|
||||||
crop_arg = f"crop={cropPos[0]}:{cropPos[1]}:{cropPos[2]}:{cropPos[3]},"
|
crop_arg = f"crop={cropPos[0]}:{cropPos[1]}:{cropPos[2]}:{cropPos[3]},"
|
||||||
print (f"crop={cropPos[0]}:{cropPos[1]}:{cropPos[2]}:{cropPos[3]},")
|
print(f"crop={cropPos[0]}:{cropPos[1]}:{cropPos[2]}:{cropPos[3]},")
|
||||||
|
|
||||||
if (target_height / target_height) >= (cropPos[0] / cropPos[1]):
|
if (target_height / target_height) >= (cropPos[0] / cropPos[1]):
|
||||||
tmp_width = target_height * (cropPos[0] / cropPos[1])
|
tmp_width = target_height * (cropPos[0] / cropPos[1])
|
||||||
@@ -132,11 +142,17 @@ def main():
|
|||||||
|
|
||||||
pad_arg = f"pad={target_width}:{target_height}:(ow-iw)/2:(ih-oh)/2,"
|
pad_arg = f"pad={target_width}:{target_height}:(ow-iw)/2:(ih-oh)/2,"
|
||||||
|
|
||||||
print("Encoding \"%s\"" %vid_name)
|
print("Encoding \"%s\" ..." % vid_name)
|
||||||
if False:
|
if False:
|
||||||
p = subprocess.Popen(["ffmpeg", "-i", fpath, "-vf", crop_arg + scale_arg + pad_arg + "setsar=sar=1", "-y", "-pix_fmt", "yuv420p", "-c:v", "libx264", "-crf", "15", "-preset", "medium","-loglevel", "error", f"_{vid_name}_cropped.mp4"])
|
p = subprocess.Popen(["ffmpeg", "-i", fpath, "-vf", crop_arg + scale_arg + pad_arg + "setsar=sar=1", "-y", "-pix_fmt",
|
||||||
|
"yuv420p", "-c:v", "libx264", "-crf", "15", "-preset", "medium", "-loglevel", "error", f"_{vid_name}_cropped.mp4"])
|
||||||
else:
|
else:
|
||||||
p = subprocess.Popen(["ffmpeg", "-t", "15", "-ss", "30", "-i", fpath, "-vf", crop_arg + scale_arg + pad_arg + "setsar=sar=1", "-y", "-pix_fmt", "yuv420p", "-c:v", "libx264", "-crf", "15", "-preset", "medium","-loglevel", "error", f"_{vid_name}_cropped.mp4"])
|
# for Debug
|
||||||
|
p = subprocess.Popen(["ffmpeg", "-t", "15", "-ss", "30", "-i", fpath, "-vf", crop_arg + scale_arg + pad_arg + "setsar=sar=1", "-y",
|
||||||
|
"-pix_fmt", "yuv420p", "-c:v", "libx264", "-crf", "15", "-preset", "medium", "-loglevel", "error", f"_{vid_name}_cropped.mp4"])
|
||||||
|
print("Encoding Done.")
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in new issue
Block a user