fix output ratio bug

This commit is contained in:
JunHo Kim committed 2021-07-05 11:15:39 +09:00
1 parent 3011f51ce7
commit dd59f9818b
1 file changed
+14 -10
+14 -10
View File
@@ -14,9 +14,11 @@ args = parser.parse_args()
def resizeCropInfo(key, cropPos, frame_width, frame_height): def resizeCropInfo(key, cropPos, frame_width, frame_height):
def _IsOutOfBound(_cropPos): def _IsOutOfBound(_cropPos):
w, h, x, y = _cropPos w, h, x, y = _cropPos
if (w + x) >= frame_width or (h + y) >= frame_height: if (w + x) > frame_width or (h + y) > frame_height:
return True return True
if not all([a > 0 for a in _cropPos]): if not all([a >= 0 for a in [x, y]]):
return True
if not all([a >= 0 for a in [w, h]]):
return True return True
w, h, x, y = cropPos w, h, x, y = cropPos
@@ -85,11 +87,11 @@ def main():
for fpath in vid_paths: for fpath in vid_paths:
vid_name = os.path.basename(fpath)[:-4] vid_name = os.path.basename(fpath)[:-4]
# Debugging # Debugging
fpath = "01. Group S - I Swear MV.avi" fpath = "01. In the sky - 블랙비트.avi"
print("File to detect crop: ", fpath) print("File to detect crop: ", fpath)
p = subprocess.Popen(["ffmpeg", "-ss", "30", "-i", fpath, "-vf", "cropdetect=limit=38:reset=30", p = subprocess.Popen(["ffmpeg", "-ss", "30", "-i", fpath, "-vf", "cropdetect=limit=38",
"-vframes", "1500", "-f", "null", "out.null"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) "-vframes", "3000", "-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)
@@ -138,14 +140,16 @@ def main():
# 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]},")
# Debugging
print(f"[debug] 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])
scale_arg = f"scale=w={tmp_width}:h={target_height},"
else:
tmp_height = target_width * (cropPos[1] / cropPos[0]) tmp_height = target_width * (cropPos[1] / cropPos[0])
scale_arg = f"scale=w={target_width}:h={tmp_height}," scale_arg = f"scale=w={target_width}:h={tmp_height},"
else:
tmp_width = target_height * (cropPos[0] / cropPos[1])
scale_arg = f"scale=w={tmp_width}:h={target_height},"
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,"
@@ -159,8 +163,8 @@ def main():
"-pix_fmt", "yuv420p", "-c:v", "libx264", "-crf", "15", "-preset", "medium", "-loglevel", "error", f"_{vid_name}_cropped.mp4"]) "-pix_fmt", "yuv420p", "-c:v", "libx264", "-crf", "15", "-preset", "medium", "-loglevel", "error", f"_{vid_name}_cropped.mp4"])
print("Encoding Done.") print("Encoding Done.")
# debugging
return return
if __name__ == "__main__": if __name__ == "__main__":
main() main()