-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
161 lines (138 loc) · 6.08 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import re
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
from os import path
from pathlib import Path
import cv2
import imagehash
import numpy
from PIL import Image
executor = ThreadPoolExecutor(max_workers=3)
def dict_by_value(dict, value):
for name, age in dict.items():
if age == value:
return name
def write_fingerprint(path, fingerprint):
path = "fingerprints/" + replace(path) + "/fingerprint.txt"
with open(path, "w+") as text_file:
text_file.write(fingerprint)
def replace(s):
return re.sub('[^A-Za-z0-9]+', '', s)
def create_video_fingerprint(path):
video_fingerprint = ""
video = cv2.VideoCapture(path)
frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
fps = int(video.get(cv2.CAP_PROP_FPS))
success, frame = video.read()
count = 0
Path("fingerprints/" + replace(path) + "/frames").mkdir(parents=True, exist_ok=True)
quarter_frames_or_first_X_mins = min(int(frames / 4), int(fps * 60 * max_fingerprint_mins))
while count < quarter_frames_or_first_X_mins: # what is less - the first quarter or the first 10 minuets
if debug:
cv2.imwrite("fingerprints/" + replace(path) + "/frames/frame%d.jpg" % count, frame)
image = Image.fromarray(numpy.uint8(frame))
frame_fingerprint = str(imagehash.dhash(image))
video_fingerprint += frame_fingerprint
if count % 1000 == 0:
print(path + " " + str(count) + "/" + str(quarter_frames_or_first_X_mins))
success, frame = video.read()
count += 1
if video_fingerprint == "":
raise Exception("video fingerprint empty created " + path)
return video_fingerprint
def get_equal_frames(print1, print2):
equal_frames = []
for j in range(0, int(len(print1) / 16 / check_frame)):
if print1[j * 16 * check_frame:j * 16 * check_frame + 16] == print2[
j * 16 * check_frame:j * 16 * check_frame + 16]:
equal_frames.append(print1[j * 16 * check_frame:j * 16 * check_frame + 16])
return equal_frames
def get_start_end(print1, print2):
highest_equal_frames = []
for k in range(0, int(len(print1) / 16)):
equal_frames = get_equal_frames(print1[-k * 16:], print2)
if len(equal_frames) > len(highest_equal_frames):
highest_equal_frames = equal_frames
equal_frames = get_equal_frames(print1, print2[k * 16:])
if len(equal_frames) > len(highest_equal_frames):
highest_equal_frames = equal_frames
regex_string = ".*?".join(highest_equal_frames) + "){1,}"
regex_string = regex_string[:-21] + '(' + regex_string[-21:]
p = re.compile(regex_string)
search = re.search(p, "".join(print1))
search2 = re.search(p, "".join(print2))
return (int(search.start() / 16), int(search.end() / 16)), (int(search2.start() / 16), int(search2.end() / 16))
def get_or_create_fingerprint(file):
if path.exists("fingerprints/" + replace(file) + "/fingerprint.txt"):
print(file + " fingerprint exists - loading it")
with open("fingerprints/" + replace(file) + "/fingerprint.txt", "r") as text_file:
fingerprint = text_file.read()
else:
print(file + " fingerprint does not exist - creating it")
fingerprint = create_video_fingerprint(file)
write_fingerprint(file, fingerprint)
return fingerprint
start = datetime.now()
print(start)
debug = True
check_frame = 10 # 1 (slow) to 10 (fast) is fine
max_fingerprint_mins = 10
print("Check Frame: " + str(check_frame))
file_paths = [
'samples/Modern Family (2009) S11E01.mkv',
'samples/Modern Family (2009) S11E02.mkv',
'samples/Modern Family (2009) S11E03.mkv',
'samples/Modern Family (2009) S11E04.mkv',
'samples/Modern Family (2009) S11E05.mkv',
'samples/Modern Family (2009) S11E06.mkv',
'samples/Modern Family (2009) S11E07.mkv',
'samples/Modern Family (2009) S11E08.mkv',
'samples/Modern Family (2009) S11E09.mkv',
'samples/Modern Family (2009) S11E10.mkv',
'samples/Modern Family (2009) S11E11.mkv',
'samples/Modern Family (2009) S11E12.mkv',
'samples/Modern Family (2009) S11E13.mkv',
'samples/Modern Family (2009) S11E14.mkv',
'samples/Modern Family (2009) S11E15.mkv',
'samples/Modern Family (2009) S11E16.mkv',
'samples/Modern Family (2009) S11E17.mkv',
'samples/Modern Family (2009) S11E18.mkv',
]
futures = []
fingerprints = []
file_paths = [
'/media/video/TV-Sendungen/Modern Family (2009)/Staffel 08/Modern Family (2009) S08E01.mkv',
'/media/video/TV-Sendungen/Modern Family (2009)/Staffel 08/Modern Family (2009) S08E02.mkv'
]
for file_path in file_paths:
futures.append(executor.submit(get_or_create_fingerprint, file_path))
for future in futures:
fingerprints.append(future.result())
counter = 0
average = 0
while len(fingerprints) - 1 > counter:
try:
start_end = get_start_end(fingerprints[counter], fingerprints[counter + 1])
print(file_paths[counter] + " start frame: " + str(start_end[0][0] - check_frame + 1) + " end frame: " + str(
start_end[0][1]))
print(
file_paths[counter + 1] + " start frame: " + str(start_end[1][0] - check_frame + 1) + " end frame: " + str(
start_end[1][1]))
average += start_end[0][1] - start_end[0][0]
average += start_end[1][1] - start_end[1][0]
except:
print("could not compare fingerprints from files " + file_paths[counter] + " " + file_paths[counter + 1])
counter += 2
if (len(fingerprints) % 2) != 0:
try:
start_end = get_start_end(fingerprints[-2], fingerprints[-1])
print(file_paths[-1] + " start frame: " + str(start_end[1][0] - check_frame + 1) + " end frame: " + str(
start_end[1][1]))
average += start_end[1][1] - start_end[1][0]
except:
print("could not compare fingerprints from files " + file_paths[-2] + " " + file_paths[-1])
end = datetime.now()
print(end)
print("duration: " + str(end - start))
print("average: " + str(int(average / len(fingerprints)) + check_frame * 2 - 2))
executor.shutdown()