|
1 | 1 | const inquirer = require('inquirer')
|
2 | 2 | const byteSize = require('byte-size')
|
3 | 3 |
|
| 4 | +exports.formatMenu = async function(formats) { |
| 5 | + // Select video preset or resolution (it may include a audio track) |
| 6 | + let { |
| 7 | + finalFormatId, |
| 8 | + videoFormatId, |
| 9 | + audioFormatId, |
| 10 | + height |
| 11 | + } = await selectPresetOrResolution(formats) |
4 | 12 |
|
5 |
| -exports.formatMenu = async function (formats) { |
6 |
| - let remainingFormats = formats |
| 13 | + // FinalFormatId means no further selection is required |
| 14 | + if (!finalFormatId) { |
| 15 | + let videoFormat |
| 16 | + // Specifiy which video with this height |
| 17 | + if (height) { |
| 18 | + videoFormat = await exports.selectOneVideo( |
| 19 | + formats.filter(f => f.height === height) |
| 20 | + ) |
| 21 | + videoFormatId = videoFormat.format_id |
| 22 | + } |
7 | 23 |
|
8 |
| - // By default, we ignore 'video only' files |
9 |
| - remainingFormats = remainingFormats.filter(f => f.acodec !== 'none') |
| 24 | + // Specify audio track |
| 25 | + audioFormatId = await selectAudio(formats, videoFormat) |
| 26 | + } |
10 | 27 |
|
11 |
| - remainingFormats = await exports.filterByProperty( |
12 |
| - 'Select resolution:', |
13 |
| - f => (f.resolution || 'audio only'), |
14 |
| - remainingFormats |
15 |
| - ) |
| 28 | + if (finalFormatId) { |
| 29 | + return {formatString: finalFormatId, hasVideo: true, hasAudio: true} |
| 30 | + } |
16 | 31 |
|
17 |
| - // remainingFormats = await exports.filterByProperty( |
18 |
| - // 'Select extension:', |
19 |
| - // f => f.extension, |
20 |
| - // remainingFormats |
21 |
| - // ) |
| 32 | + if (videoFormatId && audioFormatId) { |
| 33 | + if (videoFormatId === audioFormatId) { |
| 34 | + return {formatString: videoFormatId, hasVideo: true, hasAudio: true} |
| 35 | + } |
22 | 36 |
|
23 |
| - if (remainingFormats.length > 1) { |
24 |
| - remainingFormats = [await exports.selectOne(remainingFormats)] |
25 |
| - } |
| 37 | + return { |
| 38 | + formatString: `${videoFormatId}+${audioFormatId}`, |
| 39 | + hasVideo: true, |
| 40 | + hasAudio: true |
| 41 | + } |
| 42 | + } |
26 | 43 |
|
27 |
| - const formatString = remainingFormats[0].format_id |
28 |
| - return {formatString, extension: remainingFormats[0].ext} |
29 |
| -} |
| 44 | + if (videoFormatId) { |
| 45 | + // The special case 'video only' has no audio |
| 46 | + return {formatString: videoFormatId, hasVideo: true, hasAudio: false} |
| 47 | + } |
30 | 48 |
|
| 49 | + // The special case 'audio only' has no video data |
| 50 | + return {formatString: audioFormatId, hasVideo: false, hasAudio: true} |
| 51 | +} |
31 | 52 |
|
32 |
| -exports.filterByProperty = async function(message, displayFun, list) { |
| 53 | +async function selectPresetOrResolution(formats) { |
33 | 54 | const answers = await inquirer.prompt([
|
34 | 55 | {
|
35 | 56 | type: 'list',
|
36 | 57 | name: 'q',
|
37 |
| - message, |
38 |
| - choices: [...new Set(list.map(displayFun))] |
| 58 | + message: 'What do you want?', |
| 59 | + pageSize: 10, |
| 60 | + choices: [ |
| 61 | + { |
| 62 | + name: 'best video + best audio', |
| 63 | + value: {finalFormatId: 'bestvideo+bestaudio/best'} |
| 64 | + }, |
| 65 | + { |
| 66 | + name: 'worst video + worst audio', |
| 67 | + value: {finalFormatId: 'worstvideo+worstaudio/worst'} |
| 68 | + }, |
| 69 | + { |
| 70 | + name: '<480p mp4', |
| 71 | + value: { |
| 72 | + finalFormatId: |
| 73 | + 'bestvideo[height<=480][ext=mp4]+bestaudio[ext=m4a]/best[height<=480][ext=mp4]' |
| 74 | + } |
| 75 | + }, |
| 76 | + {name: 'audio only', value: {}}, |
| 77 | + new inquirer.Separator('--- specify resolution: ---'), |
| 78 | + ...getResolutions(formats).map(resolution => ({ |
| 79 | + name: resolution + 'p', |
| 80 | + value: {height: resolution} |
| 81 | + })) |
| 82 | + ] |
39 | 83 | }
|
40 | 84 | ])
|
41 |
| - return list.filter(f => displayFun(f) === answers.q) |
| 85 | + |
| 86 | + return answers.q |
42 | 87 | }
|
43 | 88 |
|
44 |
| -exports.selectOne = async function(formats) { |
| 89 | +exports.selectOneVideo = async function(formats) { |
45 | 90 | const answers = await inquirer.prompt([
|
46 | 91 | {
|
47 | 92 | type: 'list',
|
48 | 93 | name: 'q',
|
49 |
| - message: 'Several matches. Select an option:', |
50 |
| - choices: formats.map(f => { |
51 |
| - return {name: exports.createDescription(f), value: f} |
52 |
| - }) |
| 94 | + message: 'Select a video file:', |
| 95 | + choices: formats.map(f => ({ |
| 96 | + name: exports.createVideoDescription(f), |
| 97 | + value: f |
| 98 | + })) |
53 | 99 | }
|
54 | 100 | ])
|
55 | 101 | return answers.q
|
56 | 102 | }
|
57 |
| - |
58 | 103 |
|
59 |
| -exports.createDescription = function(f) { |
60 |
| - return `${f.ext} | ${f.format} (${byteSize(f.filesize, { units: 'iec' })})`; |
| 104 | +async function selectAudio(formats, selectedVideoFormat) { |
| 105 | + const choices = [] |
| 106 | + |
| 107 | + if (selectedVideoFormat && selectedVideoFormat.acodec !== 'none') { |
| 108 | + choices.push({ |
| 109 | + name: `Use audio included in the video: ${exports.createAudioShortDescription( |
| 110 | + selectedVideoFormat |
| 111 | + )}`, |
| 112 | + value: selectedVideoFormat.format_id |
| 113 | + }) |
| 114 | + choices.push(new inquirer.Separator()) |
| 115 | + } |
| 116 | + |
| 117 | + choices.push({name: 'best audio', value: 'bestaudio'}) |
| 118 | + choices.push({name: 'worst audio', value: 'worstaudio'}) |
| 119 | + choices.push(new inquirer.Separator('--- specify: ---')) |
| 120 | + choices.push( |
| 121 | + ...getAudioFormats(formats).map(f => ({ |
| 122 | + name: exports.createAudioDescription(f), |
| 123 | + value: f.format_id |
| 124 | + })) |
| 125 | + ) |
| 126 | + |
| 127 | + const audioAnswers = await inquirer.prompt([ |
| 128 | + { |
| 129 | + message: 'Select audio:', |
| 130 | + type: 'list', |
| 131 | + name: 'q', |
| 132 | + pageSize: 10, |
| 133 | + choices |
| 134 | + } |
| 135 | + ]) |
| 136 | + return audioAnswers.q |
| 137 | +} |
| 138 | + |
| 139 | +function getResolutions(formats) { |
| 140 | + const resolutions = formats.filter(f => Boolean(f.height)).map(f => f.height) |
| 141 | + |
| 142 | + return [...new Set(resolutions)].sort(f => f.height).reverse() |
| 143 | +} |
| 144 | + |
| 145 | +function getAudioFormats(formats) { |
| 146 | + return formats.filter(f => f.acodec && f.acodec !== 'none') |
| 147 | +} |
| 148 | + |
| 149 | +exports.createVideoDescription = function(f) { |
| 150 | + return ( |
| 151 | + paddingRight(f.ext, 4) + |
| 152 | + paddingRight(f.width ? f.width + 'x' + f.height : null, 9) + |
| 153 | + paddingRight(f.format_note, 10) + |
| 154 | + paddingRight(byteSize(f.filesize, {units: 'iec'}), 8) + |
| 155 | + exports.createAudioShortDescription(f, 'audio: ') + |
| 156 | + '(' + |
| 157 | + f.format_id + |
| 158 | + ')' |
| 159 | + ) |
| 160 | +} |
| 161 | + |
| 162 | +const paddingRight = function(value, width) { |
| 163 | + if (!value) { |
| 164 | + value = '' |
| 165 | + } |
| 166 | + |
| 167 | + // Value might be an object. Wrap it so we can call padEnd on it. |
| 168 | + value = String(value) |
| 169 | + return value.padEnd(width) + ' ' |
| 170 | +} |
| 171 | + |
| 172 | +const paddingLeft = function(value, width, suffix = '') { |
| 173 | + if (value) { |
| 174 | + value += suffix |
| 175 | + } else { |
| 176 | + value = '' |
| 177 | + } |
| 178 | + |
| 179 | + // Value might be an object. Wrap it so we can call padEnd on it. |
| 180 | + value = String(value) |
| 181 | + return value.padStart(width) + ' ' |
61 | 182 | }
|
62 | 183 |
|
| 184 | +exports.createAudioDescription = function(f) { |
| 185 | + return ( |
| 186 | + paddingRight(f.ext, 4) + |
| 187 | + paddingRight(f.acodec, 9) + |
| 188 | + '@ ' + |
| 189 | + paddingLeft(f.abr, 3, 'k') + |
| 190 | + paddingRight(f.format_note, 10) + |
| 191 | + paddingLeft(byteSize(f.filesize, {units: 'iec'}), 7) + |
| 192 | + '(' + |
| 193 | + f.format_id + |
| 194 | + ')' |
| 195 | + ) |
| 196 | +} |
| 197 | + |
| 198 | +exports.createAudioShortDescription = function(f, prefix = '') { |
| 199 | + if (f.acodec && f.acodec !== 'none') { |
| 200 | + return ( |
| 201 | + prefix + paddingRight(f.acodec, 9) + '@ ' + paddingLeft(f.abr, 3, 'k') |
| 202 | + ) |
| 203 | + } |
| 204 | + |
| 205 | + return '' |
| 206 | +} |
0 commit comments