diff options
author | Alex <alexta69@gmail.com> | 2021-11-20 10:14:03 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-20 10:14:03 +0200 |
commit | f61ecc147636868027d957c8255903525022e198 (patch) | |
tree | f140c3c5ad806c197f1abcf259ad2db9bcc1323b | |
parent | fix breakage after upgrade (diff) | |
parent | simplified format handling (diff) | |
download | metube-f61ecc147636868027d957c8255903525022e198.tar.gz metube-f61ecc147636868027d957c8255903525022e198.tar.bz2 metube-f61ecc147636868027d957c8255903525022e198.zip |
Merge pull request #73 from asuyou/mp3-support
Added simple MP3 support
-rw-r--r-- | app/dl_formats.py | 62 | ||||
-rw-r--r-- | app/ytdl.py | 20 | ||||
-rw-r--r-- | ui/src/app/app.component.html | 2 | ||||
-rw-r--r-- | ui/src/app/app.component.ts | 28 | ||||
-rw-r--r-- | ui/src/app/formats.ts | 46 |
5 files changed, 128 insertions, 30 deletions
diff --git a/app/dl_formats.py b/app/dl_formats.py new file mode 100644 index 0000000..1f27ee9 --- /dev/null +++ b/app/dl_formats.py @@ -0,0 +1,62 @@ +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 + """ + format = format or "best" + + if format.startswith("custom:"): + return format[7:] + + 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: + """ + 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}, + ) + + return ytdl_opts diff --git a/app/ytdl.py b/app/ytdl.py index a14855d..e0267d7 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,21 +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]'
- 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
@@ -171,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)
diff --git a/ui/src/app/app.component.html b/ui/src/app/app.component.html index 20a78b4..039aab6 100644 --- a/ui/src/app/app.component.html +++ b/ui/src/app/app.component.html @@ -26,7 +26,7 @@ <div class="col-md-5 add-url-component"> <div class="input-group"> <div class="input-group-prepend"> - <span class="input-group-text">Video quality</span> + <span class="input-group-text">Quality</span> </div> <select class="custom-select" name="quality" [(ngModel)]="quality" (change)="qualityChanged()" [disabled]="addInProgress || downloads.loading"> <option *ngFor="let q of qualities" [ngValue]="q.id">{{ q.text }}</option> diff --git a/ui/src/app/app.component.ts b/ui/src/app/app.component.ts index 55f7e6e..26ae3c5 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,19 +14,9 @@ import { MasterCheckboxComponent } from './master-checkbox.component'; }) export class AppComponent implements AfterViewInit { addUrl: string; - qualities: Array<Object> = [ - {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<Object> = [ - {id: "any", text: "Any"}, - {id: "mp4", text: "MP4"} - ]; format: string; addInProgress = false; @@ -42,8 +33,10 @@ 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() + this.quality = cookieService.get('metube_quality') || 'best'; } ngAfterViewInit() { @@ -76,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) { @@ -86,6 +81,13 @@ 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 + 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 diff --git a/ui/src/app/formats.ts b/ui/src/app/formats.ts new file mode 100644 index 0000000..15be903 --- /dev/null +++ b/ui/src/app/formats.ts @@ -0,0 +1,46 @@ +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: '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: '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: '320', text: '320 kbps' }, + { id: '192', text: '192 kbps' }, + { id: '128', text: '128 kbps' }, + ], + }, +]; |