|
import streamlit as st |
|
import pyaudio |
|
import wave |
|
import os |
|
import tempfile |
|
|
|
|
|
FORMAT = pyaudio.paInt16 |
|
CHANNELS = 1 |
|
RATE = 44100 |
|
CHUNK = 1024 |
|
|
|
|
|
audio = pyaudio.PyAudio() |
|
|
|
|
|
st.title("Простой диктофон") |
|
|
|
|
|
record_seconds = st.number_input("Введите продолжительность записи (в секундах):", min_value=1, value=5) |
|
|
|
|
|
if st.button("Начать запись"): |
|
|
|
stream = audio.open(format=FORMAT, channels=CHANNELS, |
|
rate=RATE, input=True, |
|
frames_per_buffer=CHUNK) |
|
st.write("Запись...") |
|
frames = [] |
|
|
|
|
|
for i in range(0, int(RATE / CHUNK * record_seconds)): |
|
data = stream.read(CHUNK) |
|
frames.append(data) |
|
|
|
st.write("Запись завершена.") |
|
|
|
|
|
stream.stop_stream() |
|
stream.close() |
|
audio.terminate() |
|
|
|
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio: |
|
with wave.open(temp_audio.name, 'wb') as wf: |
|
wf.setnchannels(CHANNELS) |
|
wf.setsampwidth(audio.get_sample_size(FORMAT)) |
|
wf.setframerate(RATE) |
|
wf.writeframes(b''.join(frames)) |
|
|
|
st.write("Аудио сохранено.") |
|
|
|
|
|
with open(temp_audio.name, "rb") as f: |
|
audio_bytes = f.read() |
|
st.audio(audio_bytes, format='audio/wav') |
|
|
|
|
|
file_name = st.text_input("Введите имя файла для сохранения:", value="recorded_audio.wav") |
|
if st.button("Сохранить файл"): |
|
with open(file_name, "wb") as f: |
|
f.write(audio_bytes) |
|
st.write(f"Файл сохранен как {file_name}") |
|
|
|
|
|
os.remove(temp_audio.name) |