Forráskód Böngészése

voice messages: use ffmpeg conversion functions from mautrix-python

Sumner Evans 3 éve
szülő
commit
c22b5f56be

+ 10 - 3
mautrix_signal/portal.py

@@ -68,6 +68,7 @@ from mautrix.types import (
     VideoInfo,
 )
 from mautrix.util.bridge_state import BridgeStateEvent
+from mautrix.util.ffmpeg import convert_bytes, convert_path
 from mautrix.util.format_duration import format_duration
 from mautrix.util.message_send_checkpoint import MessageSendCheckpointStatus
 
@@ -80,7 +81,7 @@ from .db import (
     Reaction as DBReaction,
 )
 from .formatter import matrix_to_signal, signal_to_matrix
-from .util import id_to_str, to_m4a, to_ogg
+from .util import id_to_str
 
 if TYPE_CHECKING:
     from .__main__ import SignalBridge
@@ -253,7 +254,11 @@ class Portal(DBPortal, BasePortal):
     async def _make_attachment(message: MediaMessageEventContent, path: str) -> Attachment:
         outgoing_filename = path
         if message.msgtype == MessageType.AUDIO:
-            outgoing_filename = await to_m4a(path)
+            outgoing_filename = (
+                (await convert_path(path, ".m4a", output_args=("-c:a", "aac"), remove_input=True))
+                .absolute()
+                .as_posix()
+            )
             message.info.mimetype = "audio/mp4"
         attachment = Attachment(
             custom_filename=message.body,
@@ -877,7 +882,9 @@ class Portal(DBPortal, BasePortal):
                 **(content.info.serialize() if content.info else {}),
             }
             content["org.matrix.msc3245.voice"] = {}
-            data = await to_ogg(data, attachment.content_type)
+            data = await convert_bytes(
+                data, ".ogg", output_args=("-c:a", "libvorbis"), input_mime=attachment.content_type
+            )
 
         return content, data
 

+ 0 - 1
mautrix_signal/util/__init__.py

@@ -1,3 +1,2 @@
-from .audio_convert import to_m4a, to_ogg
 from .color_log import ColorFormatter
 from .id_to_str import id_to_str

+ 0 - 83
mautrix_signal/util/audio_convert.py

@@ -1,83 +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
-from pathlib import Path
-import asyncio
-import logging
-import mimetypes
-import os
-import shutil
-import tempfile
-
-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="mxsignal_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})"
-            )
-            raise ChildProcessError(f"ffmpeg error: {err_text}")
-
-
-async def to_m4a(path_str: str) -> Optional[str]:
-    path = Path(path_str)
-    output_file_name = (path.parent / (path.stem + ".m4a")).absolute().as_posix()
-    proc = await asyncio.create_subprocess_exec(
-        "ffmpeg",
-        "-i",
-        path,
-        "-c:a",
-        "aac",
-        output_file_name,
-        stdout=asyncio.subprocess.PIPE,
-        stdin=asyncio.subprocess.PIPE,
-    )
-    _, stderr = await proc.communicate()
-    if proc.returncode == 0:
-        path.unlink(missing_ok=True)
-        return output_file_name
-    else:
-        err_text = stderr.decode("utf-8") if stderr is not None else f"unknown ({proc.returncode})"
-        raise ChildProcessError(f"ffmpeg error: {err_text}")