Merge pull request #211 from ThibaultNocchi/add_write_info_json

Add possibilty to use yt dlp write-info-json flag
This commit is contained in:
meeb 2022-02-03 15:10:27 +11:00 committed by GitHub
commit 238c0b5911
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 4 deletions

View File

@ -0,0 +1,21 @@
# Generated by Django 3.2.11 on 2022-02-01 16:54
import django.core.files.storage
from django.db import migrations, models
import sync.models
class Migration(migrations.Migration):
dependencies = [
('sync', '0010_auto_20210924_0554'),
]
operations = [
migrations.AddField(
model_name='source',
name='write_json',
field=models.BooleanField(
default=False, help_text='Write a JSON file with the media info, these may be detected and used by some media servers', verbose_name='write json'),
),
]

View File

@ -298,6 +298,11 @@ class Source(models.Model):
default=False,
help_text=_('Write an NFO file in XML with the media info, these may be detected and used by some media servers')
)
write_json = models.BooleanField(
_('write json'),
default=False,
help_text=_('Write a JSON file with the media info, these may be detected and used by some media servers')
)
has_failed = models.BooleanField(
_('has failed'),
default=False,
@ -1072,6 +1077,16 @@ class Media(models.Model):
def nfopath(self):
return self.source.directory_path / self.nfoname
@property
def jsonname(self):
filename = os.path.basename(self.media_file.path)
prefix, ext = os.path.splitext(filename)
return f'{prefix}.info.json'
@property
def jsonpath(self):
return self.source.directory_path / self.jsonname
@property
def directory_path(self):
# Otherwise, create a suitable filename from the source media_format
@ -1220,7 +1235,7 @@ 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))
str(self.filepath), self.source.write_json)
# Return the download paramaters
return format_str, self.source.extension

View File

@ -111,6 +111,10 @@
<td class="hide-on-small-only">Write NFO?</td>
<td><span class="hide-on-med-and-up">Write NFO?<br></span><strong>{% if source.write_nfo %}<i class="fas fa-check"></i>{% else %}<i class="fas fa-times"></i>{% endif %}</strong></td>
</tr>
<tr title="Should a JSON file be written with the media?">
<td class="hide-on-small-only">Write JSON?</td>
<td><span class="hide-on-med-and-up">Write JSON?<br></span><strong>{% if source.write_json %}<i class="fas fa-check"></i>{% else %}<i class="fas fa-times"></i>{% endif %}</strong></td>
</tr>
{% if source.delete_old_media and source.days_to_keep > 0 %}
<tr title="Days after which your media from this source will be locally deleted">
<td class="hide-on-small-only">Delete old media</td>

View File

@ -280,7 +280,7 @@ class AddSourceView(CreateView):
fields = ('source_type', 'key', 'name', 'directory', 'media_format',
'index_schedule', 'download_media', 'download_cap', 'delete_old_media',
'days_to_keep', 'source_resolution', 'source_vcodec', 'source_acodec',
'prefer_60fps', 'prefer_hdr', 'fallback', 'copy_thumbnails', 'write_nfo')
'prefer_60fps', 'prefer_hdr', 'fallback', 'copy_thumbnails', 'write_nfo', 'write_json')
errors = {
'invalid_media_format': _('Invalid media format, the media format contains '
'errors or is empty. Check the table at the end of '
@ -371,7 +371,7 @@ class UpdateSourceView(UpdateView):
fields = ('source_type', 'key', 'name', 'directory', 'media_format',
'index_schedule', 'download_media', 'download_cap', 'delete_old_media',
'days_to_keep', 'source_resolution', 'source_vcodec', 'source_acodec',
'prefer_60fps', 'prefer_hdr', 'fallback', 'copy_thumbnails', 'write_nfo')
'prefer_60fps', 'prefer_hdr', 'fallback', 'copy_thumbnails', 'write_nfo', 'write_json')
errors = {
'invalid_media_format': _('Invalid media format, the media format contains '
'errors or is empty. Check the table at the end of '
@ -421,6 +421,8 @@ class DeleteSourceView(DeleteView, FormMixin):
delete_file(media.thumbpath)
# Delete NFO file if it exists
delete_file(media.nfopath)
# Delete JSON file if it exists
delete_file(media.jsonpath)
return super().post(request, *args, **kwargs)
def get_success_url(self):

View File

@ -64,7 +64,7 @@ def get_media_info(url):
return response
def download_media(url, media_format, extension, output_file):
def download_media(url, media_format, extension, output_file, info_json):
'''
Downloads a YouTube URL to a file on disk.
'''
@ -108,6 +108,7 @@ def download_media(url, media_format, extension, output_file):
'outtmpl': output_file,
'quiet': True,
'progress_hooks': [hook],
'writeinfojson': info_json
})
with yt_dlp.YoutubeDL(opts) as y:
try: