Exemplo n.º 1
0
    def _download_gif(self,
                      file: telegram.File,
                      channel_id: str = "") -> Tuple[IO[bytes], str, str, str]:
        """
        Download and convert GIF image.

        Args:
            file: Telegram File object
            channel_id: Destination channel ID of the message

        Returns:
            Tuple[IO[bytes], str, str, str]:
                ``tempfile`` file-like object, MIME type, proposed file name
        """
        file, _, filename, path = self._download_file(file, 'video/mpeg')
        gif_file = tempfile.NamedTemporaryFile(suffix='.gif')
        v = VideoFileClip(path)
        self.logger.info(
            "Convert Telegram MP4 to GIF from "
            "channel %s with size %s", channel_id, v.size)
        if channel_id == "blueset.wechat" and v.size[0] > 600:
            # Workaround: Compress GIF for slave channel `blueset.wechat`
            # TODO: Move this logic to `blueset.wechat` in the future
            subprocess.Popen([
                "ffmpeg", "-y", "-i", path, '-vf', "scale=600:-2",
                gif_file.name
            ],
                             bufsize=0).wait()
        else:
            v.write_gif(gif_file.name, program="ffmpeg")
        file.close()
        gif_file.seek(0)
        return gif_file, "image/gif", os.path.basename(
            gif_file.name), gif_file.name
Exemplo n.º 2
0
    def _download_gif(self,
                      file: telegram.File) -> Tuple[IO[bytes], str, str, str]:
        """
        Download and convert GIF image.

        Args:
            file: Telegram File object

        Returns:
            Tuple[IO[bytes], str, str, str]:
                ``tempfile`` file-like object, MIME type, proposed file name
        """
        file, _, filename, path = self._download_file(file, 'video/mpeg')
        gif_file = tempfile.NamedTemporaryFile(suffix='.gif')
        VideoFileClip(path).write_gif(gif_file.name, program="ffmpeg")
        file.close()
        gif_file.seek(0)
        return gif_file, "image/gif", os.path.basename(
            gif_file.name), gif_file.name