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
| def decode(invideo_path, outfile_path):
i = 0
step = 20
chunk_size = 1_000_000
chunk = []
cap = cv2.VideoCapture(invideo_path)
ret, frame = cap.read()
while ret:
frame_shape = np.shape(frame)
for j in range(0, frame_shape[0], step):
for k in range(0, frame_shape[1], step):
piece = frame[j:j+step, k:k+step].reshape(step*step, 3)
rounded = normal(piece.mean(0)).round().astype(np.uint8)
chunk.append(rounded)
if len(chunk) >= chunk_size:
data_bits = np.array(chunk).reshape(len(chunk) * 3, 1)
data_bytes = np.packbits(data_bits)
len_of_data = int.from_bytes(data_bytes[:4], byteorder='big')
data_bytes_retrieved = data_bytes[4:len_of_data]
with open(outfile_path, 'ab') as f:
f.write(data_bytes_retrieved)
chunk = []
ret, frame = cap.read()
i += 1
cap.release()
if chunk:
data_bits = np.array(chunk).reshape(len(chunk) * 3, 1)
data_bytes = np.packbits(data_bits)
len_of_data = int.from_bytes(data_bytes[:4], byteorder='big')
data_bytes_retrieved = data_bytes[4:len_of_data]
with open(outfile_path, 'ab') as f:
f.write(data_bytes_retrieved) |
Partager