Skip to content

Commit

Permalink
Merge pull request #19 from mov-cli/fix/return-correct-audio-track
Browse files Browse the repository at this point in the history
Fix/return correct audio track
  • Loading branch information
THEGOLDENPRO authored Aug 5, 2024
2 parents 3c91579 + dc0e743 commit e6c3d1e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
2 changes: 1 addition & 1 deletion mov_cli_youtube/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
}
}

__version__ = "1.3.2"
__version__ = "1.3.4"
39 changes: 31 additions & 8 deletions mov_cli_youtube/yt_dlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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]

Expand Down

0 comments on commit e6c3d1e

Please sign in to comment.