-
Notifications
You must be signed in to change notification settings - Fork 1
/
make_highlight.py
68 lines (44 loc) · 1.29 KB
/
make_highlight.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import pickle
from heapq import *
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
from moviepy.editor import VideoFileClip, concatenate_videoclips
filename = 'test720.mp4'
def get_key(s):
return s[0]
with open('score.pkl','rb') as fp:
score = pickle.load(fp)
blocks = []
s = None
maxi = 0
for i in range(len(score)):
if score[i] >= 0.74:
if s is None:
s = i
else:
maxi += score[i]
else:
if s is not None:
st = s*3
et = (i-1)*3 + 4
duration = et - st
maxi /= (i-s)
heappush(blocks,(1-maxi,st,et,duration)) # because it's min-heap
s = None
maxi = 0
# clips selection
selecteds = []
time_remain = 180 # sec
while time_remain > 0:
block = heappop(blocks)
selected = (block[1],block[2],block[3],block[0])
heappush(selecteds, selected)
time_remain -= block[3]
selecteds.sort(key=get_key)
# make highlight
clips = []
for selected in selecteds:
tname = 'tttmp\{}.mp4'.format(selected[0])
ffmpeg_extract_subclip(filename, selected[0], selected[1], targetname=tname)
clips.append(VideoFileClip(tname))
final_clip = concatenate_videoclips(clips)
final_clip.write_videofile("my_concatenation.mp4")