python将文本隐藏到音频文件中

编码

import wave

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 in enumerate(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)

解码

# 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 in range(0, len(binstring), 8))
print(raw)
print(raw.decode())

成功后使用sonic visualiser 打开可以看到高的采样点代表1,低的采样点代表0,如此11100110…这样排列出来就是我们要隐藏的文本信息。
在这里插入图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值