frame_bytes = bytearray() string='杭州涅普科技有限责任公司出品' string=string.encode() bits = list(map(int, ''.join(['{0:08b}'.format(i) for i in string]))) print(bits) for i,bit inenumerate(bits): print("[",bit,"]",end = '') frame_bytes.append(bit*255) print("[",bit*255,"]") frame_modified = bytes(frame_bytes)
# Write bytes to a new wave audio file with wave.open('song_embedded.wav', 'wb') as fd: fd.setparams((1, 1, 44100, 44100, 'NONE', 'not compressed')) fd.writeframes(frame_modified)
解码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# We will use wave package available in native Python installation to read and write .wav audio file import wave # read wave audiaudioo file song = wave.open("song_embedded.wav", mode='rb') # Read frames and convert to byte array frame_bytes = bytearray(list(song.readframes(song.getnframes()))) binstring='' print(frame_bytes) for bit in frame_bytes: if bit==255: binstring=binstring+'1' else : binstring=binstring+'0' raw=bytes(int(binstring[i : i + 8], 2) for i inrange(0, len(binstring), 8)) print(raw) print(raw.decode())
说些什么吧!