This repository has been archived by the owner on Sep 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
menu.js
206 lines (180 loc) · 4.74 KB
/
menu.js
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
196
197
198
199
200
201
202
203
204
205
206
const inquirer = require('inquirer')
const byteSize = require('byte-size')
exports.formatMenu = async function(formats) {
// Select video preset or resolution (it may include a audio track)
let {
finalFormatId,
videoFormatId,
audioFormatId,
height
} = await selectPresetOrResolution(formats)
// FinalFormatId means no further selection is required
if (!finalFormatId) {
let videoFormat
// Specifiy which video with this height
if (height) {
videoFormat = await exports.selectOneVideo(
formats.filter(f => f.height === height)
)
videoFormatId = videoFormat.format_id
}
// Specify audio track
audioFormatId = await selectAudio(formats, videoFormat)
}
if (finalFormatId) {
return {formatString: finalFormatId, hasVideo: true, hasAudio: true}
}
if (videoFormatId && audioFormatId) {
if (videoFormatId === audioFormatId) {
return {formatString: videoFormatId, hasVideo: true, hasAudio: true}
}
return {
formatString: `${videoFormatId}+${audioFormatId}`,
hasVideo: true,
hasAudio: true
}
}
if (videoFormatId) {
// The special case 'video only' has no audio
return {formatString: videoFormatId, hasVideo: true, hasAudio: false}
}
// The special case 'audio only' has no video data
return {formatString: audioFormatId, hasVideo: false, hasAudio: true}
}
async function selectPresetOrResolution(formats) {
const answers = await inquirer.prompt([
{
type: 'list',
name: 'q',
message: 'What do you want?',
pageSize: 10,
choices: [
{
name: 'best video + best audio',
value: {finalFormatId: 'bestvideo+bestaudio/best'}
},
{
name: 'worst video + worst audio',
value: {finalFormatId: 'worstvideo+worstaudio/worst'}
},
{
name: '<480p mp4',
value: {
finalFormatId:
'bestvideo[height<=480][ext=mp4]+bestaudio[ext=m4a]/best[height<=480][ext=mp4]'
}
},
{name: 'audio only', value: {}},
new inquirer.Separator('--- specify resolution: ---'),
...getResolutions(formats).map(resolution => ({
name: resolution + 'p',
value: {height: resolution}
}))
]
}
])
return answers.q
}
exports.selectOneVideo = async function(formats) {
const answers = await inquirer.prompt([
{
type: 'list',
name: 'q',
message: 'Select a video file:',
choices: formats.map(f => ({
name: exports.createVideoDescription(f),
value: f
}))
}
])
return answers.q
}
async function selectAudio(formats, selectedVideoFormat) {
const choices = []
if (selectedVideoFormat && selectedVideoFormat.acodec !== 'none') {
choices.push({
name: `Use audio included in the video: ${exports.createAudioShortDescription(
selectedVideoFormat
)}`,
value: selectedVideoFormat.format_id
})
choices.push(new inquirer.Separator())
}
choices.push({name: 'best audio', value: 'bestaudio'})
choices.push({name: 'worst audio', value: 'worstaudio'})
choices.push(new inquirer.Separator('--- specify: ---'))
choices.push(
...getAudioFormats(formats).map(f => ({
name: exports.createAudioDescription(f),
value: f.format_id
}))
)
const audioAnswers = await inquirer.prompt([
{
message: 'Select audio:',
type: 'list',
name: 'q',
pageSize: 10,
choices
}
])
return audioAnswers.q
}
function getResolutions(formats) {
const resolutions = formats.filter(f => Boolean(f.height)).map(f => f.height)
return [...new Set(resolutions)].sort(f => f.height).reverse()
}
function getAudioFormats(formats) {
return formats.filter(f => f.acodec && f.acodec !== 'none')
}
exports.createVideoDescription = function(f) {
return (
paddingRight(f.ext, 4) +
paddingRight(f.width ? f.width + 'x' + f.height : null, 9) +
paddingRight(f.format_note, 10) +
paddingRight(byteSize(f.filesize, {units: 'iec'}), 8) +
exports.createAudioShortDescription(f, 'audio: ') +
'(' +
f.format_id +
')'
)
}
const paddingRight = function(value, width) {
if (!value) {
value = ''
}
// Value might be an object. Wrap it so we can call padEnd on it.
value = String(value)
return value.padEnd(width) + ' '
}
const paddingLeft = function(value, width, suffix = '') {
if (value) {
value += suffix
} else {
value = ''
}
// Value might be an object. Wrap it so we can call padEnd on it.
value = String(value)
return value.padStart(width) + ' '
}
exports.createAudioDescription = function(f) {
return (
paddingRight(f.ext, 4) +
paddingRight(f.acodec, 9) +
'@ ' +
paddingLeft(f.abr, 3, 'k') +
paddingRight(f.format_note, 10) +
paddingLeft(byteSize(f.filesize, {units: 'iec'}), 7) +
'(' +
f.format_id +
')'
)
}
exports.createAudioShortDescription = function(f, prefix = '') {
if (f.acodec && f.acodec !== 'none') {
return (
prefix + paddingRight(f.acodec, 9) + '@ ' + paddingLeft(f.abr, 3, 'k')
)
}
return ''
}