From e6084c43f5ddc3a9eb48d927a89ea88193030e41 Mon Sep 17 00:00:00 2001 From: bertieb Date: Fri, 18 Dec 2020 17:41:16 +0000 Subject: [PATCH] Now cuts on keyframes, it kinda works a bit better --- efficientjson.py | 54 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/efficientjson.py b/efficientjson.py index 93bf9de..2080387 100644 --- a/efficientjson.py +++ b/efficientjson.py @@ -129,8 +129,12 @@ def process_highlights(jsonfile): video_length = get_video_duration(videofile) # main clip: full length minus half transition duration + + # cut on keyframe + cut_time = nearest_keyframe(videofile, (video_length - 1)) + ffmpeg_video_args = ["-i", videofile, - "-t", str(video_length - 1), + "-t", str(cut_time), "-c", "copy", "{}".format(videofile.replace(".mkv", "M.mkv"))] @@ -138,7 +142,7 @@ def process_highlights(jsonfile): # omega clip: half transition duration ffmpeg_video_args = ["-i", videofile, - "-ss", str(video_length - 1), + "-ss", str(cut_time), "-c:v", "libx264", "-crf", "20", "-c:a", "copy", "{}".format(videofile.replace(".mkv", "O.mkv"))] @@ -148,9 +152,12 @@ def process_highlights(jsonfile): """Split last videofile into videofileA + videofileM""" import subprocess + # cut on keyframe + cut_time = nearest_keyframe(videofile, TRANSITION_DURATION/2) + # alpha clip: half transition duration ffmpeg_video_args = ["-i", videofile, - "-t", str(TRANSITION_DURATION/2), + "-t", str(cut_time), "-c:v", "libx264", "-crf", "20", "-c:a", "copy", "{}".format(videofile.replace(".mkv", "A.mkv"))] @@ -158,7 +165,7 @@ def process_highlights(jsonfile): # main clip: start at half transition duration, full length ffmpeg_video_args = ["-i", videofile, - "-ss", str(TRANSITION_DURATION/2), + "-ss", str(cut_time), "-c", "copy", "{}".format(videofile.replace(".mkv", "M.mkv"))] @@ -171,9 +178,14 @@ def process_highlights(jsonfile): video_length = get_video_duration(videofile) + # cut on keyframes + cut_time1 = nearest_keyframe(videofile, TRANSITION_DURATION/2) + cut_time2 = nearest_keyframe(videofile, ( + video_length - (TRANSITION_DURATION/2))) + # alpha clip: half transition duration ffmpeg_video_args = ["-i", videofile, - "-t", str(TRANSITION_DURATION/2), + "-t", str(cut_time1), "-c:v", "libx264", "-crf", "20", "-c:a", "copy", "{}".format(videofile.replace(".mkv", "A.mkv"))] @@ -181,8 +193,8 @@ def process_highlights(jsonfile): # main clip: full length minus half transition duration ffmpeg_video_args = ["-i", videofile, - "-ss", str(TRANSITION_DURATION/2), - "-t", str(video_length - (TRANSITION_DURATION/2)), + "-ss", str(cut_time1), + "-t", str(cut_time2), "-c", "copy", "{}".format(videofile.replace(".mkv", "M.mkv"))] @@ -191,7 +203,7 @@ def process_highlights(jsonfile): # omega clip: half transition duration ffmpeg_video_args = ["-i", videofile, "-ss", - str(video_length - (TRANSITION_DURATION/2)), + str(cut_time2), "-c:v", "libx264", "-crf", "20", "-c:a", "copy", "{}".format(videofile.replace(".mkv", "O.mkv"))] @@ -211,7 +223,12 @@ def process_highlights(jsonfile): import pdb pdb.set_trace() - clip2 = VideoFileClip(os.path.join(workingdir, video2)) + try: + clip2 = VideoFileClip(os.path.join(workingdir, video2)) + except IOError as e: + print("Error making joined clip: {}".format(e)) + import pdb + pdb.set_trace() final = CompositeVideoClip([clip1, clip2.set_start(clip1.end-tr) @@ -227,6 +244,8 @@ def process_highlights(jsonfile): see eg https://superuser.com/a/1426307""" import subprocess + print("Finding nearest keyframe to {} for {}".format( + checktime, videofile)) if checktime > 15: skiptime = checktime - 15 @@ -247,13 +266,20 @@ def process_highlights(jsonfile): stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, universal_newlines=True) - while not proc.poll(): - line = proc.stdout.readline() + try: + lines, error = proc.communicate(timeout=10) + except subprocess.TimoutExpired: + proc.kill() + lines, error = proc.communicate() + + print(lines) + for line in lines.split("\n"): if "pkt_pts_time" in line: frametime = float(line.split("=")[1]) - frames.append(frametime) + if round(frametime, 2) != 0.00: # avoid 0s iframe + frames.append(frametime) if len(frames) > 20: - proc.kill() + break # if "stdout" in complete: # for line in complete["stdout"].split("\n"): @@ -263,7 +289,7 @@ def process_highlights(jsonfile): # get closest value, see https://stackoverflow.com/a/12141207 keyframetime = min(frames, key=lambda x: abs(x-checktime)) - return str(round(keyframetime-0.1, 2)) + return str(round(keyframetime, 2)) # we return the time just before it # so that ffmpeg seeks to that when # cutting