diff --git a/tubesync/sync/models/media__tasks.py b/tubesync/sync/models/media__tasks.py index 106a27c6..fef043e3 100644 --- a/tubesync/sync/models/media__tasks.py +++ b/tubesync/sync/models/media__tasks.py @@ -1,3 +1,4 @@ +import os from common.logger import log from common.errors import ( NoMetadataException, @@ -47,6 +48,50 @@ def download_checklist(self, skip_checks=False): return False +def download_finished(self, format_str, container, downloaded_filepath=None): + media = self + if downloaded_filepath is None: + downloaded_filepath = self.filepath + filepath = Path(downloaded_filepath) + # Media has been downloaded successfully + log.info(f'Successfully downloaded media: {media} (UUID: {media.pk}) to: ' + f'"{filepath}"') + # Link the media file to the object and update info about the download + media.media_file.name = str(media.source.type_directory_path / media.filename) + media.downloaded = True + media.download_date = timezone.now() + media.downloaded_filesize = os.path.getsize(filepath) + media.downloaded_container = container + if '+' in format_str: + # Seperate audio and video streams + vformat_code, aformat_code = format_str.split('+') + aformat = media.get_format_by_code(aformat_code) + vformat = media.get_format_by_code(vformat_code) + media.downloaded_format = vformat['format'] + media.downloaded_height = vformat['height'] + media.downloaded_width = vformat['width'] + media.downloaded_audio_codec = aformat['acodec'] + media.downloaded_video_codec = vformat['vcodec'] + media.downloaded_container = container + media.downloaded_fps = vformat['fps'] + media.downloaded_hdr = vformat['is_hdr'] + else: + # Combined stream or audio-only stream + cformat_code = format_str + cformat = media.get_format_by_code(cformat_code) + media.downloaded_audio_codec = cformat['acodec'] + if cformat['vcodec']: + # Combined + media.downloaded_format = cformat['format'] + media.downloaded_height = cformat['height'] + media.downloaded_width = cformat['width'] + media.downloaded_video_codec = cformat['vcodec'] + media.downloaded_fps = cformat['fps'] + media.downloaded_hdr = cformat['is_hdr'] + else: + media.downloaded_format = 'audio' + + def wait_for_premiere(self): hours = lambda td: 1+int((24*td.days)+(td.seconds/(60*60)))