-
Notifications
You must be signed in to change notification settings - Fork 1
/
video_hash.py
35 lines (27 loc) · 969 Bytes
/
video_hash.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
import hashlib
import numpy as np
from moviepy.editor import VideoFileClip
from frame_hash import FrameHash
from db_model import Video, add_video
def generate_file_md5(filename, blocksize=2**20):
m = hashlib.md5()
with open(filename, "rb") as f:
while True:
buf = f.read(blocksize)
if not buf:
break
m.update(buf)
return m.hexdigest()
def hash_frame(frame):
f_hasher = FrameHash(frame)
f_hasher.calculate_hash()
return f_hasher.get_result()
if __name__ == '__main__':
filepath = "output.avi"
clip = VideoFileClip(filepath)
clip_file_hash = generate_file_md5(filepath)
frame_hash_values = []
for f in clip.iter_frames(progress_bar=True):
frame_hash_values.append(hash_frame(f))
frame_hash_values = np.ravel(np.array(frame_hash_values, dtype=bool))
add_video(clip_file_hash, np.getbuffer(frame_hash_values))