feat: Add file size check and mp3 conversion for large audio files

This commit is contained in:
Paul Gauthier (aider) 2025-01-04 11:59:42 -08:00
parent 9b46991721
commit 73837730fa

View file

@ -140,10 +140,20 @@ class Voice:
while not self.q.empty():
file.write(self.q.get())
# ai: File uploads are currently limited to 25 MB
# check if the format is wav and the file is >25mb
# if so, offer to switch to mp3 format. ai!
if self.audio_format != "wav":
# Check file size and offer to convert to mp3 if too large
file_size = os.path.getsize(temp_wav)
if file_size > 25 * 1024 * 1024: # 25 MB
print("\nWarning: Recording is too large for direct upload (over 25MB)")
if input("Convert to mp3 to reduce size? [Y/n] ").lower() in ("", "y"):
filename = tempfile.mktemp(suffix=".mp3")
audio = AudioSegment.from_wav(temp_wav)
audio.export(filename, format="mp3")
os.remove(temp_wav)
print(f"Converted to mp3, new size: {os.path.getsize(filename)/1024/1024:.1f}MB")
else:
print("Uploading large file - may fail if over 25MB")
filename = temp_wav
elif self.audio_format != "wav":
filename = tempfile.mktemp(suffix=f".{self.audio_format}")
audio = AudioSegment.from_wav(temp_wav)
audio.export(filename, format=self.audio_format)