From b3a589f1a9d4f84e180623cb32cb826579f52553 Mon Sep 17 00:00:00 2001 From: asuyou Date: Mon, 25 Oct 2021 17:15:09 +0100 Subject: Added simple MP3 support --- app/ytdl.py | 8 ++++++++ ui/src/app/app.component.ts | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/ytdl.py b/app/ytdl.py index f9084e3..0400877 100644 --- a/app/ytdl.py +++ b/app/ytdl.py @@ -39,6 +39,14 @@ class Download: vfmt, afmt = '', '' if format == 'mp4': vfmt, afmt = '[ext=mp4]', '[ext=m4a]' + elif format == 'mp3': + afmt = '/best' + ytdl_opts["writethumbnail"] = True + ytdl_opts["postprocessors"] = [ + {"key": "FFmpegExtractAudio", "preferredcodec": "mp3"}, + {"key": "EmbedThumbnail"}, + ] + if quality == 'best': self.format = f'bestvideo{vfmt}+bestaudio{afmt}/best{vfmt}' elif quality in ('1440p', '1080p', '720p', '480p'): diff --git a/ui/src/app/app.component.ts b/ui/src/app/app.component.ts index 55f7e6e..a86bbe1 100644 --- a/ui/src/app/app.component.ts +++ b/ui/src/app/app.component.ts @@ -24,7 +24,8 @@ export class AppComponent implements AfterViewInit { quality: string; formats: Array = [ {id: "any", text: "Any"}, - {id: "mp4", text: "MP4"} + {id: "mp4", text: "MP4"}, + {id: "mp3", text: "MP3"} ]; format: string; addInProgress = false; -- cgit From d0518142599d326bac12a66e0173eb0e2da2c66d Mon Sep 17 00:00:00 2001 From: asuyou Date: Thu, 28 Oct 2021 11:19:17 +0100 Subject: Added quality choice based on format --- app/dl_formats.py | 85 +++++++++++++++++++++++++++++++++++++++++++ app/ytdl.py | 26 ++----------- ui/src/app/app.component.html | 12 +++--- ui/src/app/app.component.ts | 25 ++++++------- ui/src/app/formats.ts | 39 ++++++++++++++++++++ 5 files changed, 145 insertions(+), 42 deletions(-) create mode 100644 app/dl_formats.py create mode 100644 ui/src/app/formats.ts diff --git a/app/dl_formats.py b/app/dl_formats.py new file mode 100644 index 0000000..184f9f9 --- /dev/null +++ b/app/dl_formats.py @@ -0,0 +1,85 @@ +def get_format(format: str, quality: str) -> str: + """ + Returns format for download + + Args: + format (str): format selected + quality (str): quality selected + + Raises: + Exception: unknown quality, unknown format + + Returns: + dl_format: Formatted download string + """ + audio_fmt = "" + video_fmt = "" + final_fmt = "" + + if format.startswith("custom: "): + final_fmt = format[7:] + elif format == "any": + final_fmt = "bv*+ba/b" + elif format == "mp3": + audio_fmt = _get_audio_fmt(quality) + elif format == "mp4": + audio_fmt = "ba/b" + video_fmt = _get_video_fmt(quality) + else: + raise Exception(f"Unknown format {format}") + + if not final_fmt: + final_fmt = video_fmt + audio_fmt + + return final_fmt + + +def get_opts(format: str, quality: str, ytdl_opts: dict) -> dict: + """ + Returns extra download options + Mostly postprocessing options + + Args: + format (str): format selected + quality (str): quality of format selected (needed for some formats) + ytdl_opts (dict): current options selected + + Returns: + ytdl_opts: Extra options + """ + if "postprocessors" not in ytdl_opts: + ytdl_opts["postprocessors"] = [] + + if format == "mp3": + extra_args = {} + if quality != "best": + extra_args = {"preferredquality": quality} + ytdl_opts["postprocessors"].append( + {"key": "FFmpegExtractAudio", "preferredcodec": "mp3", **extra_args}, + ) + + elif format == "mp4": + ytdl_opts["merge_output_format"] = "mp4" + + return ytdl_opts + + +def _get_audio_fmt(quality: str) -> str: + if quality == "best" or quality in ("128", "192", "320"): + audio_fmt = "ba/b" + # Audio quality needs to be set post-download, set in opts + else: + raise Exception(f"Unknown quality {quality}") + + return audio_fmt + + +def _get_video_fmt(quality: str) -> str: + if quality == "best": + video_fmt = "bv*+" + elif quality in ("1440", "1080", "720", "480"): + video_fmt = f"bv[height<={quality}]+" + else: + raise Exception(f"Unknown quality {quality}") + + return video_fmt diff --git a/app/ytdl.py b/app/ytdl.py index 0400877..63c3786 100644 --- a/app/ytdl.py +++ b/app/ytdl.py @@ -4,6 +4,7 @@ from collections import OrderedDict import asyncio import multiprocessing import logging +from dl_formats import get_format, get_opts log = logging.getLogger('ytdl') @@ -36,29 +37,8 @@ class Download: def __init__(self, download_dir, output_template, quality, format, ytdl_opts, info): self.download_dir = download_dir self.output_template = output_template - vfmt, afmt = '', '' - if format == 'mp4': - vfmt, afmt = '[ext=mp4]', '[ext=m4a]' - elif format == 'mp3': - afmt = '/best' - ytdl_opts["writethumbnail"] = True - ytdl_opts["postprocessors"] = [ - {"key": "FFmpegExtractAudio", "preferredcodec": "mp3"}, - {"key": "EmbedThumbnail"}, - ] - - if quality == 'best': - self.format = f'bestvideo{vfmt}+bestaudio{afmt}/best{vfmt}' - elif quality in ('1440p', '1080p', '720p', '480p'): - res = quality[:-1] - self.format = f'bestvideo[height<={res}]{vfmt}+bestaudio{afmt}/best[height<={res}]{vfmt}' - elif quality == 'audio': - self.format = f'bestaudio{afmt}' - elif quality.startswith('custom:'): - self.format = quality[7:] - else: - raise Exception(f'unknown quality {quality}') - self.ytdl_opts = ytdl_opts + self.format = get_format(format, quality) + self.ytdl_opts = get_opts(format, quality, ytdl_opts) self.info = info self.canceled = False self.tmpfilename = None diff --git a/ui/src/app/app.component.html b/ui/src/app/app.component.html index 20a78b4..1a90d5f 100644 --- a/ui/src/app/app.component.html +++ b/ui/src/app/app.component.html @@ -26,20 +26,20 @@
- Video quality + Format
- +
- Format + Quality
- +
diff --git a/ui/src/app/app.component.ts b/ui/src/app/app.component.ts index a86bbe1..28b9539 100644 --- a/ui/src/app/app.component.ts +++ b/ui/src/app/app.component.ts @@ -5,6 +5,7 @@ import { CookieService } from 'ngx-cookie-service'; import { DownloadsService, Status } from './downloads.service'; import { MasterCheckboxComponent } from './master-checkbox.component'; +import { Formats, Format, Quality } from './formats'; @Component({ selector: 'app-root', @@ -13,20 +14,9 @@ import { MasterCheckboxComponent } from './master-checkbox.component'; }) export class AppComponent implements AfterViewInit { addUrl: string; - qualities: Array = [ - {id: "best", text: "Best"}, - {id: "1440p", text: "1440p"}, - {id: "1080p", text: "1080p"}, - {id: "720p", text: "720p"}, - {id: "480p", text: "480p"}, - {id: "audio", text: "Audio only"} - ]; + formats: Format[] = Formats; + qualities: Quality[]; quality: string; - formats: Array = [ - {id: "any", text: "Any"}, - {id: "mp4", text: "MP4"}, - {id: "mp3", text: "MP3"} - ]; format: string; addInProgress = false; @@ -45,6 +35,8 @@ export class AppComponent implements AfterViewInit { constructor(public downloads: DownloadsService, private cookieService: CookieService) { this.quality = cookieService.get('metube_quality') || 'best'; this.format = cookieService.get('metube_format') || 'any'; + // Needs to be set or qualities won't automatically be set + this.setQualities() } ngAfterViewInit() { @@ -77,6 +69,8 @@ export class AppComponent implements AfterViewInit { formatChanged() { this.cookieService.set('metube_format', this.format, { expires: 3650 }); + // Updates to use qualities available + this.setQualities() } queueSelectionChanged(checked: number) { @@ -87,6 +81,11 @@ export class AppComponent implements AfterViewInit { this.doneDelSelected.nativeElement.disabled = checked == 0; } + setQualities() { + // qualities for specific format + this.qualities = this.formats.find(el => el.id == this.format).qualities + } + addDownload(url?: string, quality?: string, format?: string) { url = url ?? this.addUrl quality = quality ?? this.quality diff --git a/ui/src/app/formats.ts b/ui/src/app/formats.ts new file mode 100644 index 0000000..5264cd0 --- /dev/null +++ b/ui/src/app/formats.ts @@ -0,0 +1,39 @@ +export interface Format { + id: string; + text: string; + qualities: Quality[]; +} + +export interface Quality { + id: string; + text: string; +} + +export const Formats: Format[] = [ + { + id: 'any', + text: 'Any', + qualities: [{ id: 'best', text: 'Best' }], + }, + { + id: 'mp4', + text: 'MP4', + qualities: [ + { id: 'best', text: 'Best' }, + { id: '1440', text: '1440p' }, + { id: '1080', text: '1080p' }, + { id: '720', text: '720p' }, + { id: '480', text: '480p' }, + ], + }, + { + id: 'mp3', + text: 'MP3', + qualities: [ + { id: 'best', text: 'Best' }, + { id: '128', text: '128 kbps' }, + { id: '192', text: '192 kbps' }, + { id: '320', text: '320 kbps' }, + ], + }, +]; -- cgit From 9e82aa1976eeca20d643073596cdca0196695617 Mon Sep 17 00:00:00 2001 From: asuyou Date: Sat, 30 Oct 2021 18:06:56 +0100 Subject: Fixed "custom:" download format --- app/dl_formats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/dl_formats.py b/app/dl_formats.py index 184f9f9..f1b3881 100644 --- a/app/dl_formats.py +++ b/app/dl_formats.py @@ -16,7 +16,7 @@ def get_format(format: str, quality: str) -> str: video_fmt = "" final_fmt = "" - if format.startswith("custom: "): + if format.startswith("custom:"): final_fmt = format[7:] elif format == "any": final_fmt = "bv*+ba/b" -- cgit From bae29a4bad5f3ac6177d6f908cc0fc3dac3a71e6 Mon Sep 17 00:00:00 2001 From: asuyou Date: Sat, 30 Oct 2021 18:17:13 +0100 Subject: Flipped back around sides of quality and format --- ui/src/app/app.component.html | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ui/src/app/app.component.html b/ui/src/app/app.component.html index 1a90d5f..039aab6 100644 --- a/ui/src/app/app.component.html +++ b/ui/src/app/app.component.html @@ -26,20 +26,20 @@
- Format + Quality
- +
- Quality + Format
- +
-- cgit From 85140fd4ca1abc8431c1b434579f6ed38d06e2a3 Mon Sep 17 00:00:00 2001 From: asuyou Date: Mon, 1 Nov 2021 20:37:53 +0000 Subject: Automatically switches to best on format change --- ui/src/app/app.component.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/ui/src/app/app.component.ts b/ui/src/app/app.component.ts index 28b9539..ed19618 100644 --- a/ui/src/app/app.component.ts +++ b/ui/src/app/app.component.ts @@ -84,6 +84,7 @@ export class AppComponent implements AfterViewInit { setQualities() { // qualities for specific format this.qualities = this.formats.find(el => el.id == this.format).qualities + this.quality = "best" } addDownload(url?: string, quality?: string, format?: string) { -- cgit From 26798a4930da04d5ce4d54742e0d8ca8d802e9ba Mon Sep 17 00:00:00 2001 From: asuyou Date: Wed, 3 Nov 2021 21:57:34 +0000 Subject: Any now shows all avaliable formats --- ui/src/app/app.component.html | 2 +- ui/src/app/app.component.ts | 17 +++++++++-------- ui/src/app/formats.ts | 39 +++++++++++++++++++++++++++++---------- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/ui/src/app/app.component.html b/ui/src/app/app.component.html index 039aab6..fda6033 100644 --- a/ui/src/app/app.component.html +++ b/ui/src/app/app.component.html @@ -29,7 +29,7 @@ Quality diff --git a/ui/src/app/app.component.ts b/ui/src/app/app.component.ts index ed19618..dd0b7f1 100644 --- a/ui/src/app/app.component.ts +++ b/ui/src/app/app.component.ts @@ -5,7 +5,7 @@ import { CookieService } from 'ngx-cookie-service'; import { DownloadsService, Status } from './downloads.service'; import { MasterCheckboxComponent } from './master-checkbox.component'; -import { Formats, Format, Quality } from './formats'; +import { Formats, Format, Quality, fillQualities, getQualityById } from './formats'; @Component({ selector: 'app-root', @@ -14,9 +14,9 @@ import { Formats, Format, Quality } from './formats'; }) export class AppComponent implements AfterViewInit { addUrl: string; - formats: Format[] = Formats; + formats: Format[] = fillQualities(Formats); qualities: Quality[]; - quality: string; + quality: Quality; format: string; addInProgress = false; @@ -33,10 +33,11 @@ export class AppComponent implements AfterViewInit { faRedoAlt = faRedoAlt; constructor(public downloads: DownloadsService, private cookieService: CookieService) { - this.quality = cookieService.get('metube_quality') || 'best'; this.format = cookieService.get('metube_format') || 'any'; // Needs to be set or qualities won't automatically be set this.setQualities() + let qualityId = cookieService.get('metube_quality') || this.qualities[0].id + this.quality = getQualityById(this.formats, qualityId); } ngAfterViewInit() { @@ -64,7 +65,7 @@ export class AppComponent implements AfterViewInit { } qualityChanged() { - this.cookieService.set('metube_quality', this.quality, { expires: 3650 }); + this.cookieService.set('metube_quality', this.quality.id, { expires: 3650 }); } formatChanged() { @@ -84,13 +85,13 @@ export class AppComponent implements AfterViewInit { setQualities() { // qualities for specific format this.qualities = this.formats.find(el => el.id == this.format).qualities - this.quality = "best" + this.quality = this.qualities.find(el => el.value === "best") } addDownload(url?: string, quality?: string, format?: string) { url = url ?? this.addUrl - quality = quality ?? this.quality - format = format ?? this.format + quality = quality ?? this.quality.value + format = format ?? this.quality.fmt this.addInProgress = true; this.downloads.add(url, quality, format).subscribe((status: Status) => { diff --git a/ui/src/app/formats.ts b/ui/src/app/formats.ts index 5264cd0..4f8dd0c 100644 --- a/ui/src/app/formats.ts +++ b/ui/src/app/formats.ts @@ -7,33 +7,52 @@ export interface Format { export interface Quality { id: string; text: string; + value: string; + fmt?: string; } export const Formats: Format[] = [ { id: 'any', text: 'Any', - qualities: [{ id: 'best', text: 'Best' }], + qualities: [], }, { id: 'mp4', text: 'MP4', qualities: [ - { id: 'best', text: 'Best' }, - { id: '1440', text: '1440p' }, - { id: '1080', text: '1080p' }, - { id: '720', text: '720p' }, - { id: '480', text: '480p' }, + { id: "1", value: 'best', text: 'Best MP4' }, + { id: "2", value: '1440', text: '1440p' }, + { id: "3", value: '1080', text: '1080p' }, + { id: "4", value: '720', text: '720p' }, + { id: "5", value: '480', text: '480p' }, ], }, { id: 'mp3', text: 'MP3', qualities: [ - { id: 'best', text: 'Best' }, - { id: '128', text: '128 kbps' }, - { id: '192', text: '192 kbps' }, - { id: '320', text: '320 kbps' }, + { id: "6", value: 'best', text: 'Best MP3' }, + { id: "7", value: '128', text: '128 kbps' }, + { id: "8", value: '192', text: '192 kbps' }, + { id: "9", value: '320', text: '320 kbps' }, ], }, ]; + +export const fillQualities = (formats: Format[]): Format[] => { + let allQualities: Quality[] = []; + formats.forEach((fmt) => { + fmt.qualities = fmt.qualities.map((ql) => ({ ...ql, fmt: fmt.id })); + allQualities = allQualities.concat(fmt.qualities); + }); + + formats.find((format) => format.id === 'any').qualities = allQualities; + return formats; +}; + +export const getQualityById = (formats: Format[], id: string): Quality => { + return formats + .find(ql => ql.qualities.find(el => el.id === id)).qualities + .find(el => el.id === id) +} -- cgit From 63ec5c37afc99453125b855559c8913811217bdf Mon Sep 17 00:00:00 2001 From: asuyou Date: Fri, 5 Nov 2021 23:46:40 +0000 Subject: Changed back to original format download --- app/dl_formats.py | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/app/dl_formats.py b/app/dl_formats.py index f1b3881..84be68e 100644 --- a/app/dl_formats.py +++ b/app/dl_formats.py @@ -12,24 +12,12 @@ def get_format(format: str, quality: str) -> str: Returns: dl_format: Formatted download string """ - audio_fmt = "" - video_fmt = "" final_fmt = "" if format.startswith("custom:"): final_fmt = format[7:] - elif format == "any": - final_fmt = "bv*+ba/b" - elif format == "mp3": - audio_fmt = _get_audio_fmt(quality) - elif format == "mp4": - audio_fmt = "ba/b" - video_fmt = _get_video_fmt(quality) else: - raise Exception(f"Unknown format {format}") - - if not final_fmt: - final_fmt = video_fmt + audio_fmt + final_fmt = _get_final_fmt(format, quality) return final_fmt @@ -58,15 +46,12 @@ def get_opts(format: str, quality: str, ytdl_opts: dict) -> dict: {"key": "FFmpegExtractAudio", "preferredcodec": "mp3", **extra_args}, ) - elif format == "mp4": - ytdl_opts["merge_output_format"] = "mp4" - return ytdl_opts def _get_audio_fmt(quality: str) -> str: if quality == "best" or quality in ("128", "192", "320"): - audio_fmt = "ba/b" + audio_fmt = "bestaudio/best" # Audio quality needs to be set post-download, set in opts else: raise Exception(f"Unknown quality {quality}") @@ -74,12 +59,29 @@ def _get_audio_fmt(quality: str) -> str: return audio_fmt -def _get_video_fmt(quality: str) -> str: +def _get_video_res(quality: str) -> str: if quality == "best": - video_fmt = "bv*+" + video_fmt = "" elif quality in ("1440", "1080", "720", "480"): - video_fmt = f"bv[height<={quality}]+" + video_fmt = f"[height<={quality}]" else: raise Exception(f"Unknown quality {quality}") return video_fmt + + +def _get_final_fmt(format: str, quality: str) -> str: + vfmt, afmt, vres = "", "", "" + + if format == "mp4": + # video {res} {vfmt} + audio {afmt} {res} {vfmt} + vfmt, afmt = "[ext=mp4]", "[ext=m4a]" + vres = _get_video_res(quality) + combo = vres + vfmt + final_fmt = f"bestvideo{combo}+bestaudio{afmt}/best{combo}" + elif format == "mp3": + final_fmt = _get_audio_fmt(quality) + else: + raise Exception(f"Unkown format {format}") + + return final_fmt -- cgit From b661d4db591c8680905ad22a26a5234066a2c940 Mon Sep 17 00:00:00 2001 From: asuyou Date: Sat, 13 Nov 2021 23:49:30 +0000 Subject: Fixed "any" format to act like original --- ui/src/app/app.component.ts | 6 +++--- ui/src/app/formats.ts | 45 ++++++++++++++++++++------------------------- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/ui/src/app/app.component.ts b/ui/src/app/app.component.ts index dd0b7f1..0bab9d1 100644 --- a/ui/src/app/app.component.ts +++ b/ui/src/app/app.component.ts @@ -5,7 +5,7 @@ import { CookieService } from 'ngx-cookie-service'; import { DownloadsService, Status } from './downloads.service'; import { MasterCheckboxComponent } from './master-checkbox.component'; -import { Formats, Format, Quality, fillQualities, getQualityById } from './formats'; +import { Formats, Format, Quality, getQualityById } from './formats'; @Component({ selector: 'app-root', @@ -14,7 +14,7 @@ import { Formats, Format, Quality, fillQualities, getQualityById } from './forma }) export class AppComponent implements AfterViewInit { addUrl: string; - formats: Format[] = fillQualities(Formats); + formats: Format[] = Formats; qualities: Quality[]; quality: Quality; format: string; @@ -91,7 +91,7 @@ export class AppComponent implements AfterViewInit { addDownload(url?: string, quality?: string, format?: string) { url = url ?? this.addUrl quality = quality ?? this.quality.value - format = format ?? this.quality.fmt + format = format ?? this.format this.addInProgress = true; this.downloads.add(url, quality, format).subscribe((status: Status) => { diff --git a/ui/src/app/formats.ts b/ui/src/app/formats.ts index 4f8dd0c..f24b210 100644 --- a/ui/src/app/formats.ts +++ b/ui/src/app/formats.ts @@ -8,51 +8,46 @@ export interface Quality { id: string; text: string; value: string; - fmt?: string; } export const Formats: Format[] = [ { id: 'any', text: 'Any', - qualities: [], + qualities: [ + { id: '0', value: 'best', text: 'Best' }, + { id: '1', value: '1440', text: '1440p' }, + { id: '2', value: '1080', text: '1080p' }, + { id: '3', value: '720', text: '720p' }, + { id: '4', value: '480', text: '480p' }, + { id: '5', value: 'audio', text: 'Audio Only' }, + ], }, { id: 'mp4', text: 'MP4', qualities: [ - { id: "1", value: 'best', text: 'Best MP4' }, - { id: "2", value: '1440', text: '1440p' }, - { id: "3", value: '1080', text: '1080p' }, - { id: "4", value: '720', text: '720p' }, - { id: "5", value: '480', text: '480p' }, + { id: '6', value: 'best', text: 'Best' }, + { id: '7', value: '1440', text: '1440p' }, + { id: '8', value: '1080', text: '1080p' }, + { id: '9', value: '720', text: '720p' }, + { id: '10', value: '480', text: '480p' }, ], }, { id: 'mp3', text: 'MP3', qualities: [ - { id: "6", value: 'best', text: 'Best MP3' }, - { id: "7", value: '128', text: '128 kbps' }, - { id: "8", value: '192', text: '192 kbps' }, - { id: "9", value: '320', text: '320 kbps' }, + { id: '11', value: 'best', text: 'Best' }, + { id: '12', value: '128', text: '128 kbps' }, + { id: '13', value: '192', text: '192 kbps' }, + { id: '14', value: '320', text: '320 kbps' }, ], }, ]; -export const fillQualities = (formats: Format[]): Format[] => { - let allQualities: Quality[] = []; - formats.forEach((fmt) => { - fmt.qualities = fmt.qualities.map((ql) => ({ ...ql, fmt: fmt.id })); - allQualities = allQualities.concat(fmt.qualities); - }); - - formats.find((format) => format.id === 'any').qualities = allQualities; - return formats; -}; - export const getQualityById = (formats: Format[], id: string): Quality => { return formats - .find(ql => ql.qualities.find(el => el.id === id)).qualities - .find(el => el.id === id) -} + .find((ql) => ql.qualities.find((el) => el.id === id)) + .qualities.find((el) => el.id === id); +}; -- cgit From eadf8239e968d3ffd2e1da344ba9d8bc8fd78a1c Mon Sep 17 00:00:00 2001 From: asuyou Date: Sun, 14 Nov 2021 23:17:07 +0000 Subject: Changed to "any" to work like original (backend) --- app/dl_formats.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/app/dl_formats.py b/app/dl_formats.py index 84be68e..b47ae5c 100644 --- a/app/dl_formats.py +++ b/app/dl_formats.py @@ -60,7 +60,7 @@ def _get_audio_fmt(quality: str) -> str: def _get_video_res(quality: str) -> str: - if quality == "best": + if quality in ("best", "audio"): video_fmt = "" elif quality in ("1440", "1080", "720", "480"): video_fmt = f"[height<={quality}]" @@ -73,12 +73,17 @@ def _get_video_res(quality: str) -> str: def _get_final_fmt(format: str, quality: str) -> str: vfmt, afmt, vres = "", "", "" - if format == "mp4": + if format in ("mp4", "any"): # video {res} {vfmt} + audio {afmt} {res} {vfmt} - vfmt, afmt = "[ext=mp4]", "[ext=m4a]" - vres = _get_video_res(quality) - combo = vres + vfmt - final_fmt = f"bestvideo{combo}+bestaudio{afmt}/best{combo}" + if format == "mp4": + vfmt, afmt = "[ext=mp4]", "[ext=m4a]" + + if quality == "audio": + final_fmt = "bestaudio/best" + else: + vres = _get_video_res(quality) + combo = vres + vfmt + final_fmt = f"bestvideo{combo}+bestaudio{afmt}/best{combo}" elif format == "mp3": final_fmt = _get_audio_fmt(quality) else: -- cgit From 2e591122f2de091f76ddf2bdb57aaf29e72ed6f4 Mon Sep 17 00:00:00 2001 From: asuyou Date: Fri, 19 Nov 2021 17:57:43 +0000 Subject: Quality stays the same if it exists on next format --- ui/src/app/app.component.html | 2 +- ui/src/app/app.component.ts | 14 +++++++------- ui/src/app/formats.ts | 37 +++++++++++++++---------------------- 3 files changed, 23 insertions(+), 30 deletions(-) diff --git a/ui/src/app/app.component.html b/ui/src/app/app.component.html index fda6033..039aab6 100644 --- a/ui/src/app/app.component.html +++ b/ui/src/app/app.component.html @@ -29,7 +29,7 @@ Quality diff --git a/ui/src/app/app.component.ts b/ui/src/app/app.component.ts index 0bab9d1..26ae3c5 100644 --- a/ui/src/app/app.component.ts +++ b/ui/src/app/app.component.ts @@ -5,7 +5,7 @@ import { CookieService } from 'ngx-cookie-service'; import { DownloadsService, Status } from './downloads.service'; import { MasterCheckboxComponent } from './master-checkbox.component'; -import { Formats, Format, Quality, getQualityById } from './formats'; +import { Formats, Format, Quality } from './formats'; @Component({ selector: 'app-root', @@ -16,7 +16,7 @@ export class AppComponent implements AfterViewInit { addUrl: string; formats: Format[] = Formats; qualities: Quality[]; - quality: Quality; + quality: string; format: string; addInProgress = false; @@ -36,8 +36,7 @@ export class AppComponent implements AfterViewInit { this.format = cookieService.get('metube_format') || 'any'; // Needs to be set or qualities won't automatically be set this.setQualities() - let qualityId = cookieService.get('metube_quality') || this.qualities[0].id - this.quality = getQualityById(this.formats, qualityId); + this.quality = cookieService.get('metube_quality') || 'best'; } ngAfterViewInit() { @@ -65,7 +64,7 @@ export class AppComponent implements AfterViewInit { } qualityChanged() { - this.cookieService.set('metube_quality', this.quality.id, { expires: 3650 }); + this.cookieService.set('metube_quality', this.quality, { expires: 3650 }); } formatChanged() { @@ -85,12 +84,13 @@ export class AppComponent implements AfterViewInit { setQualities() { // qualities for specific format this.qualities = this.formats.find(el => el.id == this.format).qualities - this.quality = this.qualities.find(el => el.value === "best") + const exists = this.qualities.find(el => el.id === this.quality) + this.quality = exists ? this.quality : 'best' } addDownload(url?: string, quality?: string, format?: string) { url = url ?? this.addUrl - quality = quality ?? this.quality.value + quality = quality ?? this.quality format = format ?? this.format this.addInProgress = true; diff --git a/ui/src/app/formats.ts b/ui/src/app/formats.ts index f24b210..bb2fe07 100644 --- a/ui/src/app/formats.ts +++ b/ui/src/app/formats.ts @@ -7,7 +7,6 @@ export interface Format { export interface Quality { id: string; text: string; - value: string; } export const Formats: Format[] = [ @@ -15,39 +14,33 @@ export const Formats: Format[] = [ id: 'any', text: 'Any', qualities: [ - { id: '0', value: 'best', text: 'Best' }, - { id: '1', value: '1440', text: '1440p' }, - { id: '2', value: '1080', text: '1080p' }, - { id: '3', value: '720', text: '720p' }, - { id: '4', value: '480', text: '480p' }, - { id: '5', value: 'audio', text: 'Audio Only' }, + { id: 'best', text: 'Best' }, + { id: '1440', text: '1440p' }, + { id: '1080', text: '1080p' }, + { id: '720', text: '720p' }, + { id: '480', text: '480p' }, + { id: 'audio', text: 'Audio Only' }, ], }, { id: 'mp4', text: 'MP4', qualities: [ - { id: '6', value: 'best', text: 'Best' }, - { id: '7', value: '1440', text: '1440p' }, - { id: '8', value: '1080', text: '1080p' }, - { id: '9', value: '720', text: '720p' }, - { id: '10', value: '480', text: '480p' }, + { id: 'best', text: 'Best' }, + { id: '1440', text: '1440p' }, + { id: '1080', text: '1080p' }, + { id: '720', text: '720p' }, + { id: '480', text: '480p' }, ], }, { id: 'mp3', text: 'MP3', qualities: [ - { id: '11', value: 'best', text: 'Best' }, - { id: '12', value: '128', text: '128 kbps' }, - { id: '13', value: '192', text: '192 kbps' }, - { id: '14', value: '320', text: '320 kbps' }, + { id: 'best', text: 'Best' }, + { id: '128', text: '128 kbps' }, + { id: '192', text: '192 kbps' }, + { id: '320', text: '320 kbps' }, ], }, ]; - -export const getQualityById = (formats: Format[], id: string): Quality => { - return formats - .find((ql) => ql.qualities.find((el) => el.id === id)) - .qualities.find((el) => el.id === id); -}; -- cgit From 99c366e5c29a80b3d836fca22c37a7be523fc311 Mon Sep 17 00:00:00 2001 From: asuyou Date: Fri, 19 Nov 2021 18:01:02 +0000 Subject: Audio quality sorting now follows mp4 --- ui/src/app/formats.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ui/src/app/formats.ts b/ui/src/app/formats.ts index bb2fe07..15be903 100644 --- a/ui/src/app/formats.ts +++ b/ui/src/app/formats.ts @@ -38,9 +38,9 @@ export const Formats: Format[] = [ text: 'MP3', qualities: [ { id: 'best', text: 'Best' }, - { id: '128', text: '128 kbps' }, - { id: '192', text: '192 kbps' }, { id: '320', text: '320 kbps' }, + { id: '192', text: '192 kbps' }, + { id: '128', text: '128 kbps' }, ], }, ]; -- cgit From 69746826f8765dc61c2b4ba82bc4ccbe664bc933 Mon Sep 17 00:00:00 2001 From: asuyou Date: Fri, 19 Nov 2021 20:37:55 +0000 Subject: dl_formats options are now inline --- app/dl_formats.py | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/app/dl_formats.py b/app/dl_formats.py index b47ae5c..d0af344 100644 --- a/app/dl_formats.py +++ b/app/dl_formats.py @@ -49,27 +49,6 @@ def get_opts(format: str, quality: str, ytdl_opts: dict) -> dict: return ytdl_opts -def _get_audio_fmt(quality: str) -> str: - if quality == "best" or quality in ("128", "192", "320"): - audio_fmt = "bestaudio/best" - # Audio quality needs to be set post-download, set in opts - else: - raise Exception(f"Unknown quality {quality}") - - return audio_fmt - - -def _get_video_res(quality: str) -> str: - if quality in ("best", "audio"): - video_fmt = "" - elif quality in ("1440", "1080", "720", "480"): - video_fmt = f"[height<={quality}]" - else: - raise Exception(f"Unknown quality {quality}") - - return video_fmt - - def _get_final_fmt(format: str, quality: str) -> str: vfmt, afmt, vres = "", "", "" @@ -81,11 +60,20 @@ def _get_final_fmt(format: str, quality: str) -> str: if quality == "audio": final_fmt = "bestaudio/best" else: - vres = _get_video_res(quality) + if quality in ("best", "audio"): + vres = "" + elif quality in ("1440", "1080", "720", "480"): + vres = f"[height<={quality}]" + else: + raise Exception(f"Unknown quality {quality}") combo = vres + vfmt final_fmt = f"bestvideo{combo}+bestaudio{afmt}/best{combo}" elif format == "mp3": - final_fmt = _get_audio_fmt(quality) + if quality == "best" or quality in ("128", "192", "320"): + final_fmt = "bestaudio/best" + # Audio quality needs to be set post-download, set in opts + else: + raise Exception(f"Unknown quality {quality}") else: raise Exception(f"Unkown format {format}") -- cgit From f52bea74d335cc84b9d08ebe485e620a8309b2a3 Mon Sep 17 00:00:00 2001 From: Alex Shnitman Date: Sat, 20 Nov 2021 10:12:08 +0200 Subject: simplified format handling --- app/dl_formats.py | 54 ++++++++++++++++++------------------------------------ app/ytdl.py | 2 +- 2 files changed, 19 insertions(+), 37 deletions(-) diff --git a/app/dl_formats.py b/app/dl_formats.py index d0af344..1f27ee9 100644 --- a/app/dl_formats.py +++ b/app/dl_formats.py @@ -12,14 +12,27 @@ def get_format(format: str, quality: str) -> str: Returns: dl_format: Formatted download string """ - final_fmt = "" + format = format or "best" if format.startswith("custom:"): - final_fmt = format[7:] - else: - final_fmt = _get_final_fmt(format, quality) + return format[7:] - return final_fmt + if format == "mp3": + # Audio quality needs to be set post-download, set in opts + return "bestaudio/best" + + if format in ("mp4", "any"): + if quality == "audio": + return "bestaudio/best" + + # video {res} {vfmt} + audio {afmt} {res} {vfmt} + vfmt, afmt = ("[ext=mp4]", "[ext=m4a]") if format == "mp4" else ("", "") + vres = f"[height<={quality}]" if quality != "best" else "" + vcombo = vres + vfmt + + return f"bestvideo{vcombo}+bestaudio{afmt}/best{vcombo}" + + raise Exception(f"Unkown format {format}") def get_opts(format: str, quality: str, ytdl_opts: dict) -> dict: @@ -47,34 +60,3 @@ def get_opts(format: str, quality: str, ytdl_opts: dict) -> dict: ) return ytdl_opts - - -def _get_final_fmt(format: str, quality: str) -> str: - vfmt, afmt, vres = "", "", "" - - if format in ("mp4", "any"): - # video {res} {vfmt} + audio {afmt} {res} {vfmt} - if format == "mp4": - vfmt, afmt = "[ext=mp4]", "[ext=m4a]" - - if quality == "audio": - final_fmt = "bestaudio/best" - else: - if quality in ("best", "audio"): - vres = "" - elif quality in ("1440", "1080", "720", "480"): - vres = f"[height<={quality}]" - else: - raise Exception(f"Unknown quality {quality}") - combo = vres + vfmt - final_fmt = f"bestvideo{combo}+bestaudio{afmt}/best{combo}" - elif format == "mp3": - if quality == "best" or quality in ("128", "192", "320"): - final_fmt = "bestaudio/best" - # Audio quality needs to be set post-download, set in opts - else: - raise Exception(f"Unknown quality {quality}") - else: - raise Exception(f"Unkown format {format}") - - return final_fmt diff --git a/app/ytdl.py b/app/ytdl.py index 688744b..e0267d7 100644 --- a/app/ytdl.py +++ b/app/ytdl.py @@ -159,7 +159,7 @@ class DownloadQueue: elif etype == 'video' or etype.startswith('url') and 'id' in entry and 'title' in entry: if entry['id'] not in self.queue: dl = DownloadInfo(entry['id'], entry['title'], entry.get('webpage_url') or entry['url'], quality, format) - dldirectory = self.config.DOWNLOAD_DIR if quality != 'audio' else self.config.AUDIO_DOWNLOAD_DIR + dldirectory = self.config.DOWNLOAD_DIR if (quality != 'audio' and format != 'mp3') else self.config.AUDIO_DOWNLOAD_DIR self.queue[entry['id']] = Download(dldirectory, self.config.OUTPUT_TEMPLATE, quality, format, self.config.YTDL_OPTIONS, dl) self.event.set() await self.notifier.added(dl) -- cgit