使用pyaudio点击按钮记录大部分音频

时间:2018-02-21 11:49:12

标签: audio-recording pyaudio

我想将大型音频文件剪切成不同的片段,并使用pyaudio以WAV格式存储它们。我基本上需要听音频,然后将文件从起点切割到我想要切割的位置,然后再次开始录制并剪切另一部分,但我不知道我怎么能用pyaudio来做。我在寻找替代图书馆吗? 我是python的新手,任何形式的帮助都会很明显。

这是代码,我已经尝试过:

import pyaudio
import wave
import time

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
WAVE_OUTPUT_FILENAME = "output.wav"

wf = wave.open("A001017001_Edited.wav", 'rb')
p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
            channels=CHANNELS,
            rate=RATE,
            input=True,
            frames_per_buffer=CHUNK)

check = True;
While(check):
 start = input("Do you wish to start recording?,then press ENTER")
 if (start == 13):
    try:
     stream.start_stream()
     p = time.time()
     kdata = wf.readframes(CHUNK)
     while len(kdata) > 0:
      stream.write(kdata)
      kdata = wf.readframes(CHUNK)
    except KeyboardInterrupt:
        q = time.time()

 RECORD_SECONDS = (q-p); #gets time since wave file is played
 frames = []
 for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)

 print(int(RATE / CHUNK * RECORD_SECONDS))
 print("stopped recording")
 stream.stop_stream()
 wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
 wf.setnchannels(CHANNELS)
 wf.setsampwidth(p.get_sample_size(FORMAT))
 wf.setframerate(RATE)
 wf.writeframes(b''.join(frames))
                             #compare if the whole audio is listened 
                              #or not and
                              #if yes return false
stream.close()
p.terminate()
wf.close()

0 个答案:

没有答案