ETA display

This commit is contained in:
JunHo Kim committed 2021-12-14 10:52:34 +09:00
1 parent 99ecefe592
commit 1293b9184a
3 files changed
+148 -90

No files matched your search

+16 -9
View File
@@ -17,11 +17,10 @@ parser.add_argument("-d", "--debug", default=False, action="store_true", help="f
# logfile option # logfile option
parser.add_argument("-s", "--cropsize_scale", default=1, type=int, help="before use crop size from logfile, scale up the crop size (for reuse when vid rescaled)") parser.add_argument("-s", "--cropsize_scale", default=1, type=int, help="before use crop size from logfile, scale up the crop size (for reuse when vid rescaled)")
parser.add_argument("-log", "--only_logfile", default=False, action="store_true", help="large vid mode. (only write log file.)") parser.add_argument("-log", "--only_logfile", default=False, action="store_true", help="large vid mode. (only write log file.)")
parser.add_argument("-man", "--manual_mode", default=False, action="store_true", help="manual cropping without using log file (Ignore log files)") parser.add_argument("-manual", "--manual_mode", default=False, action="store_true", help="manual cropping without using log file (Ignore log files)")
parser.add_argument("-edit", "--edit_mode", default=False, action="store_true", help="get first crop area from log file") parser.add_argument("-edit", "--edit_mode", default=False, action="store_true", help="get first crop area from log file")
args = parser.parse_args() args = parser.parse_args()
# option when only write log file (no encoding) : -log # option when only write log file (no encoding) : -log
# options when only edit log file that already exists (no encoding) : -log -edit # options when only edit log file that already exists (no encoding) : -log -edit
@@ -51,7 +50,6 @@ def main():
for vid_index, fpath in enumerate(input_list): for vid_index, fpath in enumerate(input_list):
vid_name = os.path.basename(fpath)[:-4] vid_name = os.path.basename(fpath)[:-4]
vid_num = len(input_list) vid_num = len(input_list)
isSkipped = False
log_path = f"{logs_dir}/{vid_name}.txt" log_path = f"{logs_dir}/{vid_name}.txt"
print(f"[INFO]\t [Video num : {vid_index + 1} / {vid_num}]") print(f"[INFO]\t [Video num : {vid_index + 1} / {vid_num}]")
print("[INFO]\t File path: ", fpath) print("[INFO]\t File path: ", fpath)
@@ -75,29 +73,38 @@ def main():
else : else :
cropPos = autoDetectCropArea(fpath) cropPos = autoDetectCropArea(fpath)
# Crop Area validation check (width, height must be even number) # Crop Area validation check (width, height must be even number)
cropPos = adjustCropArea(cropPos) if cropPos == []:
cropPos = [crop_cfg.frame_width, crop_cfg.frame_height, 0, 0]
cropPos = adjustCropArea(cropPos, crop_cfg.frame_width, crop_cfg.frame_height)
print(cropPos)
cv2.namedWindow(f'{vid_name}', cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) cv2.namedWindow(f'{vid_name}', cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO)
# Display crop size control UI # Display crop size control UI
while True: while True:
retCode, cropPos = video_croppping(crop_cfg, cropPos) retCode, cropPos = videoCropping(crop_cfg, cropPos)
if retCode == 1: if retCode == 1:
break break
elif retCode == 2: elif retCode == 2:
crop_cfg.getVidInfo(fpath) crop_cfg.getVidInfo(fpath)
continue continue
elif retCode == 3:
print("")
return
print("") print("")
crop_cfg.vidRelease() crop_cfg.vidRelease()
cv2.destroyAllWindows() cv2.destroyAllWindows()
if not isSkipped: if not crop_cfg.isSkipped:
writeLog(logs_dir, log_path, cropPos) writeLog(logs_dir, log_path, cropPos)
# Set ffmpeg commands, actual ffmpeg encoding # Set ffmpeg commands, actual ffmpeg encoding
if not (args.only_logfile or isSkipped): if not (args.only_logfile or crop_cfg.isSkipped):
ffmpeg_encoding(args, vid_index, vid_num, cropPos, vid_name, fpath) retCode = ffmpegEncoding(args, vid_index, vid_num, cropPos, vid_name, fpath, crop_cfg.ffmpeg_process)
if retCode < 0 :
return retCode
if __name__ == "__main__": if __name__ == "__main__":
main() main()
print("[INFO]\t Program End.") print(f"\r\n Program End. end time : {getTime('string')}")
+79 -68
View File
@@ -1,7 +1,9 @@
from pillar_detection_utils import * from pillar_detection_utils import *
import signal
class VideoCropCfg(): class VideoCropCfg():
def __init__(self): def __init__(self):
self.ffmpeg_process = []
self.vid_name = '' self.vid_name = ''
self.frame_pos = 0 self.frame_pos = 0
@@ -20,19 +22,20 @@ class VideoCropCfg():
def getVidInfo(self, fpath): def getVidInfo(self, fpath):
self.captured_vid = cv2.VideoCapture(fpath) self.captured_vid = cv2.VideoCapture(fpath)
self.frame_width = self.captured_vid.get(cv2.CAP_PROP_FRAME_WIDTH) self.frame_width = int(self.captured_vid.get(cv2.CAP_PROP_FRAME_WIDTH))
self.frame_height = self.captured_vid.get(cv2.CAP_PROP_FRAME_HEIGHT) self.frame_height = int(self.captured_vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
self.fps = self.captured_vid.get(cv2.CAP_PROP_FPS) self.fps = self.captured_vid.get(cv2.CAP_PROP_FPS)
def vidRelease(self): def vidRelease(self):
self.captured_vid.release() self.captured_vid.release()
def video_croppping(crop_cfg, cropPos): def videoCropping(crop_cfg, cropPos):
''' '''
retCode value mean retCode value mean
0 : keep playing 0 : keep playing
1 : done with cropping (to next vid) 1 : done with cropping (to next vid)
2 : replay 2 : replay
3 : program exit
''' '''
retCode = 0 retCode = 0
@@ -44,8 +47,8 @@ def video_croppping(crop_cfg, cropPos):
# Close button(X button) event # Close button(X button) event
if cv2.getWindowProperty(f'{crop_cfg.vid_name}', cv2.WND_PROP_VISIBLE) < 1 : if cv2.getWindowProperty(f'{crop_cfg.vid_name}', cv2.WND_PROP_VISIBLE) < 1 :
retCode = 1 retCode = 3
return retCode, cropPos return retCode, []
if key > 0 : if key > 0 :
# 13 == Enter key ASCII Code # 13 == Enter key ASCII Code
@@ -76,9 +79,11 @@ def video_croppping(crop_cfg, cropPos):
print("[INFO]\t ############################################################################################################################################################################ ") print("[INFO]\t ############################################################################################################################################################################ ")
# Skip this Vid # Skip this Vid
elif key == ord('n') or key == ord('N'): elif key == ord('n') or key == ord('N'):
print("")
print(f"[INFO]\t Skip this video") print(f"[INFO]\t Skip this video")
crop_cfg.isSkipped = True crop_cfg.isSkipped = True
retCode = 1 retCode = 1
return retCode, []
# Preview this crop area on new window # Preview this crop area on new window
elif key == ord('v') or key == ord('V'): elif key == ord('v') or key == ord('V'):
_x = cropPos[2] _x = cropPos[2]
@@ -113,7 +118,6 @@ def video_croppping(crop_cfg, cropPos):
_path = os.path.join(path, image_name) _path = os.path.join(path, image_name)
imwrite(_path, _frame[_y: (_y + _h), _x: (_x + _w)]) imwrite(_path, _frame[_y: (_y + _h), _x: (_x + _w)])
print(f"[INFO]\t Saved current frame in crop area : {_path}") print(f"[INFO]\t Saved current frame in crop area : {_path}")
else: else:
# Crop Area control # Crop Area control
cropPos = resizeCropInfo(key, cropPos, crop_cfg.frame_width, crop_cfg.frame_height) cropPos = resizeCropInfo(key, cropPos, crop_cfg.frame_width, crop_cfg.frame_height)
@@ -124,23 +128,22 @@ def video_croppping(crop_cfg, cropPos):
if(crop_cfg.frame_pos >= crop_cfg.frame_count): if(crop_cfg.frame_pos >= crop_cfg.frame_count):
retCode = 1 retCode = 1
# When paused, hold current frame untill unpause.
if crop_cfg.isPaused:
# deep copy current frame, use that frame for draw rectangle.
crop_cfg.boxedFrame = copy.deepcopy(crop_cfg.cleanFrame)
cv2.rectangle(crop_cfg.boxedFrame, (cropPos[2], cropPos[3]), (cropPos[2] + cropPos[0],
cropPos[3] + cropPos[1]), (0, 0, 255), thickness=2, lineType=cv2.LINE_8)
cv2.imshow(f'{crop_cfg.vid_name}', crop_cfg.boxedFrame)
else : else :
_, crop_cfg.frame = crop_cfg.captured_vid.read() # When paused, hold current frame untill unpause.
# deep copy current frame for clean preview. if crop_cfg.isPaused:
crop_cfg.cleanFrame = copy.deepcopy(crop_cfg.frame) # deep copy current frame, use that frame for draw rectangle.
cv2.rectangle(crop_cfg.frame, (cropPos[2], cropPos[3]), (cropPos[2] + cropPos[0], crop_cfg.boxedFrame = copy.deepcopy(crop_cfg.cleanFrame)
cropPos[3] + cropPos[1]), (0, 0, 255), thickness=2, lineType=cv2.LINE_8) cv2.rectangle(crop_cfg.boxedFrame, (cropPos[2], cropPos[3]), (cropPos[2] + cropPos[0],
cv2.imshow(f'{crop_cfg.vid_name}', crop_cfg.frame) cropPos[3] + cropPos[1]), (0, 0, 255), thickness=2, lineType=cv2.LINE_8)
crop_cfg.boxedFrame = copy.deepcopy(crop_cfg.frame) cv2.imshow(f'{crop_cfg.vid_name}', crop_cfg.boxedFrame)
else :
_, crop_cfg.frame = crop_cfg.captured_vid.read()
# deep copy current frame for clean preview.
crop_cfg.cleanFrame = copy.deepcopy(crop_cfg.frame)
cv2.rectangle(crop_cfg.frame, (cropPos[2], cropPos[3]), (cropPos[2] + cropPos[0],
cropPos[3] + cropPos[1]), (0, 0, 255), thickness=2, lineType=cv2.LINE_8)
cv2.imshow(f'{crop_cfg.vid_name}', crop_cfg.frame)
crop_cfg.boxedFrame = copy.deepcopy(crop_cfg.frame)
print("[INFO]\t Current crop area : ", end="") print("[INFO]\t Current crop area : ", end="")
print(cropPos, end="") print(cropPos, end="")
@@ -148,7 +151,9 @@ def video_croppping(crop_cfg, cropPos):
return retCode, cropPos return retCode, cropPos
def ffmpeg_encoding(args, vid_index, vid_num, cropPos, vid_name, fpath): def ffmpegEncoding(args, vid_index, vid_num, cropPos, vid_name, fpath, ffmpeg_process):
retCode = -1
uhd_output = args.uhd_output uhd_output = args.uhd_output
debug = args.debug debug = args.debug
add_letterbox = args.add_letterbox add_letterbox = args.add_letterbox
@@ -217,26 +222,27 @@ def ffmpeg_encoding(args, vid_index, vid_num, cropPos, vid_name, fpath):
ffmpeg arguments ffmpeg arguments
-stats : print only encoding progress status (ignore loglevel argument) -stats : print only encoding progress status (ignore loglevel argument)
-loglevel : make ffmpeg stdout more verbose or not -loglevel : make ffmpeg stdout more verbose or not
quiet : no logs
warning : only warning, error warning : only warning, error
error : only errors error : only errors
fatal : only fatal (crash) errors fatal : only fatal (crash) errors
info : defualt info : defualt
''' '''
loglevel = 'fatal' loglevel = 'quiet'
ffmpeg_command = f"ffmpeg {debug_shortOutput} -i \"{fpath}\" -vf \"{crop_arg}{scale_arg}{pad_arg}{sharpen_arg}\"\ ffmpeg_command = f"ffmpeg {debug_shortOutput} -i \"{fpath}\" -vf \"{crop_arg}{scale_arg}{pad_arg}{sharpen_arg}\"\
-y -pix_fmt yuv420p -c:v libx264 -crf {crf} -c:a copy -preset medium -loglevel {loglevel}\ -y -pix_fmt yuv420p -c:v libx264 -crf {crf} -c:a copy -preset medium -loglevel {loglevel}\
{uhd_output_args} \"{output_path}\"" {uhd_output_args} \"{output_path}\""
duration_list = getVideDuration(fpath) vid_duration = getVideDuration(fpath)
vid_hour = duration_list[0] vid_hour = vid_duration[0]
vid_min = duration_list[1] vid_min = vid_duration[1]
vid_sec = duration_list[2] vid_sec = vid_duration[2]
# print("====================================================================") # print("====================================================================")
print("[INFO]\t Encoding start \"%s\" ..." % vid_name) print("[INFO]\t Encoding start \"%s\" ..." % vid_name)
print("[INFO]\t Video CRF : %d" % int(crf)) print("[INFO]\t CRF : %d" % int(crf))
print("[INFO]\t Video Duration : %02d:%02d:%02d" % (vid_hour, vid_min, vid_sec)) print("[INFO]\t Video Duration : %02d:%02d:%02d" % (vid_hour, vid_min, vid_sec))
print("[INFO]\t Pixel format : YUV420p") print("[INFO]\t Pixel format : YUV420p")
print("[INFO]\t Video Codec : libx264") print("[INFO]\t Encoding Codec : libx264")
print("[INFO]\t Encoding Preset : medium") print("[INFO]\t Ffmpeg Preset : medium")
print(f"[INFO]\t Cropping Info : [W : {cropPos[0]} H : {cropPos[1]} X : {cropPos[2]} Y : {cropPos[3]}]") print(f"[INFO]\t Cropping Info : [W : {cropPos[0]} H : {cropPos[1]} X : {cropPos[2]} Y : {cropPos[3]}]")
print("[INFO]\t Crop area ratio : %.5f" %current_crop_ratio) print("[INFO]\t Crop area ratio : %.5f" %current_crop_ratio)
if add_letterbox : if add_letterbox :
@@ -252,39 +258,44 @@ def ffmpeg_encoding(args, vid_index, vid_num, cropPos, vid_name, fpath):
# st_time = time.time() # st_time = time.time()
vid_index += 1 vid_index += 1
if manual_mode: start_time = getTime()
# Manual Mode (always encoding one by one) try:
# Hold process when encoding last video if manual_mode:
if vid_index == vid_num : # Manual Mode (always cropping one by one)
print(f"[INFO]\t Video Duration : {vid_hour:02d}:{vid_min:02d}:{vid_sec:02d}\ # Hold process when encoding last video
Current Time : {getTime()}") if vid_index == vid_num :
subprocess.Popen(ffmpeg_command + ' -stats', shell=False).wait() print(f"[INFO]\t Video Duration : {vid_hour:02d}:{vid_min:02d}:{vid_sec:02d}\
else : Start Time : {getTime('string')}")
subprocess.Popen(ffmpeg_command, shell=False) ffmpeg_process.append(subprocess.Popen(ffmpeg_command + ' -stats', shell=False).wait())
else: else :
# Log file mode (encoding simultaneously n videos) ffmpeg_process.append(subprocess.Popen(ffmpeg_command, shell=False))
if args.multi_encoding == 0: else:
# duration_list[0] : hour, [1] : min, [2] : sec # Log file mode (encoding simultaneously n videos)
duration_list = getVideDuration(fpath) if args.multi_encoding == 0:
print(f"[INFO]\t Video Duration : {duration_list[0]:02d}:{duration_list[1]:02d}:{duration_list[2]:02d}\ subprocess.Popen(ffmpeg_command + ' -stats', shell=False).wait()
Current Time : {getTime()}")
p = subprocess.Popen(ffmpeg_command + ' -stats', shell=False).wait()
# [21.12.08] progress status coding.. elif (vid_index % args.multi_encoding == 0) or (vid_index == vid_num):
# p = subprocess.Popen(ffmpeg_command, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print(f"[INFO]\t Video Duration : {vid_hour:02d}:{vid_min:02d}:{vid_sec:02d}\
# while p.poll() is None: Start Time : {getTime('string')}")
# # print('now encoding..', end='\r') p = subprocess.Popen(ffmpeg_command + ' -stats', shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8')
# infos = p.stderr.read().decode('utf-8') ffmpeg_process.append(p.pid)
# print('h',infos) while p.poll() is None:
# infos = p.stdout.read().decode('utf-8') displayRemainTime(vid_duration, p.stderr, start_time)
# print('t',infos) else :
# continue p = subprocess.Popen(ffmpeg_command, shell=False)
elif (vid_index % args.multi_encoding == 0) or (vid_index == vid_num): ffmpeg_process.append(p.pid)
# duration_list[0] : hour, [1] : min, [2] : sec
duration_list = getVideDuration(fpath) retCode = 0
print(f"[INFO]\t Video Duration : {duration_list[0]:02d}:{duration_list[1]:02d}:{duration_list[2]:02d}\ except Exception as e :
Current Time : {getTime()}") print('[!!! ERROR !!!]\t', e)
subprocess.Popen(ffmpeg_command + ' -stats', shell=False).wait() # print(ffmpeg_process)
else : # for p in ffmpeg_process :
subprocess.Popen(ffmpeg_command, shell=False) # print('terminate')
# os.killpg(os.getpgid(p, signal.SIGTERM))
# # p.terminate()
# # p.kill()
retCode = -1
finally :
return retCode
+53 -13
View File
@@ -9,6 +9,53 @@ import copy
import datetime import datetime
import time import time
def getTime(tformat = None):
dt_now = datetime.datetime.now()
dt_str = dt_now.strftime('[%Y-%m-%d %H:%M:%S]')
if tformat == 'string' :
out = dt_str
else:
out = dt_now
# print(dt_now + datetime.timedelta(hours=1))
return out
def timeToSecond(time_stamp):
return (time_stamp[0] * 3600) + (time_stamp[1] * 60) + (time_stamp[2])
def displayRemainTime(vid_duration, ffmpeg_stream, start_time):
#TODO : now encoding... [percentage] [ETA : yyyy-mm-dd hh:mm:ss]
# infos (ex)= "frame= 228 fps= 37 q=26.0 size= 3840kB time=00:00:08.08 bitrate=3890.7kbits/s speed=1.31x"
infos = ffmpeg_stream.readline()
# parsing time stamp
time_stamp = re.findall("time=\S+", infos)
if time_stamp != []:
'''
parsing process
1. List type to String type
2. lid of "time=" string
3. get each time arguments (h:m:s)
4. String type to Int type
'''
time_stamp = time_stamp[0]
time_stamp = time_stamp[5:]
time_stamp = time_stamp.split(':')
time_stamp = [int(float(x)) for x in time_stamp]
# current_percentage
current_progress_sec = timeToSecond(time_stamp)
total_progress_sec = timeToSecond(vid_duration)
current_percentage = (current_progress_sec / total_progress_sec) * 100
# eta_time
passed_time = getTime() - start_time
passed_time = passed_time.total_seconds()
encoding_speed = current_percentage / passed_time
eta = (100 - current_percentage) / encoding_speed
eta_time = datetime.datetime.now() + datetime.timedelta(seconds=eta)
eta_time = eta_time.strftime('[ETA : %Y-%m-%d %H:%M:%S]')
print(f"[INFO]\t now encoding ... {eta_time} [{current_percentage:.2f}%] [Speed : {encoding_speed:.2f} %/s]", end='\r')
def resizeCropInfo(key, cropPos, frame_width, frame_height): def resizeCropInfo(key, cropPos, frame_width, frame_height):
# Adjust crop info in display layout. # Adjust crop info in display layout.
def _IsOutOfBound(_cropPos): def _IsOutOfBound(_cropPos):
@@ -60,13 +107,6 @@ def resizeCropInfo(key, cropPos, frame_width, frame_height):
return cropPos 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 getVideDuration(file_path): def getVideDuration(file_path):
ffprobe_command = f"ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 \"{file_path}\"" ffprobe_command = f"ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 \"{file_path}\""
p = subprocess.run(ffprobe_command, shell=False, stdout=subprocess.PIPE) p = subprocess.run(ffprobe_command, shell=False, stdout=subprocess.PIPE)
@@ -164,15 +204,15 @@ def autoDetectCropArea(fpath):
print("[INFO]\t Detecting crop area...") 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, encoding='utf-8')
infos = p.stderr.read().decode('utf-8') infos = p.stderr.read()
allCrops = re.findall("crop=\S+", infos) allCrops = re.findall("crop=\S+", infos)
mostCommonCrop = Counter(allCrops).most_common(1) mostCommonCrop = Counter(allCrops).most_common(1)
if len(mostCommonCrop) > 0 : if len(mostCommonCrop) > 0 :
crop = mostCommonCrop[0][0] crop = mostCommonCrop[0][0]
else : else :
print(f"[!!! ERROR !!!]\t Can`t read input vid. Skip to next vid \t ({fpath})") print(f"[!!! ERROR !!!]\t Can`t auto detect crop area \t ({fpath})")
return cropPos return cropPos
# Delete "crop=" in list "crop" # Delete "crop=" in list "crop"
@@ -183,7 +223,7 @@ def autoDetectCropArea(fpath):
return cropPos return cropPos
def adjustCropArea(cropPos): def adjustCropArea(cropPos, frame_width, frame_height):
# 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:
@@ -191,7 +231,7 @@ def adjustCropArea(cropPos):
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_height:
cropPos[1] -= 1 cropPos[1] -= 1
else: else:
cropPos[1] += 1 cropPos[1] += 1
@@ -203,7 +243,7 @@ def adjustCropArea(cropPos):
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_height:
cropPos[1] -= 2 cropPos[1] -= 2
else: else:
cropPos[1] += 2 cropPos[1] += 2