Skip to content

Commit

Permalink
tidy up and pass a serialised list through to youtube.download_media,…
Browse files Browse the repository at this point in the history
… may help with #362
  • Loading branch information
meeb committed Dec 12, 2023
1 parent 6853c1f commit 4fdd172
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
26 changes: 11 additions & 15 deletions tubesync/sync/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
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
Expand Down Expand Up @@ -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'),
Expand All @@ -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,
Expand All @@ -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: '<i class="fab fa-youtube"></i>',
Expand Down Expand Up @@ -1390,8 +1386,8 @@ def download_media(self):
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
Expand Down
20 changes: 9 additions & 11 deletions tubesync/sync/youtube.py
Original file line number Diff line number Diff line change
@@ -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.
'''

Expand Down Expand Up @@ -64,17 +64,17 @@ 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.
'''

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

Expand Down Expand Up @@ -106,8 +106,8 @@ def hook(event):
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,
Expand All @@ -120,7 +120,8 @@ def hook(event):
'writeautomaticsub': auto_subtitles,
'subtitleslangs': sub_langs.split(','),
}

if not sponsor_categories:
sponsor_categories = []
sbopt = {
'key': 'SponsorBlock',
'categories': [sponsor_categories]
Expand All @@ -130,19 +131,16 @@ def hook(event):
'add_chapters': True,
'add_metadata': True
}

opts = get_yt_opts()
if embed_thumbnail:
ytopts['postprocessors'].append({'key': 'EmbedThumbnail'})
if embed_metadata:
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])
Expand Down

0 comments on commit 4fdd172

Please sign in to comment.