with wave.open('result.wav',mode='wb') as f: f.setparams((2,2,200000,0,"NONE","NOT COMPRESSED")) #从左往右依次为双声道,量化精度两字节,采样率200k,不压缩,不压缩 f.writeframes((f_array*40).astype(np.int16)) #40是一个音量系数,用以在合理的范围内不失真地扩大图像大小以及精度(原来都是浮点数,转为整型会丢失精度),太大会超出量化精度的范围导致极大失真
# for i in sorted(os.listdir('raw')): for i in ['0240.png']: #这是个测试用的图 print(i) f_array=np.concatenate((f_array,procimg('raw/'+i))) with wave.open('result.wav',mode='wb') as f: f.setparams((2,2,FRAME_RATE,0,"NONE","NOT COMPRESSED")) f.writeframes((f_array*40).astype(np.int16))
VID_FPS=30 f_array=np.array([]) frames=sorted(os.listdir('raw')) # frames=['1430.png'] VID_FRAMES=len(frames) #多少帧 VID_DUR_TIME=VID_FRAMES/VID_FPS #多少秒 for i in frames: print(i) f_array=np.concatenate((f_array,procimg('raw/'+i))) SAMPLE_CNT=len(f_array)/2#采样点按左右声道两个为一对取总对数计 AUDIO_FRAME_RATE=SAMPLE_CNT//VID_DUR_TIME print(SAMPLE_CNT,AUDIO_FRAME_RATE) with wave.open('badapple.wav',mode='wb') as f: f.setparams((2,2,AUDIO_FRAME_RATE,0,"NONE","NOT COMPRESSED")) f.writeframes((f_array*40).astype(np.int16))
if (len_of_points:=len(points))<AVG_FRAMESIZE: perc=math.floor(AVG_FRAMESIZE/len_of_points) random.seed(114514) #使用固定seed使得抽样结果固定,最终效果为相同输入相同输出。wav的hash为一个固定值。 choosed=set([str(i) for i in random.sample(points,(AVG_FRAMESIZE%len_of_points))]) #使用set,后续查询时间复杂度O(1)
cap = (len_of_points*perc+len(choosed))*2 f_array = [None]*cap #预分配 cur_point = 0 for x,y in points: ch=0 ifstr([x,y]) in choosed: #愉快地O(1) ch=1 f_array[cur_point:cur_point+(perc+ch)*2] = [-(y*SIN_45+x*COS_45),-(y*COS_45-x*SIN_45)]*(perc+ch) #旋转 cur_point+=(perc+ch)*2 ...
f_array=np.array([]) frames=sorted(os.listdir('raw')) # frames=['1430.png'] VID_FRAMES=len(frames) VID_DUR_TIME=VID_FRAMES/VID_FPS for i in frames: print(i) f_array=np.concatenate((f_array,procimg('raw/'+i))) SAMPLE_CNT=len(f_array)/2 AUDIO_FRAME_RATE=SAMPLE_CNT//VID_DUR_TIME print(SAMPLE_CNT,AUDIO_FRAME_RATE) with wave.open('badapple.wav',mode='wb') as f: f.setparams((2,2,AUDIO_FRAME_RATE,0,"NONE","NOT COMPRESSED")) f.writeframes((f_array*40).astype(np.int16))
print(VID_FRAMES*AVG_FRAMESIZE*2) cur_len=0#now pointer for i in frames: print(i) # procimg('raw/'+i) frame_len=(frame_data:=procimg('raw/'+i)).__len__() f_array[cur_len:cur_len+frame_len]=frame_data print(f'set f_array[{cur_len}:{cur_len+frame_len}]') cur_len+=frame_len SAMPLE_CNT=len(f_array)/2 AUDIO_FRAME_RATE=SAMPLE_CNT//VID_DUR_TIME print(SAMPLE_CNT,AUDIO_FRAME_RATE) with wave.open('badapple.wav',mode='wb') as f: f.setparams((2,2,AUDIO_FRAME_RATE,0,"NONE","NOT COMPRESSED")) f.writeframes((np.array(f_array)*40).astype(np.int16))