add ivtc process. refine log, comment

This commit is contained in:
JunHo Kim committed 2021-07-26 16:44:27 +09:00
1 parent 2553a770f8
commit 2465f21471
2 files changed
+187 -133

No files matched your search

+25 -8
View File
@@ -3,10 +3,27 @@ Convenient Vid cropping tool
## Info ## Info
- Window 용 Script - Window 용 Script
- 재생은 x3 배속 - 재생은 x3 배속으로 고정
## ivtc Code
< 하나의 파일을 변환하는 코드 >
ffmpeg -i in.mp4 -vf fieldmatch=mode=pc_n:combmatch=full,bwdif=0:-1:1,decimate -c:v libx264 -crf 0 -preset ultrafast out.mp4
< 폴더 내에 있는 모든 파일을 변환하는 코드 >
for %a in ("*.*") do ffmpeg -i "%a" -vf fieldmatch=mode=pc_n:combmatch=full,bwdif=0:-1:1,decimate -c:v libx264 -crf 0 -preset ultrafast ".\out\%~na_ivtc.mp4"
## ToDo ## ToDo
1. 우선 IVTC 파일이 1:1 매칭 되는 지 확인
2. 안되면 안된 파일들 목록 만들기
3. 만든 목록 파일들 IVTC 처리
4. 처리하는 동안 진행상황 Output
- **Concealing bitstream error** 해결 - **Concealing bitstream error** 해결
- ~~타이틀 창 등에 현재 재생 퍼센트 / 프레임 수 Display~~
- 타이틀 한글 깨지는 것
- 한 프레임씩 이동 하는 기능
- 처음에 목록 보여주고 시작? (필요한지?)
- Log Ouput 필요 - Log Ouput 필요
### 포함 해야 할 내용 ### 포함 해야 할 내용
- 날짜, 시간 정보 - 날짜, 시간 정보
@@ -24,10 +41,10 @@ Convenient Vid cropping tool
- 영상 파일 명.txt - 영상 파일 명.txt
- 같은 파일에 매 편집 시 누적되는 식 - 같은 파일에 매 편집 시 누적되는 식
- 각 편집 로그의 구별은 편집 시간정보별로. - 각 편집 로그의 구별은 편집 시간정보별로.
### 구조적 문제 ### 구조적 문제
- Subprocess 기반으로 FFMPEG 동작 - Subprocess 기반으로 FFMPEG 동작
- 현재 Encoding 중인 영상이 어느정도 진행 되었는 지 알 수 없음 - 현재 Encoding 중인 영상이 어느정도 진행 되었는 지 알 수 없음
- 전체 프로그램이 종료되기 전까지는 파일이 생성되지 않음 (subprocess가 계속 돌고있는 듯 함) - 전체 프로그램이 종료되기 전까지는 파일이 생성되지 않음 (subprocess가 계속 돌고있는 듯 함)
- 엔터를 누를 때 마다 다음 영상으로 넘어가기 때문에, 이전 영상의 진행도를 알기 어려울 듯 하다. - 엔터를 누를 때 마다 다음 영상으로 넘어가기 때문에, 이전 영상의 진행도를 알기 어려울 듯 하다.
- _생성되는 각 프로세스를 다른 변수명에 Mapping 하여 Log 관리를 각각 해야 하나?_ - _생성되는 각 프로세스를 다른 변수명에 Mapping 하여 Log 관리를 각각 해야 하나?_
- Encoding이 종료되면 프로세스를 종료시키는 것 같은 방법이 필요할듯.. - Encoding이 종료되면 프로세스를 종료시키는 것 같은 방법이 필요할듯..
+162 -125
View File
@@ -1,6 +1,3 @@
import io
from contextlib import redirect_stderr
import os import os
from collections import Counter from collections import Counter
import cv2 import cv2
@@ -9,14 +6,16 @@ import re
import subprocess import subprocess
import argparse import argparse
import copy import copy
import base64 import time
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input_dir", default="./", help="Input vid directory") 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("-o", "--output_dir", default="./out", help="Output vid directory") parser.add_argument("-o", "--output_dir", default="./out", help="Output vid directory")
parser.add_argument("-d", "--debug", default=False, action="store_true", help="for debugging. short output") parser.add_argument("-d", "--debug", default=False, action="store_true", help="for debugging. short output")
args = parser.parse_args() args = parser.parse_args()
# Adjust crop info in display layout.
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
@@ -69,27 +68,62 @@ def resizeCropInfo(key, cropPos, frame_width, frame_height):
def main(): def main():
vid_types = ('*.avi', '*.mov', '*.mp4', '*.mxf') vid_types = ('*.avi', '*.mov', '*.mp4', '*.mxf')
vid_grapped = [] source_dir = args.source_dir
ivtc_dir = args.ivtc_dir
output_dir = args.output_dir
source_list = []
ivtc_list = []
for types in vid_types : for types in vid_types :
vid_grapped += sorted(glob.glob(os.path.join(args.input_dir, types))) source_list += sorted(glob.glob(os.path.join(source_dir, types)))
#-------------------------------------------------------- ivtc_list += sorted(glob.glob(os.path.join(ivtc_dir, "*.mp4")))
f = io.StringIO()
#--------------------------------------------------------
for i, fpath in enumerate(vid_grapped): if len(ivtc_list) > 0:
# debugging ivtc_input_list = []
# fpath = "01. TRAX - PARADOX MV.avi" 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 i >= 68 : if not vid_name + "_ivtc" in ivtc_name_list :
return 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_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_name = os.path.basename(fpath)[:-4]
vid_num = len(vid_grapped) vid_num = len(ivtc_list)
# Korean title text bug fix (work in progress)
# vid_name = vid_name.encode('utf-8').decode('cp949') # vid_name = vid_name.encode('utf-8').decode('cp949')
print(f"Video num : {i + 1} / {vid_num}") print(f"[INFO]\t Video num : {i + 1} / {vid_num}")
print("File to detect crop: ", fpath) print("[INFO]\t File to detect crop area: ", fpath)
print("[INFO]\t Detecting crop area...")
p = subprocess.Popen(["ffmpeg", "-ss", "10", "-i", fpath, "-vf", "cropdetect=limit=38", p = subprocess.Popen(["ffmpeg", "-ss", "10", "-i", fpath, "-vf", "cropdetect=limit=38",
"-vframes", "3000", "-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')
@@ -98,116 +132,119 @@ def main():
# 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 detected crop info: %s" % mostCommonCrop)
global crop
crop = mostCommonCrop[0][0]
# Delete "crop=" in list cropPos if len(mostCommonCrop) > 0 :
crop = mostCommonCrop[0][0]
else :
print(f"[!!! ERROR !!!]\t Can`t read input vid. Skip to next vid \t ({fpath})")
continue
# Delete "crop=" in list "crop"
cropPos = crop.split(":") cropPos = crop.split(":")
cropPos[0] = cropPos[0][5:] cropPos[0] = cropPos[0][5:]
print(f"[INFO]\t Most common detected crop area: [w : {cropPos[0]}, h : {cropPos[1]}, x : {cropPos[2]}, y : {cropPos[3]}]")
cropPos = [int(x) for x in cropPos] cropPos = [int(x) for x in cropPos]
with redirect_stderr(f): # Capture vid, Get Vid info
# Capture vid, Get Vid info 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) fps = capture.get(cv2.CAP_PROP_FPS)
fps = capture.get(cv2.CAP_PROP_FPS) frame_pos = 0
frame_pos = 0 frame_count = 0
frame_count = 0
target_ratio = 16 / 9
# Crop Area even num check # Crop Area even num check
if (cropPos[0] % 2) != 0 : if (cropPos[0] % 2) != 0 :
if (cropPos[0] + 1) >= frame_width: if (cropPos[0] + 1) >= frame_width:
cropPos[0] -= 1 cropPos[0] -= 1
else: else:
cropPos[0] += 1 cropPos[0] += 1
if (cropPos[1] % 2) != 0 : if (cropPos[1] % 2) != 0 :
if (cropPos[1] + 1) >= frame_width: if (cropPos[1] + 1) >= frame_width:
cropPos[1] -= 1 cropPos[1] -= 1
else: else:
cropPos[1] += 1 cropPos[1] += 1
# Crop Area Multiple of 4 check # Crop Area Multiple of 4 check
if (cropPos[0] % 4) != 0 : if (cropPos[0] % 4) != 0 :
if (cropPos[0] + 2) >= frame_width: if (cropPos[0] + 2) >= frame_width:
cropPos[0] -= 2 cropPos[0] -= 2
else: else:
cropPos[0] += 2 cropPos[0] += 2
if (cropPos[1] % 4) != 0 : if (cropPos[1] % 4) != 0 :
if (cropPos[1] + 2) >= frame_width: if (cropPos[1] + 2) >= frame_width:
cropPos[1] -= 2 cropPos[1] -= 2
else: else:
cropPos[1] += 2 cropPos[1] += 2
print("Target Ratio : %.7f" %target_ratio) key = None
print("Input dir :", args.input_dir) isPaused = False
pausedFrame = None
frame = None
cv2.namedWindow(f'{vid_name}', cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO)
while True:
key = cv2.waitKeyEx(10)
key = None # Close button(X button) event
isPaused = False if cv2.getWindowProperty(f'{vid_name}', cv2.WND_PROP_VISIBLE) < 1 :
pausedFrame = None return
frame = None
cv2.namedWindow(f'{vid_name}', cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO)
while True:
key = cv2.waitKeyEx(10)
# Handle close button event if key > 0 :
if cv2.getWindowProperty(f'{vid_name}', cv2.WND_PROP_VISIBLE) < 1 : # 13 == Enter key ASCII Code
return if key == 13:
if key > 0 :
# 13 == Enter key ASCII Code
if key == 13:
break
# Pause
elif key == ord('p') or key == ord('P'):
if isPaused :
isPaused = False
else :
isPaused = True
frame = copy.deepcopy(pausedFrame)
# Replay
elif key == ord('r') or key == ord('R'):
if not isPaused:
capture = cv2.VideoCapture(fpath)
continue
# Move Forward (10s)
elif key == ord('e') or key == ord('E'):
if not isPaused:
capture.set(cv2.CAP_PROP_POS_FRAMES, frame_pos + (fps * 10))
# Move Backward (10s)
elif key == ord('q') or key == ord('Q'):
if not isPaused:
capture.set(cv2.CAP_PROP_POS_FRAMES, frame_pos - (fps * 10))
# Skip this Vid
elif key == ord('n') or key == ord('N'):
break;
else:
# Crop Area control
cropPos = resizeCropInfo(key, cropPos, frame_width, frame_height)
frame_pos = capture.get(cv2.CAP_PROP_POS_FRAMES)
frame_count = capture.get(cv2.CAP_PROP_FRAME_COUNT)
if(frame_pos >= frame_count):
break break
# Pause
elif key == ord('p') or key == ord('P'):
if isPaused :
isPaused = False
else :
isPaused = True
frame = copy.deepcopy(pausedFrame)
# Replay
elif key == ord('r') or key == ord('R'):
if not isPaused:
capture = cv2.VideoCapture(fpath)
continue
# Move Forward (10s)
elif key == ord('e') or key == ord('E'):
if not isPaused:
capture.set(cv2.CAP_PROP_POS_FRAMES, frame_pos + (fps * 10))
# Move Backward (10s)
elif key == ord('q') or key == ord('Q'):
if not isPaused:
capture.set(cv2.CAP_PROP_POS_FRAMES, frame_pos - (fps * 10))
elif key == ord('h') or key == ord('H'):
print("[INFO]\t ### KEY GUIDE ########################################################################################################")
print("[INFO]\t # (MOVE : wasd), (RESIZE : ijkl), (FOR / BACKWARD : q, e), (RESET : r), (PAUSE : p), (SKIP : n), (CONFIRM : enter) #")
print("[INFO]\t ###################################################################################################################### ")
# Skip this Vid
elif key == ord('n') or key == ord('N'):
break;
else:
# Crop Area control
cropPos = resizeCropInfo(key, cropPos, frame_width, frame_height)
print("Current Crop Info : ", end="") frame_pos = capture.get(cv2.CAP_PROP_POS_FRAMES)
print(cropPos, end="") frame_count = capture.get(cv2.CAP_PROP_FRAME_COUNT)
print(f"\t Frame Count ({frame_pos}/{frame_count})", end="\r")
if not isPaused: if(frame_pos >= frame_count):
ret, frame = capture.read() break
pausedFrame = copy.deepcopy(frame)
cv2.rectangle(frame, (cropPos[2], cropPos[3]), (cropPos[2] + cropPos[0], print("[INFO]\t Current crop area : ", end="")
cropPos[3] + cropPos[1]), (0, 0, 255), thickness=1, lineType=cv2.LINE_8) print(cropPos, end="")
cv2.imshow(f'{vid_name}', frame) print(f"\t Frame Count ({frame_pos}/{frame_count})\t[PRESS \"h\" TO VIEW KEY GUIDE]", end="\r")
else :
pausedFrame = copy.deepcopy(frame) if not isPaused:
cv2.rectangle(pausedFrame, (cropPos[2], cropPos[3]), (cropPos[2] + cropPos[0], ret, frame = capture.read()
cropPos[3] + cropPos[1]), (0, 0, 255), thickness=1, lineType=cv2.LINE_8) pausedFrame = copy.deepcopy(frame)
cv2.imshow(f'{vid_name}', pausedFrame) 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.imshow(f'{vid_name}', frame)
else :
pausedFrame = copy.deepcopy(frame)
cv2.rectangle(pausedFrame, (cropPos[2], cropPos[3]), (cropPos[2] + cropPos[0],
cropPos[3] + cropPos[1]), (0, 0, 255), thickness=1, lineType=cv2.LINE_8)
cv2.imshow(f'{vid_name}', pausedFrame)
print("") print("")
capture.release() capture.release()
@@ -222,13 +259,13 @@ 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"[CropInfo] (W : {cropPos[0]}), (H : {cropPos[1]}), (X : {cropPos[2]}), (Y : {cropPos[3]})") print(f"[INFO]\t [CropInfo] (W : {cropPos[0]}), (H : {cropPos[1]}), (X : {cropPos[2]}), (Y : {cropPos[3]})")
crop_ratio = cropPos[0] / cropPos[1] crop_ratio = cropPos[0] / cropPos[1]
scale_arg = "" scale_arg = ""
# Set padding argument # Set padding(letter box) argument
# Offset for 16:9 display ratio # variable "offset" for 16:9 output display ratio
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
tmp_height = cropPos[1] + offset tmp_height = cropPos[1] + offset
@@ -237,7 +274,7 @@ def main():
tmp_height -= (tmp_height % 4) tmp_height -= (tmp_height % 4)
# 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"[INFO]\t [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
tmp_width = cropPos[0] + offset tmp_width = cropPos[0] + offset
@@ -246,15 +283,15 @@ def main():
tmp_width -= (tmp_width % 4) tmp_width -= (tmp_width % 4)
# 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"
print(f"[PadInfo] (W : {tmp_width}), (H : {cropPos[1]})") print(f"[INFO]\t [PadInfo] (W : {tmp_width}), (H : {cropPos[1]})")
print("Encoding \"%s\" ..." % vid_name) print("[INFO]\t Encoding \"%s\" ..." % vid_name)
if not os.path.exists(args.output_dir) : if not os.path.exists(output_dir) :
os.mkdir(args.output_dir) os.mkdir(output_dir)
print(f'{args.output_dir} created') print(f'[INFO]\t {output_dir} created')
output_path = os.path.join(args.output_dir, f"{vid_name}_box.mp4") output_path = os.path.join(output_dir, f"{vid_name}_box.mp4")
if args.debug: if args.debug:
# for Debug # for Debug
p = subprocess.Popen(["ffmpeg", "-t", "15", "-ss", "30", "-i", fpath, "-vf", crop_arg + scale_arg + pad_arg, "-y", p = subprocess.Popen(["ffmpeg", "-t", "15", "-ss", "30", "-i", fpath, "-vf", crop_arg + scale_arg + pad_arg, "-y",