-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.ts
195 lines (181 loc) · 6.06 KB
/
info.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* @file : info.ts
* @summary : video info command
* @version : 1.0.0
* @project : YtKit
* @description : displays information about a video
* @author : Benjamin Maggi
* @email : [email protected]
* @date : 05 Jul 2021
* @license: : MIT
*
* Copyright 2021 Benjamin Maggi <[email protected]>
*
*
* License:
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import ytdl = require('ytdl-core');
import { AnyJson } from '@salesforce/ts-types';
import * as utils from '../utils/utils';
import { YtKitCommand } from '../YtKitCommand';
import { flags, FlagsConfig } from '../YtKitFlags';
export interface IOutputVideoMeta {
label: string;
path: string;
requires?: string | boolean | ((value: unknown) => boolean);
transformValue?: (number: number) => string;
}
export default class Info extends YtKitCommand {
public static id = 'info';
public static readonly description = 'display information about a video';
public static readonly examples = ['$ ytdl info -u https://www.youtube.com/watch?v=aqz-KE-bpKQ'];
public static readonly flagsConfig: FlagsConfig = {
url: flags.string({
char: 'u',
description: 'Youtube video or playlist url',
required: true,
}),
formats: flags.boolean({
char: 'f',
description: 'Display available video formats',
}),
};
protected videoInfo?: ytdl.videoInfo;
public async run(): Promise<ytdl.videoInfo | undefined> {
this.videoInfo = await this.getVideoInfo();
if (this.videoInfo) {
this.showVideoInfo();
}
return this.videoInfo;
}
/**
* Prepares video metadata information.
*
* @returns {IOutputVideoMeta[]} a collection of labels and values to print
*/
private prepareVideoMetaBatch(): IOutputVideoMeta[] {
return [
{
label: 'title',
path: 'title',
},
{
label: 'author',
path: 'author.name',
},
{
label: 'avg rating',
path: 'averageRating',
},
{
label: 'views',
path: 'viewCount',
},
{
label: 'publish date',
path: 'publishDate',
},
{
label: 'length',
path: 'lengthSeconds',
requires: utils.getValueFrom<ytdl.videoFormat[]>(this.videoInfo, 'formats').some((format) => format.isLive),
transformValue: utils.toHumanTime,
},
];
}
/**
* Print stilized output
*
* @param {string} label the label for the value
* @param {string} value the value to print
*/
private logStyledProp(label: string, value: string): void {
this.ux.log(`${this.ux.chalk.bold.gray(label)}: ${value}`);
}
/**
* Prints video metadata information.
*
* @returns {void}
*/
private printVideoMeta(): void {
this.prepareVideoMetaBatch().forEach((outputVideoMeta: IOutputVideoMeta) => {
const { label } = outputVideoMeta;
const value = utils.getValueFrom<string>(this.videoInfo, `videoDetails.${outputVideoMeta.path}`, '');
if (!outputVideoMeta.requires) {
if (outputVideoMeta.transformValue) {
return this.logStyledProp(label, outputVideoMeta.transformValue(parseInt(value, 10)));
}
return this.logStyledProp(label, value);
}
});
}
/**
* Prepares table rows.
*
* @returns {AnyJson}
*/
private prepareTableRows(): AnyJson {
return utils.getValueFrom<ytdl.videoFormat[]>(this.videoInfo, 'formats').map((format) => ({
itag: utils.getValueFrom<string>(format, 'itag', ''),
container: utils.getValueFrom<string>(format, 'container', ''),
quality: utils.getValueFrom<string>(format, 'qualityLabel', ''),
codecs: this.getCodec(format),
/* istanbul ignore else */
bitrate: utils.getValueFromMeta(format, 'bitrate', format.qualityLabel, '', utils.toHumanSize),
'audio bitrate': utils.getValueFromMeta(format, 'audioBitrate', format.audioBitrate, '', utils.toHumanSize),
size: utils.getValueFromMeta(format, 'contentLength', format.contentLength, '', utils.toHumanSize),
}));
}
private getCodec(format: ytdl.videoFormat): string {
return utils.getValueFrom<string>(format, 'codecs', '');
}
/**
* Prints video format information.
*
* @param {Object} videoInfo the video info object
* @returns {Promise<void>}
*/
private printVideoFormats(): void {
const headers = ['itag', 'container', 'quality', 'codecs', 'bitrate', 'audio bitrate', 'size'];
this.ux.table(this.prepareTableRows(), headers);
}
/**
* Prints basic video information.
*
* @param {Object} videoInfo the video info object
* @returns {Promise<void>}
*/
private showVideoInfo(): void {
if (this.flags.formats) {
this.printVideoFormats();
} else {
this.printVideoMeta();
}
}
/**
* Gets info from a video additional formats and deciphered URLs.
*
* @returns {Promise<ytdl.videoInfo | undefined>} the video info object or undefined if it fails
*/
private async getVideoInfo(): Promise<ytdl.videoInfo | undefined> {
return await ytdl.getInfo(this.flags.url);
}
}