我正试图解决我的音频隐写代码中的一个问题。由于将消息隐藏在wav音频文件中,考虑到整个音频隐藏的要点,存在一些不应该存在的噪音。非常感谢你的帮助!
这是代码
import wave
import os
global chosen_audio
def hide():
hide_menu()
global chosen_audio
message = input('Input message to be hidden: \n')
print('hiding message ... \n')
audio = wave.open(chosen_audio, 'rb')
frame_bytes = bytearray(list(audio.readframes(audio.getnframes())))
message = message + int((len(frame_bytes) - (len(message) * 8 * 8)) / 8) * '#'
bits = list(
map(int, ''.join([bin(ord(i)).lstrip('0b').rjust(8, '0') for i in message])))
for i, bit in enumerate(bits):
frame_bytes[i] = (frame_bytes[i] & 254) | bit # replace lsb
frames_modified = bytes(frame_bytes)
with wave.open('C:/Users/*****/PycharmProjects/steganography/modified_audio.wav', 'wb') as modified_audio:
modified_audio.setparams(audio.getparams())
modified_audio.writeframes(frames_modified)
print('message hidden ... \n')
modified_audio.close()
audio.close()
def reveal():
modified_audio = wave.open('C:/Users/*****/PycharmProjects/steganography/modified_audio.wav', 'rb')
frame_bytes = bytearray(list(modified_audio.readframes(modified_audio.getnframes())))
ls_bits = [frame_bytes[i] & 1 for i in range(len(frame_bytes))]
text = "".join(chr(int("".join(map(str, ls_bits[i:i + 8])), 2)) for i in range(0, len(ls_bits), 8))
message = text.split("###")[0]
modified_audio.close()
return message
def mode():
method = input(
' \n\t\t\tPLEASE, CHOOSE THE PROCEDURE! \n\n \tPRESS H FOR HIDING THE MESSAGE \t PRESS R FOR REVEALING THE MESSAGE FROM THE AUDIO\n')
if method == 'H' or method == 'h':
hide()
elif method == 'r' or method == 'R':
reveal()
else:
print('I don\'t think we have such a option')
mode()
def hide_menu():
global chosen_audio
chosen_option = ''
print(chosen_option)
chosen_option = ''
chosen_audio = ''
print(' \nCHOOSE YOUR AUDIO FILE! ')
chosen_option = (
input('\t press 1 & ENTER for your own audio path\n''\t press 2 & ENTER for default audio file\n'))
if chosen_option == '1':
file_path = input('Enter a file path: ')
if os.path.exists(file_path):
print('The path is okay, file exists!')
chosen_audio = file_path
else:
print('The specified file in this path does NOT exist')
hide_menu()
elif chosen_option == '2':
chosen_audio_option = input(
'\t press V & enter to use voice audio \t press S & enter to use sound audio\t press M & enter to use '
'song audio\n')
if chosen_audio_option == 'M' or chosen_audio_option == 'm':
chosen_audio = 'C:\\Users\\*****\\PycharmProjects\\steganography\\song_audio.wav'
elif chosen_audio_option == 'v' or chosen_audio_option == 'V':
chosen_audio = 'C:\\Users\\*****\\PycharmProjects\\steganography\\voice_audio.wav'
elif chosen_audio_option == 's' or chosen_audio_option == 'S':
chosen_audio = 'C:\\Users\\*****\\Desktop\\audio\\hracka_pes.wav'
else:
print('No such a option !')
hide_menu()
else:
print('I don\'t think we have such a option')
hide_menu()
def reveal_menu():
global chosen_audio
chosen_audio = ''
print(' \nCHOOSE YOUR AUDIO FILE! ')
chosen_option = int(
input('\t press 1 & ENTER for your own audio path\n''\t press 2 & ENTER for default audio file\n'))
if chosen_option == 1:
file_path = input('Enter a file path: ')
if os.path.exists(file_path):
print('The path is okay, file exists!')
chosen_audio = file_path
else:
print('The specified file in this path does NOT exist')
hide_menu()
elif chosen_option == 2:
pass
mode()
# menu()
# hide()注-不能使用库进行隐写。
在modified_audio中听到噪声是主要问题。
发布于 2022-11-26 17:30:52
您的代码是用来处理包含sampwidth=1的wave文件的,但是根据您的注释,您的文件的sampwidth是2。这意味着您的frame_bytes数组不是一个样本数组,它是一个字节数组,其中每两个字节一起形成一个示例。(因为nchannels是1,所以每个帧都有一个单一音频示例。)因此,当您更改所有字节的LSB时,您不仅更改了每个示例的LSB,而且还更改了其中的一些。
您应该将其转换为一个样本数组,然后更改示例LSB。您可以为此使用array:
import array
samples = array.array('h', frame_bytes) # signed 16-bit
# ... modify the samples ...
frames_modified = samples.tobytes()当然,如果您处理的是签名的短裤(sampsize==2)或无符号字节(sampsize==1),那么最好是事先检查代码,然后相应地处理它们。你可能会做这样的事情:
samples = array.array('h' if sampsize == 2 else 'B', frame_bytes)
for i, bit in enumerate(bits):
samples[i] = samples[i] & -2 | bit(我还没有做过这么多的测试)
https://stackoverflow.com/questions/74583881
复制相似问题