Browse Source

Now cuts on keyframes, it kinda works a bit better

master
bertieb 3 years ago
parent
commit
e6084c43f5
1 changed files with 40 additions and 14 deletions
  1. +40
    -14
      efficientjson.py

+ 40
- 14
efficientjson.py View File

@@ -129,8 +129,12 @@ def process_highlights(jsonfile):
video_length = get_video_duration(videofile) video_length = get_video_duration(videofile)


# main clip: full length minus half transition duration # main clip: full length minus half transition duration

# cut on keyframe
cut_time = nearest_keyframe(videofile, (video_length - 1))

ffmpeg_video_args = ["-i", videofile, ffmpeg_video_args = ["-i", videofile,
"-t", str(video_length - 1),
"-t", str(cut_time),
"-c", "copy", "-c", "copy",
"{}".format(videofile.replace(".mkv", "M.mkv"))] "{}".format(videofile.replace(".mkv", "M.mkv"))]


@@ -138,7 +142,7 @@ def process_highlights(jsonfile):


# omega clip: half transition duration # omega clip: half transition duration
ffmpeg_video_args = ["-i", videofile, ffmpeg_video_args = ["-i", videofile,
"-ss", str(video_length - 1),
"-ss", str(cut_time),
"-c:v", "libx264", "-crf", "20", "-c:a", "copy", "-c:v", "libx264", "-crf", "20", "-c:a", "copy",
"{}".format(videofile.replace(".mkv", "O.mkv"))] "{}".format(videofile.replace(".mkv", "O.mkv"))]


@@ -148,9 +152,12 @@ def process_highlights(jsonfile):
"""Split last videofile into videofileA + videofileM""" """Split last videofile into videofileA + videofileM"""
import subprocess import subprocess


# cut on keyframe
cut_time = nearest_keyframe(videofile, TRANSITION_DURATION/2)

# alpha clip: half transition duration # alpha clip: half transition duration
ffmpeg_video_args = ["-i", videofile, ffmpeg_video_args = ["-i", videofile,
"-t", str(TRANSITION_DURATION/2),
"-t", str(cut_time),
"-c:v", "libx264", "-crf", "20", "-c:a", "copy", "-c:v", "libx264", "-crf", "20", "-c:a", "copy",
"{}".format(videofile.replace(".mkv", "A.mkv"))] "{}".format(videofile.replace(".mkv", "A.mkv"))]


@@ -158,7 +165,7 @@ def process_highlights(jsonfile):


# main clip: start at half transition duration, full length # main clip: start at half transition duration, full length
ffmpeg_video_args = ["-i", videofile, ffmpeg_video_args = ["-i", videofile,
"-ss", str(TRANSITION_DURATION/2),
"-ss", str(cut_time),
"-c", "copy", "-c", "copy",
"{}".format(videofile.replace(".mkv", "M.mkv"))] "{}".format(videofile.replace(".mkv", "M.mkv"))]


@@ -171,9 +178,14 @@ def process_highlights(jsonfile):


video_length = get_video_duration(videofile) 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 # alpha clip: half transition duration
ffmpeg_video_args = ["-i", videofile, ffmpeg_video_args = ["-i", videofile,
"-t", str(TRANSITION_DURATION/2),
"-t", str(cut_time1),
"-c:v", "libx264", "-crf", "20", "-c:a", "copy", "-c:v", "libx264", "-crf", "20", "-c:a", "copy",
"{}".format(videofile.replace(".mkv", "A.mkv"))] "{}".format(videofile.replace(".mkv", "A.mkv"))]


@@ -181,8 +193,8 @@ def process_highlights(jsonfile):


# main clip: full length minus half transition duration # main clip: full length minus half transition duration
ffmpeg_video_args = ["-i", videofile, 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", "-c", "copy",
"{}".format(videofile.replace(".mkv", "M.mkv"))] "{}".format(videofile.replace(".mkv", "M.mkv"))]


@@ -191,7 +203,7 @@ def process_highlights(jsonfile):
# omega clip: half transition duration # omega clip: half transition duration
ffmpeg_video_args = ["-i", videofile, ffmpeg_video_args = ["-i", videofile,
"-ss", "-ss",
str(video_length - (TRANSITION_DURATION/2)),
str(cut_time2),
"-c:v", "libx264", "-crf", "20", "-c:a", "copy", "-c:v", "libx264", "-crf", "20", "-c:a", "copy",
"{}".format(videofile.replace(".mkv", "O.mkv"))] "{}".format(videofile.replace(".mkv", "O.mkv"))]


@@ -211,7 +223,12 @@ def process_highlights(jsonfile):
import pdb import pdb
pdb.set_trace() 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, final = CompositeVideoClip([clip1,
clip2.set_start(clip1.end-tr) clip2.set_start(clip1.end-tr)
@@ -227,6 +244,8 @@ def process_highlights(jsonfile):


see eg https://superuser.com/a/1426307""" see eg https://superuser.com/a/1426307"""
import subprocess import subprocess
print("Finding nearest keyframe to {} for {}".format(
checktime, videofile))


if checktime > 15: if checktime > 15:
skiptime = checktime - 15 skiptime = checktime - 15
@@ -247,13 +266,20 @@ def process_highlights(jsonfile):
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
universal_newlines=True) 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: if "pkt_pts_time" in line:
frametime = float(line.split("=")[1]) frametime = float(line.split("=")[1])
frames.append(frametime)
if round(frametime, 2) != 0.00: # avoid 0s iframe
frames.append(frametime)
if len(frames) > 20: if len(frames) > 20:
proc.kill()
break


# if "stdout" in complete: # if "stdout" in complete:
# for line in complete["stdout"].split("\n"): # for line in complete["stdout"].split("\n"):
@@ -263,7 +289,7 @@ def process_highlights(jsonfile):
# get closest value, see https://stackoverflow.com/a/12141207 # get closest value, see https://stackoverflow.com/a/12141207
keyframetime = min(frames, key=lambda x: abs(x-checktime)) 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 # we return the time just before it
# so that ffmpeg seeks to that when # so that ffmpeg seeks to that when
# cutting # cutting


Loading…
Cancel
Save