Encode facebook VP9 videos to H264 (not played in Telegram on iOS)

New environment variable in envs/.env_worker: FACEBOOK_ENCODE_TO_H264=True
This commit is contained in:
Dmytro Vlasiuk 2024-08-10 15:42:01 +03:00
parent 8601925c26
commit aaa26259fd
5 changed files with 48 additions and 0 deletions

View file

@ -8,6 +8,7 @@ class WorkerSettings(Settings):
STORAGE_PATH: DirectoryPath
THUMBNAIL_FRAME_SECOND: float
INSTAGRAM_ENCODE_TO_H264: bool
FACEBOOK_ENCODE_TO_H264: bool
settings = WorkerSettings()

View file

@ -2,10 +2,12 @@ from ytdl_opts.per_host._default import DefaultHost
from ytdl_opts.per_host.instagram import InstagramHost
from ytdl_opts.per_host.tiktok import TikTokHost
from ytdl_opts.per_host.twitter import TwitterHost
from ytdl_opts.per_host.facebook import FacebookHost
__all__ = [
'DefaultHost',
'InstagramHost',
'TikTokHost',
'TwitterHost',
'FacebookHost',
]

View file

@ -0,0 +1,39 @@
from pathlib import Path
from yt_shared.constants import FACEBOOK_HOSTS
from yt_shared.enums import DownMediaType
from worker.core.config import settings
from ytdl_opts.per_host._base import AbstractHostConfig, BaseHostConfModel
from ytdl_opts.per_host._registry import HostConfRegistry
class FacebookHostModel(BaseHostConfModel):
pass
class FacebookHost(AbstractHostConfig, metaclass=HostConfRegistry):
ALLOW_NULL_HOSTNAMES = False
HOSTNAMES = FACEBOOK_HOSTS
ENCODE_AUDIO = False
ENCODE_VIDEO = settings.FACEBOOK_ENCODE_TO_H264
FFMPEG_AUDIO_OPTS = None
# Facebook returns VP9+AAC in MP4 container for logged users and needs to be
# encoded to H264 since Telegram doesn't play VP9 on iOS.
FFMPEG_VIDEO_OPTS = 'ffmpeg -y -loglevel error -i "{filepath}" -c:v libx264 -pix_fmt yuv420p -preset veryfast -crf 22 -movflags +faststart -c:a copy "{output}"'
def build_config(
self, media_type: DownMediaType, curr_tmp_dir: Path
) -> FacebookHostModel:
return FacebookHostModel(
hostnames=self.HOSTNAMES,
encode_audio=self.ENCODE_AUDIO,
encode_video=self.ENCODE_VIDEO,
ffmpeg_audio_opts=self.FFMPEG_AUDIO_OPTS,
ffmpeg_video_opts=self.FFMPEG_VIDEO_OPTS,
ytdl_opts=self._build_ytdl_opts(media_type, curr_tmp_dir),
)
def _build_custom_ytdl_video_opts(self) -> list[str]:
return self.DEFAULT_VIDEO_FORMAT_SORT_OPT

View file

@ -5,3 +5,4 @@ STORAGE_PATH=/filestorage
THUMBNAIL_FRAME_SECOND=10.0
INSTAGRAM_ENCODE_TO_H264=True
FACEBOOK_ENCODE_TO_H264=True

View file

@ -21,5 +21,10 @@ TWITTER_HOSTS = (
't.co',
'www.t.co',
)
FACEBOOK_HOSTS = (
'facebook.com',
'www.facebook.com',
)
REMOVE_QUERY_PARAMS_HOSTS = TWITTER_HOSTS + INSTAGRAM_HOSTS