Properly take advantage of HW accel
This commit is contained in:
parent
e70056f2ff
commit
751287479a
1 changed files with 99 additions and 56 deletions
|
@ -2,6 +2,7 @@ import asyncio
|
||||||
import pathlib
|
import pathlib
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
import aiohttp
|
||||||
import discord
|
import discord
|
||||||
import re
|
import re
|
||||||
import tempfile
|
import tempfile
|
||||||
|
@ -66,16 +67,46 @@ class AutoResponder(commands.Cog):
|
||||||
else:
|
else:
|
||||||
self.log.info("No HEVC streams found in %s", uri)
|
self.log.info("No HEVC streams found in %s", uri)
|
||||||
return
|
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:
|
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp:
|
||||||
tmp_path = pathlib.Path(tmp.name)
|
tmp_path = pathlib.Path(tmp.name)
|
||||||
self.log.info("Transcoding %r to %r", uri, tmp_path)
|
self.log.info("Transcoding %r to %r", uri, tmp_path)
|
||||||
args = [
|
args = [
|
||||||
"-i", str(uri),
|
"-hide_banner",
|
||||||
|
"-hwaccel", "auto",
|
||||||
|
"-i", tmp_dl.name,
|
||||||
"-c:v", "libx264",
|
"-c:v", "libx264",
|
||||||
"-crf", "25",
|
"-crf", "25",
|
||||||
"-c:a", "libopus",
|
"-c:a", "libopus",
|
||||||
"-b:a", "64k",
|
"-b:a", "64k",
|
||||||
"-preset", "slower",
|
"-preset", "faster",
|
||||||
|
"-vsync", "2",
|
||||||
|
"-pix_fmt", "yuv420p",
|
||||||
|
"-movflags", "faststart",
|
||||||
|
"-profile:v", "main",
|
||||||
"-y"
|
"-y"
|
||||||
]
|
]
|
||||||
process = await asyncio.create_subprocess_exec(
|
process = await asyncio.create_subprocess_exec(
|
||||||
|
@ -104,14 +135,26 @@ class AutoResponder(commands.Cog):
|
||||||
return
|
return
|
||||||
|
|
||||||
# Check for HEVC truth social links and convert into h264
|
# 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")
|
links = self.extract_links(message.content, "static-assets-1.truthsocial.com")
|
||||||
if links:
|
if links:
|
||||||
for link in links:
|
for link in links:
|
||||||
if link.lower().endswith(
|
hevc_containers = {
|
||||||
(".mp4", ".mov", ".qtff", ".mkv", ".asf", ".avi", ".mxf", ".ps", ".ts", ".3gp", ".3g2")
|
".mp4",
|
||||||
):
|
".mov",
|
||||||
# All containers allowed to contain HEVC
|
".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
|
# 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)
|
self.log.info("Found link to transcode: %r", link)
|
||||||
try:
|
try:
|
||||||
async with message.channel.typing():
|
async with message.channel.typing():
|
||||||
|
|
Loading…
Reference in a new issue