Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(FormatUtils): choose more specific format by itag or codec #884

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/types/FormatUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
*/
Expand All @@ -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.
*/
Expand All @@ -35,4 +43,4 @@ export interface DownloadOptions extends FormatOptions {
start: number;
end: number;
}
}
}
4 changes: 4 additions & 0 deletions src/utils/FormatUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading