diff --git a/app_worker/worker/core/config.py b/app_worker/worker/core/config.py index c2cd1f2..40d3454 100644 --- a/app_worker/worker/core/config.py +++ b/app_worker/worker/core/config.py @@ -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() diff --git a/app_worker/ytdl_opts/per_host/__init__.py b/app_worker/ytdl_opts/per_host/__init__.py index e2e613f..d5971c8 100644 --- a/app_worker/ytdl_opts/per_host/__init__.py +++ b/app_worker/ytdl_opts/per_host/__init__.py @@ -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', ] diff --git a/app_worker/ytdl_opts/per_host/facebook.py b/app_worker/ytdl_opts/per_host/facebook.py new file mode 100644 index 0000000..ed168ba --- /dev/null +++ b/app_worker/ytdl_opts/per_host/facebook.py @@ -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 \ No newline at end of file diff --git a/envs/.env_worker b/envs/.env_worker index 6b364d0..64af3e0 100644 --- a/envs/.env_worker +++ b/envs/.env_worker @@ -5,3 +5,4 @@ STORAGE_PATH=/filestorage THUMBNAIL_FRAME_SECOND=10.0 INSTAGRAM_ENCODE_TO_H264=True +FACEBOOK_ENCODE_TO_H264=True diff --git a/yt_shared/yt_shared/constants.py b/yt_shared/yt_shared/constants.py index 5c1153c..bda391d 100644 --- a/yt_shared/yt_shared/constants.py +++ b/yt_shared/yt_shared/constants.py @@ -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