encode videos to mp4 and upload to s3

This commit is contained in:
2023-06-01 00:32:11 +03:00
parent d10f9a52be
commit 7384497a02
4 changed files with 170 additions and 49 deletions

36
main.py
View File

@@ -2,15 +2,20 @@ import asyncio
import logging
import os
from io import BytesIO
from pathlib import Path
from tempfile import TemporaryFile, TemporaryDirectory
from time import time
from typing import List
import boto3
import ffmpeg
from PIL import Image
import httpx
import redis.asyncio as aioredis
from aiogram import Bot, Dispatcher
from aiogram.dispatcher import filters
from aiogram.types import Message, ParseMode, ChatActions
from aiogram.types import Message, ParseMode, ChatActions, InputFile
from aiogram.utils import executor, exceptions
import dotenv
@@ -50,18 +55,24 @@ async def send_post(post: E621Post, tag_list: List[str]):
file.name = f'file.{post.file.ext}'
file.seek(0)
if post.file.ext == 'webm':
if post.file.size < 50_000_000:
await bot.send_video(int(os.environ['SEND_CHAT']),
file,
width=post.file.width,
height=post.file.height,
thumb=post.preview.url,
caption=caption,
parse_mode=ParseMode.HTML)
else:
with TemporaryDirectory() as td:
webm_path = Path(td) / 'video.webm'
mp4_path = Path(td) / 'video.mp4'
with open(webm_path, 'wb') as webm:
webm.write(file.read())
ffmpeg\
.input(str(webm_path))\
.output(str(mp4_path), vcodec='libx264', crf='26')\
.run()
s3 = boto3.client('s3', aws_access_key_id=os.environ['AWS_ACCESS_KEY'], aws_secret_access_key=os.environ['AWS_SECRET_KEY'])
bucket = os.environ['AWS_S3_BUCKET']
upload_filename = f'e621-{post.id}-{int(time())}.mp4'
s3.upload_file(mp4_path, bucket, upload_filename, ExtraArgs={'ACL': 'public-read', 'ContentType': 'video/mp4'})
await bot.send_message(int(os.environ['SEND_CHAT']),
f'File is too large: {post.file.url}\n\n' + caption,
f'https://{bucket}.s3.amazonaws.com/{upload_filename}\n\n' + caption,
parse_mode=ParseMode.HTML)
webm_path.unlink()
mp4_path.unlink()
elif post.file.ext == 'gif':
await bot.send_animation(int(os.environ['SEND_CHAT']),
file,
@@ -180,7 +191,8 @@ async def test(msg: Message):
if not args:
await msg.reply('Please provide post id')
return
post = await e621.get_posts(f'id:{args[0]}')
post = await e621.get_posts(f'id:{args}')
print(f'id:{args}')
if not post:
await msg.reply('Post not found')
return