From 4fdd172b05cd675517155ffb2da6ed532db1cff7 Mon Sep 17 00:00:00 2001 From: meeb Date: Tue, 12 Dec 2023 14:40:23 +1100 Subject: [PATCH] tidy up and pass a serialised list through to youtube.download_media, may help with #362 --- tubesync/sync/models.py | 26 +++++++++++--------------- tubesync/sync/youtube.py | 20 +++++++++----------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/tubesync/sync/models.py b/tubesync/sync/models.py index e5c9221..356e779 100644 --- a/tubesync/sync/models.py +++ b/tubesync/sync/models.py @@ -18,7 +18,7 @@ from common.utils import clean_filename from .youtube import (get_media_info as get_youtube_media_info, download_media as download_youtube_media) from .utils import seconds_to_timestr, parse_media_format -from .matching import (get_best_combined_format, get_best_audio_format, +from .matching import (get_best_combined_format, get_best_audio_format, get_best_video_format) from .mediaservers import PlexMediaServer from .fields import CommaSepChoiceField @@ -107,7 +107,6 @@ class Source(models.Model): EXTENSION_MKV = 'mkv' EXTENSIONS = (EXTENSION_M4A, EXTENSION_OGG, EXTENSION_MKV) - # as stolen from: https://wiki.sponsor.ajay.app/w/Types / https://github.com/yt-dlp/yt-dlp/blob/master/yt_dlp/postprocessor/sponsorblock.py SPONSORBLOCK_CATEGORIES_CHOICES = ( ('sponsor', 'Sponsor'), @@ -121,15 +120,14 @@ class Source(models.Model): ) sponsorblock_categories = CommaSepChoiceField( - _(''), - possible_choices=SPONSORBLOCK_CATEGORIES_CHOICES, - all_choice="all", - allow_all=True, - all_label="(all options)", - default="all", - help_text=_("Select the sponsorblocks you want to enforce") - ) - + _(''), + possible_choices=SPONSORBLOCK_CATEGORIES_CHOICES, + all_choice='all', + allow_all=True, + all_label='(all options)', + default='all', + help_text=_('Select the sponsorblocks you want to enforce') + ) embed_metadata = models.BooleanField( _('embed metadata'), default=False, @@ -140,14 +138,12 @@ class Source(models.Model): default=False, help_text=_('Embed thumbnail into the file') ) - enable_sponsorblock = models.BooleanField( _('enable sponsorblock'), default=True, help_text=_('Use SponsorBlock?') ) - # Fontawesome icons used for the source on the front end ICONS = { SOURCE_TYPE_YOUTUBE_CHANNEL: '', @@ -1390,8 +1386,8 @@ class Media(models.Model): f'no valid format available') # Download the media with youtube-dl download_youtube_media(self.url, format_str, self.source.extension, - str(self.filepath), self.source.write_json, - self.source.sponsorblock_categories, self.source.embed_thumbnail, + str(self.filepath), self.source.write_json, + self.source.sponsorblock_categories.selected_choices, self.source.embed_thumbnail, self.source.embed_metadata, self.source.enable_sponsorblock, self.source.write_subtitles, self.source.auto_subtitles,self.source.sub_langs ) # Return the download paramaters diff --git a/tubesync/sync/youtube.py b/tubesync/sync/youtube.py index 4ac6e83..bf3e9f5 100644 --- a/tubesync/sync/youtube.py +++ b/tubesync/sync/youtube.py @@ -1,5 +1,5 @@ ''' - Wrapper for the youtube-dl library. Used so if there are any library interface + Wrapper for the yt-dlp library. Used so if there are any library interface updates we only need to udpate them in one place. ''' @@ -64,9 +64,9 @@ def get_media_info(url): return response -def download_media(url, media_format, extension, output_file, info_json, - sponsor_categories="all", - embed_thumbnail=False, embed_metadata=False, skip_sponsors=True, +def download_media(url, media_format, extension, output_file, info_json, + sponsor_categories=None, + embed_thumbnail=False, embed_metadata=False, skip_sponsors=True, write_subtitles=False, auto_subtitles=False, sub_langs='en'): ''' Downloads a YouTube URL to a file on disk. @@ -74,7 +74,7 @@ def download_media(url, media_format, extension, output_file, info_json, def hook(event): filename = os.path.basename(event['filename']) - + if event.get('downloaded_bytes') is None or event.get('total_bytes') is None: return None @@ -106,8 +106,8 @@ def download_media(url, media_format, extension, output_file, info_json, f'{total_size_str} in {elapsed_str}') else: log.warn(f'[youtube-dl] unknown event: {str(event)}') - hook.download_progress = 0 + hook.download_progress = 0 ytopts = { 'format': media_format, 'merge_output_format': extension, @@ -120,7 +120,8 @@ def download_media(url, media_format, extension, output_file, info_json, 'writeautomaticsub': auto_subtitles, 'subtitleslangs': sub_langs.split(','), } - + if not sponsor_categories: + sponsor_categories = [] sbopt = { 'key': 'SponsorBlock', 'categories': [sponsor_categories] @@ -130,7 +131,6 @@ def download_media(url, media_format, extension, output_file, info_json, 'add_chapters': True, 'add_metadata': True } - opts = get_yt_opts() if embed_thumbnail: ytopts['postprocessors'].append({'key': 'EmbedThumbnail'}) @@ -138,11 +138,9 @@ def download_media(url, media_format, extension, output_file, info_json, ffmdopt["add_metadata"] = True if skip_sponsors: ytopts['postprocessors'].append(sbopt) - ytopts['postprocessors'].append(ffmdopt) - opts.update(ytopts) - + with yt_dlp.YoutubeDL(opts) as y: try: return y.download([url])