views.py
Код: Выделить всё
import functions.bytes_to_wav_wave
if voice_value:
# convert to bytes
file_content_bytes = voice_value.read()
wav_sound = bytes_to_wav_in_memory(file_content_bytes,1,2,10)
buffer = io.BytesIO(wav_sound)
try:
file_w = bytes_to_wav_wave(file_content_bytes,'output_wave.wav', 1, 2, 44100)
except Exception as e:
print(e)
with open('output_wave.wav', 'r') as file:
content = file.read()
functions.py
Код: Выделить всё
def bytes_to_wav_wave(audio_bytes, filename, nchannels, sampwidth, framerate):
"""
Converts raw audio bytes to a WAV file using the wave module.
Args:
audio_bytes (bytes): The raw audio data.
filename (str): The name of the output WAV file.
nchannels (int): Number of audio channels (e.g., 1 for mono, 2 for stereo).
sampwidth (int): Sample width in bytes (e.g., 2 for 16-bit audio).
framerate (int): Frame rate or sample rate in Hz (e.g., 44100).
"""
with wave.open(filename, 'wb') as wf:
wf.setnchannels(nchannels)
wf.setsampwidth(sampwidth)
wf.setframerate(framerate)
wf.writeframes(audio_bytes)
# Example usage:
# Assuming 'raw_audio_data' is your bytes object containing audio data
# bytes_to_wav_wave(raw_audio_data, 'output_wave.wav', 1, 2, 44100)
Views.py
Код: Выделить всё
try:
file_w = bytes_to_wav_wave(file_content_bytes,'output_wave.wav', 1, 2, 44100)
except Exception as e:
print(e)
try:
with open('output_wave.wav', 'r') as file:
content = file.read()
except Exception as e:
print("in try")
print(e)
**кодек 'utf-8' не может декодировать байт 0xc6 в позиции 4: неверный байт продолжения
**
Подробнее здесь: https://stackoverflow.com/questions/798 ... ntinuation
Мобильная версия