This commit is contained in:
Robert 2024-03-24 00:54:01 +07:00
parent fe87afe9d0
commit 94c7f98ce8
No known key found for this signature in database
GPG Key ID: F631C7FD957D5F22
2 changed files with 11 additions and 18 deletions

View File

@ -10,21 +10,21 @@ from io import BytesIO
class AudioConverterService: class AudioConverterService:
@sync_to_async @sync_to_async
def __mp3_to_ogg(self, mp3: BytesIO) -> str: def __audio_to_ogg(self, audio: BytesIO) -> str:
filename = f"./audios/{uuid4()}.ogg" filename = f"./audios/{uuid4()}.ogg"
process = ( process = (
ffmpeg.input("pipe:") ffmpeg.input("pipe:")
.output(filename, loglevel="quiet") .output(filename, codec="libopus", loglevel="quiet")
.overwrite_output() .overwrite_output()
.run_async(pipe_stdin=True) .run_async(pipe_stdin=True)
) )
process.communicate(input=mp3.getbuffer()) process.communicate(input=audio.getbuffer())
return filename return filename
async def mp3_to_ogg(self, mp3: BytesIO) -> BytesIO: async def audio_to_ogg(self, audio: BytesIO) -> BytesIO:
filename = await self.__mp3_to_ogg(mp3) filename = await self.__audio_to_ogg(audio)
async with aiofiles.open(filename, "rb") as file: async with aiofiles.open(filename, "rb") as file:
content = BytesIO(initial_bytes=await file.read()) content = BytesIO(initial_bytes=await file.read())

View File

@ -41,20 +41,13 @@ class FileUploaderService:
if uploaded_file: if uploaded_file:
return return
if file_url.endswith(".mp3") or file_url.endswith(".ogg"): converted_content = await self.audio_converter_service.audio_to_ogg(
audio=BytesIO((await self.client.get(file_url)).read())
)
converted_content = await self.audio_converter_service.mp3_to_ogg( uploaded_file_id = await self.planfix_client.upload_file(
mp3=BytesIO((await self.client.get(file_url)).read()) file_content=converted_content
) )
uploaded_file_id = await self.planfix_client.upload_file(
file_content=converted_content
)
else:
uploaded_file_id = await self.planfix_client.upload_file_from_url(
file_url
)
await self.uploaded_file_repository.upsert( await self.uploaded_file_repository.upsert(
UploadedFile(file_id=uploaded_file_id, file_url=file_url) UploadedFile(file_id=uploaded_file_id, file_url=file_url)