diff --git a/README.md b/README.md index 94f21fb..4debd66 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,14 @@ youtube = "mov-cli-youtube" mov-cli -s youtube nyan cat ``` -### Audio Only 🔉 +### Scraper Options ⚙️ + +#### Audio Only 🔉 ```sh mov-cli -s youtube nyan cat -- --audio ``` + +#### Allow Shorts 🖼️ +```sh +mov-cli -s youtube The otter begging is adorable -- --shorts +``` \ No newline at end of file diff --git a/mov_cli_youtube/__init__.py b/mov_cli_youtube/__init__.py index 21a6849..9399c63 100644 --- a/mov_cli_youtube/__init__.py +++ b/mov_cli_youtube/__init__.py @@ -22,4 +22,4 @@ } } -__version__ = "1.3.2" \ No newline at end of file +__version__ = "1.3.4" \ No newline at end of file diff --git a/mov_cli_youtube/yt_dlp.py b/mov_cli_youtube/yt_dlp.py index 9ea6b04..495dbb7 100644 --- a/mov_cli_youtube/yt_dlp.py +++ b/mov_cli_youtube/yt_dlp.py @@ -80,16 +80,21 @@ def scrape( url = self.__get_best_stream(info, audio = True) else: url = self.__get_best_stream(info, video = True) - audio_url = self.__get_best_stream(info, audio = True) - for lang_code, caption_data in info.get("automatic_captions", {}).items(): - if lang_code == self.config.language.iso639_1: + audio_url = self.__get_best_stream( + info, + audio = True, + ensure_correct_audio_localisation = self.options.get("disable_audio_l10n", True) + ) + + for lang_code, caption_data in info.get("subtitles", {}).items(): + if lang_code.startswith(self.config.language.iso639_1): for caption in caption_data: if caption.get("ext") == "vtt": subtitles.append(caption.get("url")) - for lang_code, caption_data in info.get("subtitles", {}).items(): - if lang_code.startswith(self.config.language.iso639_1): + for lang_code, caption_data in info.get("automatic_captions", {}).items(): + if lang_code == self.config.language.iso639_1: for caption in caption_data: if caption.get("ext") == "vtt": subtitles.append(caption.get("url")) @@ -102,23 +107,41 @@ def scrape( subtitles = subtitles ) - def __get_best_stream(self, ytdlp_info: dict, video: bool = False, audio: bool = False) -> str: + def __get_best_stream(self, ytdlp_info: dict, video: bool = False, audio: bool = False, ensure_correct_audio_localisation: bool = True) -> str: """Returns the best stream respecting the parameters given.""" stream_formats_to_sort: List[Tuple[int, str]] = [] + if video is False and audio is False: + raise ValueError("Either video or audio arg must be True in '__get_best_stream'!") + for stream_format in ytdlp_info["formats"]: if video is True and stream_format["video_ext"] == "none": continue - if audio is True and stream_format["audio_ext"] == "none": - continue + if audio is True: + + if stream_format["audio_ext"] == "none": + continue + + if ensure_correct_audio_localisation and not stream_format["language"] == self.config.language.iso639_1: + continue url: str = stream_format["url"] quality: int = stream_format["quality"] stream_formats_to_sort.append((quality, url)) + # To absolutely ensure this shit doesn't blow up. + if len(stream_formats_to_sort) == 0 and audio is True: + self.logger.warning( + "Couldn't find the right audio for your currently selected language so audio " \ + "localisation will be disabled and the first audio from the YouTube video will be selected." + ) + return self.__get_best_stream( + ytdlp_info, audio = True, ensure_correct_audio_localisation = False + ) + stream_formats_to_sort.sort(key = lambda x: x[0], reverse = True) return stream_formats_to_sort[0][1]