From 43101afad45bef3a8b64d6488923e16e5d80db00 Mon Sep 17 00:00:00 2001
From: Pavel Kirilin <win10@list.ru>
Date: Mon, 21 Jun 2021 00:37:49 +0400
Subject: [PATCH] Added video file size reductor.

Signed-off-by: Pavel Kirilin <win10@list.ru>
---
 .pre-commit-config.yaml               |  6 ++++
 bot_s3rius/actions/__init__.py        |  1 +
 bot_s3rius/actions/video_converter.py | 45 +++++++++++++++++++++++++++
 deploy/Dockerfiles/Dockerfile         |  2 +-
 deploy/Dockerfiles/test.Dockerfile    |  2 +-
 poetry.lock                           | 29 ++++++++++++++++-
 pyproject.toml                        |  1 +
 7 files changed, 83 insertions(+), 3 deletions(-)
 create mode 100644 bot_s3rius/actions/video_converter.py

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 7f55576..b486db8 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -14,6 +14,12 @@ repos:
         types: [python]
         entry: isort
 
+      - id: black
+        name: black
+        language: system
+        types: [python]
+        entry: black
+
       - id: flake8
         name: flake8
         language: system
diff --git a/bot_s3rius/actions/__init__.py b/bot_s3rius/actions/__init__.py
index 5e6c7ea..6ad524f 100644
--- a/bot_s3rius/actions/__init__.py
+++ b/bot_s3rius/actions/__init__.py
@@ -10,4 +10,5 @@ def finish():
     importlib.import_module("bot_s3rius.actions.converters")
     importlib.import_module("bot_s3rius.actions.search_engines")
     importlib.import_module("bot_s3rius.actions.package_indexes")
+    importlib.import_module("bot_s3rius.actions.video_converter")
     logging.info("All actions loaded")
diff --git a/bot_s3rius/actions/video_converter.py b/bot_s3rius/actions/video_converter.py
new file mode 100644
index 0000000..988ab0d
--- /dev/null
+++ b/bot_s3rius/actions/video_converter.py
@@ -0,0 +1,45 @@
+import tempfile
+import uuid
+from pathlib import Path
+
+from ffmpeg import FFmpeg
+from telethon import events
+from telethon.tl.custom import Message
+
+from bot_s3rius.config import config
+from bot_s3rius.utils.responses import mark_unread
+
+
+@config.telegram_client.on(events.NewMessage(pattern=r"^\.reduce_vid_size"))
+@mark_unread
+async def find_py_package(event: events.NewMessage.Event):
+    video_message: Message = await event.message.get_reply_message()
+    video = video_message.video
+    if video is None:
+        await event.reply("Я не нашёл тут видео.")
+        return
+
+    await event.reply("Сща всё тут поправлю.")
+
+    tmp_dir = Path(tempfile.gettempdir())
+    converted_file = tmp_dir / f"{uuid.uuid4().hex}.mp4"
+    file = await config.telegram_client.download_media(
+        video_message,
+    )
+
+    video_path = Path(file)
+
+    await FFmpeg().input(str(video_path)).output(
+        str(converted_file), {"crf": 35}
+    ).execute()
+
+    reduced_size = (converted_file.stat().st_size / video_path.stat().st_size) * 100
+
+    await event.reply(
+        f"Reduced {reduced_size}%.",
+        video=converted_file,
+        supports_streaming=True,
+    )
+
+    video_path.unlink()
+    converted_file.unlink()
diff --git a/deploy/Dockerfiles/Dockerfile b/deploy/Dockerfiles/Dockerfile
index b81f431..9c94c12 100644
--- a/deploy/Dockerfiles/Dockerfile
+++ b/deploy/Dockerfiles/Dockerfile
@@ -2,7 +2,7 @@ FROM python:3.9-alpine3.13
 
 RUN adduser --disabled-password bot
 
-RUN apk add --no-cache curl
+RUN apk add --no-cache curl ffmpeg
 
 ENV POETRY_VERSION 1.1.6
 
diff --git a/deploy/Dockerfiles/test.Dockerfile b/deploy/Dockerfiles/test.Dockerfile
index c0b27c0..2bbf32b 100644
--- a/deploy/Dockerfiles/test.Dockerfile
+++ b/deploy/Dockerfiles/test.Dockerfile
@@ -1,6 +1,6 @@
 FROM python:3.9-alpine3.13
 
-RUN apk add --no-cache curl gcc musl-dev
+RUN apk add --no-cache curl gcc musl-dev ffmpeg
 ENV POETRY_VERSION 1.1.6
 
 RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
diff --git a/poetry.lock b/poetry.lock
index a904608..e8619c4 100644
--- a/poetry.lock
+++ b/poetry.lock
@@ -372,6 +372,14 @@ typing-extensions = ">=3.7.4.3"
 dotenv = ["python-dotenv (>=0.10.4)"]
 email = ["email-validator (>=1.0.3)"]
 
+[[package]]
+name = "pyee"
+version = "8.1.0"
+description = "A port of node.js's EventEmitter to python."
+category = "main"
+optional = false
+python-versions = "*"
+
 [[package]]
 name = "pyflakes"
 version = "2.3.1"
@@ -439,6 +447,17 @@ toml = "*"
 [package.extras]
 testing = ["fields", "hunter", "process-tests", "six", "pytest-xdist", "virtualenv"]
 
+[[package]]
+name = "python-ffmpeg"
+version = "1.0.11"
+description = "A python interface for FFmpeg"
+category = "main"
+optional = false
+python-versions = "*"
+
+[package.dependencies]
+pyee = "*"
+
 [[package]]
 name = "pytz"
 version = "2021.1"
@@ -567,7 +586,7 @@ watchdog = ["watchdog"]
 [metadata]
 lock-version = "1.1"
 python-versions = "^3.9"
-content-hash = "49ee7fc2622e2d548e496f801a6e87d68f8117c8921998687b05c7dd5d9bc070"
+content-hash = "fb7714f2bd4ebfb06543853501b0458d42f09582253173ebdf296e0db9246d73"
 
 [metadata.files]
 appdirs = [
@@ -822,6 +841,10 @@ pydantic = [
     {file = "pydantic-1.8.2-py3-none-any.whl", hash = "sha256:fec866a0b59f372b7e776f2d7308511784dace622e0992a0b59ea3ccee0ae833"},
     {file = "pydantic-1.8.2.tar.gz", hash = "sha256:26464e57ccaafe72b7ad156fdaa4e9b9ef051f69e175dbbb463283000c05ab7b"},
 ]
+pyee = [
+    {file = "pyee-8.1.0-py2.py3-none-any.whl", hash = "sha256:383973b63ad7ed5e3c0311f8b179c52981f9e7b3eaea0e9a830d13ec34dde65f"},
+    {file = "pyee-8.1.0.tar.gz", hash = "sha256:92dacc5bd2bdb8f95aa8dd2585d47ca1c4840e2adb95ccf90034d64f725bfd31"},
+]
 pyflakes = [
     {file = "pyflakes-2.3.1-py2.py3-none-any.whl", hash = "sha256:7893783d01b8a89811dd72d7dfd4d84ff098e5eed95cfa8905b22bbffe52efc3"},
     {file = "pyflakes-2.3.1.tar.gz", hash = "sha256:f5bc8ecabc05bb9d291eb5203d6810b49040f6ff446a756326104746cc00c1db"},
@@ -842,6 +865,10 @@ pytest-cov = [
     {file = "pytest-cov-2.12.1.tar.gz", hash = "sha256:261ceeb8c227b726249b376b8526b600f38667ee314f910353fa318caa01f4d7"},
     {file = "pytest_cov-2.12.1-py2.py3-none-any.whl", hash = "sha256:261bb9e47e65bd099c89c3edf92972865210c36813f80ede5277dceb77a4a62a"},
 ]
+python-ffmpeg = [
+    {file = "python-ffmpeg-1.0.11.tar.gz", hash = "sha256:bdf38ba5052f7128241a217a4411664e1047afa959416b30f133a3a349428e4c"},
+    {file = "python_ffmpeg-1.0.11-py3-none-any.whl", hash = "sha256:4eb946e7457ff5db9ce1cbe138ecdbe53e7bfe4a15a78b39051fba6b64919f36"},
+]
 pytz = [
     {file = "pytz-2021.1-py2.py3-none-any.whl", hash = "sha256:eb10ce3e7736052ed3623d49975ce333bcd712c7bb19a58b9e2089d4057d0798"},
     {file = "pytz-2021.1.tar.gz", hash = "sha256:83a4a90894bf38e243cf052c8b58f381bfe9a7a483f6a9cab140bc7f702ac4da"},
diff --git a/pyproject.toml b/pyproject.toml
index 76e1005..46e23d2 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -12,6 +12,7 @@ pydantic = "^1.8.2"
 pytz = "^2021.1"
 Telethon = "1.14.0"
 httpx = "^0.18.1"
+python-ffmpeg = "^1.0.11"
 
 [tool.poetry.dev-dependencies]
 pytest = "^6.2.4"
-- 
GitLab