From 1c1577e85fd46cbfa15bcee6531d9aafdda787e5 Mon Sep 17 00:00:00 2001 From: Loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com> Date: Wed, 29 Jan 2025 02:43:09 +0800 Subject: [PATCH] feat(FormatUtils): choose more specific format by itag or codec (#884) --- src/types/FormatUtils.ts | 10 +++++++++- src/utils/FormatUtils.ts | 4 ++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/types/FormatUtils.ts b/src/types/FormatUtils.ts index 748bb251..f6a21e2a 100644 --- a/src/types/FormatUtils.ts +++ b/src/types/FormatUtils.ts @@ -5,6 +5,10 @@ export type URLTransformer = (url: URL) => URL; export type FormatFilter = (format: Format) => boolean; export interface FormatOptions { + /** + * Video or audio itag + */ + itag?: number; /** * Video quality; 360p, 720p, 1080p, etc... also accepts 'best' and 'bestefficiency'. */ @@ -21,6 +25,10 @@ export interface FormatOptions { * File format, use 'any' to download any format */ format?: string; + /** + * Video or audio codec, e.g. 'avc', 'vp9', 'av01' for video, 'opus', 'mp4a' for audio + */ + codec?: string; /** * InnerTube client. */ @@ -35,4 +43,4 @@ export interface DownloadOptions extends FormatOptions { start: number; end: number; } -} \ No newline at end of file +} diff --git a/src/utils/FormatUtils.ts b/src/utils/FormatUtils.ts index 282ec7d6..209a40bd 100644 --- a/src/utils/FormatUtils.ts +++ b/src/utils/FormatUtils.ts @@ -155,10 +155,14 @@ export function chooseFormat(options: FormatOptions, streaming_data?: IStreaming const use_most_efficient = quality !== 'best'; let candidates = formats.filter((format) => { + if (options.itag && format.itag !== options.itag) + return false; if (requires_audio && !format.has_audio) return false; if (requires_video && !format.has_video) return false; + if (options.codec && !format.mime_type.includes(options.codec)) + return false; if (options.format !== 'any' && !format.mime_type.includes(options.format || 'mp4')) return false; if (!is_best && format.quality_label !== quality)