Skip to content

Commit

Permalink
feat(core): simplify checking if download is audio or video
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan Shatford <[email protected]>
  • Loading branch information
jordanshatford committed Oct 17, 2023
1 parent 066c424 commit b3c58db
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
22 changes: 10 additions & 12 deletions core/ydcore/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from ..models import AudioFormat
from ..models import DownloadState
from ..models import DownloadStatus
from ..models import DownloadType
from ..models import VideoFormat
from ..models import VideoWithOptions
from ..models import VideoWithOptionsAndStatus
Expand Down Expand Up @@ -54,26 +53,25 @@ def has_conflicts(self) -> bool:
return False

@property
def _download_type(self) -> DownloadType:
if self._video.options.format in VideoFormat:
return DownloadType.VIDEO
elif self._video.options.format in AudioFormat:
return DownloadType.AUDIO
else:
return DownloadType.VIDEO
def _is_audio_download(self) -> bool:
return self._video.options.format in AudioFormat

@property
def _is_video_download(self) -> bool:
return self._video.options.format in VideoFormat

@property
def as_ytdlp_params(self) -> YoutubeDLParams:
postprocessors: list[dict[str, str | bool | int]] = []
# Only append audio postprocessor if we are downloading audio format.
if self._download_type == DownloadType.AUDIO:
if self._is_audio_download:
postprocessors.append({
'key': 'FFmpegExtractAudio',
'preferredcodec': self._video.options.format.value,
'preferredquality': '192',
})
# Only append video postprocessor if we are downloading video format.
if self._download_type == DownloadType.VIDEO:
if self._is_video_download:
postprocessors.append(
{
'key': 'FFmpegVideoConvertor',
Expand Down Expand Up @@ -116,11 +114,11 @@ def _ytdlp_format(self) -> str:
extension = options.format.value
# bestvideo*[ext=X]+bestaudio/bestvideo*+bestaudio/best or
# worstvideo*[ext=X]+worstaudio/worstvideo*+worstaudio/worst
if (self._download_type == DownloadType.VIDEO):
if self._is_video_download:
proper_ext = f'{quality}video*[ext={extension}]+{quality}audio'
return f'{proper_ext}/{quality}video*+{quality}audio/{quality}'
# bestaudio[ext=X]/bestaudio/best or worstaudio[ext=X]/worstaudio/worst
elif (self._download_type == DownloadType.AUDIO):
elif self._is_audio_download:
return f'{quality}audio[ext={extension}]/{quality}audio/{quality}'
# Default to returning the quality (ie 'best' or 'worst')
return quality
Expand Down
5 changes: 0 additions & 5 deletions core/ydcore/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ class DownloadStatus(BaseModel):
postprocessor: str | None = None


class DownloadType(str, enum.Enum):
AUDIO = 'audio'
VIDEO = 'video'


class AudioFormat(str, enum.Enum):
AAC = 'aac'
FLAC = 'flac'
Expand Down

1 comment on commit b3c58db

@vercel
Copy link

@vercel vercel bot commented on b3c58db Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.