-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstereoSteg_encode.py
More file actions
68 lines (53 loc) · 1.63 KB
/
stereoSteg_encode.py
File metadata and controls
68 lines (53 loc) · 1.63 KB
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
import wave, sys
# Parse arguments
if len(sys.argv) != 4:
print(f"Usage: {sys.argv[0]} <input_file> <output_file> <text>")
sys.exit(1)
mono_file, stereo_file, text = None, None, sys.argv[3]
try:
mono_file = wave.open(sys.argv[1], "rb")
if mono_file.getnchannels() != 1:
print(f"[!] {sys.argv[1]} is not a mono file")
sys.exit(1)
except:
print(f"[!] Could not open {sys.argv[1]} as wav file")
sys.exit(1)
try:
stereo_file = wave.open(sys.argv[2], "wb")
except:
print(f"[!] Could not open {sys.argv[2]} as wav file")
sys.exit(1)
# Config output file
print(f"[+] Configuring output file {sys.argv[2]}")
stereo_file.setnchannels(2)
stereo_file.setsampwidth(mono_file.getsampwidth())
stereo_file.setframerate(mono_file.getframerate())
# Load and trim audio file
print(f"[+] Loading input file in memory and trimming")
started = False
in_frames = []
for _ in range(mono_file.getnframes()):
f = mono_file.readframes(1)
if f != b"\x00\x00":
in_frames.append(f)
# Convert text to binary
binary = ""
for l in text:
binary += bin(ord(l))[2:].zfill(8)
# Encode text
bit_size = len(in_frames) // len(binary)
print(f"[+] Encoding {len(binary)} bits into {len(in_frames)} frames")
frame_counter = 0
for bit in binary:
for _ in range(bit_size):
f = in_frames[frame_counter]
if bit == "1":
stereo_file.writeframes(f)
stereo_file.writeframes(b"\x00\x00")
else:
stereo_file.writeframes(b"\x00\x00")
stereo_file.writeframes(f)
frame_counter += 1
print(f"[+] Encoding complete")
mono_file.close()
stereo_file.close()