Properly take advantage of HW accel

This commit is contained in:
Nexus 2024-04-15 22:28:38 +01:00
parent e70056f2ff
commit 751287479a
Signed by: nex
GPG key ID: 0FA334385D0B689F

View file

@ -2,6 +2,7 @@ import asyncio
import pathlib
import subprocess
import aiohttp
import discord
import re
import tempfile
@ -66,16 +67,46 @@ class AutoResponder(commands.Cog):
else:
self.log.info("No HEVC streams found in %s", uri)
return
extension = pathlib.Path(uri).suffix
with tempfile.NamedTemporaryFile(suffix=extension) as tmp_dl:
self.log.info("Downloading %r to %r", uri, tmp_dl.name)
async with aiohttp.ClientSession(
headers={
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,"
"*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "none",
"Sec-Fetch-User": "?1",
"Sec-Gpc": "1",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0",
}
) as session:
async with session.get(uri) as resp:
resp.raise_for_status()
async for chunk in resp.content.iter_any():
# noinspection PyTypeChecker
tmp_dl.write(chunk)
self.log.info("Finished downloading %r to %r", uri, tmp_dl.name)
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp:
tmp_path = pathlib.Path(tmp.name)
self.log.info("Transcoding %r to %r", uri, tmp_path)
args = [
"-i", str(uri),
"-hide_banner",
"-hwaccel", "auto",
"-i", tmp_dl.name,
"-c:v", "libx264",
"-crf", "25",
"-c:a", "libopus",
"-b:a", "64k",
"-preset", "slower",
"-preset", "faster",
"-vsync", "2",
"-pix_fmt", "yuv420p",
"-movflags", "faststart",
"-profile:v", "main",
"-y"
]
process = await asyncio.create_subprocess_exec(
@ -104,14 +135,26 @@ class AutoResponder(commands.Cog):
return
# Check for HEVC truth social links and convert into h264
if message.channel.name == "spam" and message.author.id in {1101439218334576742, 1229496078726860921}:
links = self.extract_links(message.content, "static-assets-1.truthsocial.com")
if links:
for link in links:
if link.lower().endswith(
(".mp4", ".mov", ".qtff", ".mkv", ".asf", ".avi", ".mxf", ".ps", ".ts", ".3gp", ".3g2")
):
# All containers allowed to contain HEVC
hevc_containers = {
".mp4",
".mov",
".qtff",
".mkv",
".asf",
".avi",
".mxf",
".ps",
".ts",
".3gp",
".3g2"
}
# ^ All containers allowed to contain HEVC
# per https://en.wikipedia.org/wiki/Comparison_of_video_container_formats
if link.lower().endswith(tuple(hevc_containers)):
self.log.info("Found link to transcode: %r", link)
try:
async with message.channel.typing():