Переглянути джерело

voice messages: use convert function from mautrix-python

Sumner Evans 3 роки тому
батько
коміт
7f1197342c
2 змінених файлів з 3 додано та 69 видалено
  1. 3 6
      mautrix_instagram/portal.py
  2. 0 63
      mautrix_instagram/util/audio_convert.py

+ 3 - 6
mautrix_instagram/portal.py

@@ -35,9 +35,7 @@ from mautrix.types import (EventID, MessageEventContent, RoomID, EventType, Mess
                            ContentURI, LocationMessageEventContent, Format, UserID)
 from mautrix.errors import MatrixError, MForbidden, MNotFound, SessionNotFound
 from mautrix.util.simple_lock import SimpleLock
-
-from mautrix_instagram.util.audio_convert import to_ogg
-
+from mautrix.util.ffmpeg import convert_bytes
 
 from .db import Portal as DBPortal, Message as DBMessage, Reaction as DBReaction
 from .config import Config
@@ -468,9 +466,8 @@ class Portal(DBPortal, BasePortal):
     async def _reupload_instagram_voice(self, source: 'u.User', media: VoiceMediaItem,
                                         intent: IntentAPI) -> MediaMessageEventContent:
         async def convert_to_ogg(data, mimetype):
-            converted = await to_ogg(data, mimetype)
-            if converted is None:
-                raise ChildProcessError("Failed to convert to OGG")
+            converted = await convert_bytes(data, ".ogg", output_args=('-c:a', 'libvorbis'),
+                                            input_mime=mimetype)
             return converted, "audio/ogg"
 
         url = media.media.audio.audio_src

+ 0 - 63
mautrix_instagram/util/audio_convert.py

@@ -1,63 +0,0 @@
-# mautrix-instagram - A Matrix-Instagram puppeting bridge.
-# Copyright (C) 2021 Tulir Asokan, Sumner Evans
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU Affero General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU Affero General Public License for more details.
-#
-# You should have received a copy of the GNU Affero General Public License
-# along with this program.  If not, see <https://www.gnu.org/licenses/>.
-
-from typing import Optional
-import mimetypes
-import tempfile
-import logging
-import asyncio
-import shutil
-import os
-
-log = logging.getLogger("mau.util.audio")
-
-
-def abswhich(program: Optional[str]) -> Optional[str]:
-    if program is None:
-        return None
-    path = shutil.which(program)
-    return os.path.abspath(path) if path else None
-
-
-async def to_ogg(data: bytes, mime: str) -> Optional[bytes]:
-    default_ext = mimetypes.guess_extension(mime)
-    with tempfile.TemporaryDirectory(prefix="mxfb_audio_") as tmpdir:
-        input_file_name = os.path.join(tmpdir, f"input{default_ext}")
-        output_file_name = os.path.join(tmpdir, "output.ogg")
-        with open(input_file_name, "wb") as file:
-            file.write(data)
-        proc = await asyncio.create_subprocess_exec(
-            "ffmpeg",
-            "-i",
-            input_file_name,
-            "-c:a",
-            "libvorbis",
-            output_file_name,
-            stdout=asyncio.subprocess.PIPE,
-            stdin=asyncio.subprocess.PIPE,
-        )
-        _, stderr = await proc.communicate()
-        if proc.returncode == 0:
-            with open(output_file_name, "rb") as file:
-                return file.read()
-        else:
-            err_text = (
-                stderr.decode("utf-8")
-                if stderr is not None
-                else f"unknown ({proc.returncode})"
-            )
-            log.error(f"ffmpeg error: {err_text}")
-    return None