Added quality choice based on format
This commit is contained in:
parent
b3a589f1a9
commit
d051814259
|
|
@ -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
|
||||||
26
app/ytdl.py
26
app/ytdl.py
|
|
@ -4,6 +4,7 @@ from collections import OrderedDict
|
||||||
import asyncio
|
import asyncio
|
||||||
import multiprocessing
|
import multiprocessing
|
||||||
import logging
|
import logging
|
||||||
|
from dl_formats import get_format, get_opts
|
||||||
|
|
||||||
log = logging.getLogger('ytdl')
|
log = logging.getLogger('ytdl')
|
||||||
|
|
||||||
|
|
@ -36,29 +37,8 @@ class Download:
|
||||||
def __init__(self, download_dir, output_template, quality, format, ytdl_opts, info):
|
def __init__(self, download_dir, output_template, quality, format, ytdl_opts, info):
|
||||||
self.download_dir = download_dir
|
self.download_dir = download_dir
|
||||||
self.output_template = output_template
|
self.output_template = output_template
|
||||||
vfmt, afmt = '', ''
|
self.format = get_format(format, quality)
|
||||||
if format == 'mp4':
|
self.ytdl_opts = get_opts(format, quality, ytdl_opts)
|
||||||
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.info = info
|
self.info = info
|
||||||
self.canceled = False
|
self.canceled = False
|
||||||
self.tmpfilename = None
|
self.tmpfilename = None
|
||||||
|
|
|
||||||
|
|
@ -26,20 +26,20 @@
|
||||||
<div class="col-md-5 add-url-component">
|
<div class="col-md-5 add-url-component">
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
<div class="input-group-prepend">
|
<div class="input-group-prepend">
|
||||||
<span class="input-group-text">Video quality</span>
|
<span class="input-group-text">Format</span>
|
||||||
</div>
|
</div>
|
||||||
<select class="custom-select" name="quality" [(ngModel)]="quality" (change)="qualityChanged()" [disabled]="addInProgress || downloads.loading">
|
<select class="custom-select" name="format" [(ngModel)]="format" (change)="formatChanged()" [disabled]="addInProgress || downloads.loading">
|
||||||
<option *ngFor="let q of qualities" [ngValue]="q.id">{{ q.text }}</option>
|
<option *ngFor="let f of formats" [ngValue]="f.id">{{ f.text }}</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-4 add-url-component">
|
<div class="col-md-4 add-url-component">
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
<div class="input-group-prepend">
|
<div class="input-group-prepend">
|
||||||
<span class="input-group-text">Format</span>
|
<span class="input-group-text">Quality</span>
|
||||||
</div>
|
</div>
|
||||||
<select class="custom-select" name="format" [(ngModel)]="format" (change)="formatChanged()" [disabled]="addInProgress || downloads.loading">
|
<select class="custom-select" name="quality" [(ngModel)]="quality" (change)="qualityChanged()" [disabled]="addInProgress || downloads.loading">
|
||||||
<option *ngFor="let f of formats" [ngValue]="f.id">{{ f.text }}</option>
|
<option *ngFor="let q of qualities" [ngValue]="q.id">{{ q.text }}</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import { CookieService } from 'ngx-cookie-service';
|
||||||
|
|
||||||
import { DownloadsService, Status } from './downloads.service';
|
import { DownloadsService, Status } from './downloads.service';
|
||||||
import { MasterCheckboxComponent } from './master-checkbox.component';
|
import { MasterCheckboxComponent } from './master-checkbox.component';
|
||||||
|
import { Formats, Format, Quality } from './formats';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-root',
|
selector: 'app-root',
|
||||||
|
|
@ -13,20 +14,9 @@ import { MasterCheckboxComponent } from './master-checkbox.component';
|
||||||
})
|
})
|
||||||
export class AppComponent implements AfterViewInit {
|
export class AppComponent implements AfterViewInit {
|
||||||
addUrl: string;
|
addUrl: string;
|
||||||
qualities: Array<Object> = [
|
formats: Format[] = Formats;
|
||||||
{id: "best", text: "Best"},
|
qualities: Quality[];
|
||||||
{id: "1440p", text: "1440p"},
|
|
||||||
{id: "1080p", text: "1080p"},
|
|
||||||
{id: "720p", text: "720p"},
|
|
||||||
{id: "480p", text: "480p"},
|
|
||||||
{id: "audio", text: "Audio only"}
|
|
||||||
];
|
|
||||||
quality: string;
|
quality: string;
|
||||||
formats: Array<Object> = [
|
|
||||||
{id: "any", text: "Any"},
|
|
||||||
{id: "mp4", text: "MP4"},
|
|
||||||
{id: "mp3", text: "MP3"}
|
|
||||||
];
|
|
||||||
format: string;
|
format: string;
|
||||||
addInProgress = false;
|
addInProgress = false;
|
||||||
|
|
||||||
|
|
@ -45,6 +35,8 @@ export class AppComponent implements AfterViewInit {
|
||||||
constructor(public downloads: DownloadsService, private cookieService: CookieService) {
|
constructor(public downloads: DownloadsService, private cookieService: CookieService) {
|
||||||
this.quality = cookieService.get('metube_quality') || 'best';
|
this.quality = cookieService.get('metube_quality') || 'best';
|
||||||
this.format = cookieService.get('metube_format') || 'any';
|
this.format = cookieService.get('metube_format') || 'any';
|
||||||
|
// Needs to be set or qualities won't automatically be set
|
||||||
|
this.setQualities()
|
||||||
}
|
}
|
||||||
|
|
||||||
ngAfterViewInit() {
|
ngAfterViewInit() {
|
||||||
|
|
@ -77,6 +69,8 @@ export class AppComponent implements AfterViewInit {
|
||||||
|
|
||||||
formatChanged() {
|
formatChanged() {
|
||||||
this.cookieService.set('metube_format', this.format, { expires: 3650 });
|
this.cookieService.set('metube_format', this.format, { expires: 3650 });
|
||||||
|
// Updates to use qualities available
|
||||||
|
this.setQualities()
|
||||||
}
|
}
|
||||||
|
|
||||||
queueSelectionChanged(checked: number) {
|
queueSelectionChanged(checked: number) {
|
||||||
|
|
@ -87,6 +81,11 @@ export class AppComponent implements AfterViewInit {
|
||||||
this.doneDelSelected.nativeElement.disabled = checked == 0;
|
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) {
|
addDownload(url?: string, quality?: string, format?: string) {
|
||||||
url = url ?? this.addUrl
|
url = url ?? this.addUrl
|
||||||
quality = quality ?? this.quality
|
quality = quality ?? this.quality
|
||||||
|
|
|
||||||
|
|
@ -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' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
];
|
||||||
Loading…
Reference in New Issue