]>
Commit | Line | Data |
---|---|---|
fbad87b0 | 1 | import { truncate } from 'lodash' |
ce33919c | 2 | import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES } from '../initializers' |
fbad87b0 | 3 | import { logger } from './logger' |
ce33919c | 4 | import { generateVideoTmpPath } from './utils' |
606c946e C |
5 | import { join } from 'path' |
6 | import { root } from './core-utils' | |
cf9166cf | 7 | import { ensureDir, writeFile, remove } from 'fs-extra' |
606c946e C |
8 | import * as request from 'request' |
9 | import { createWriteStream } from 'fs' | |
fbad87b0 C |
10 | |
11 | export type YoutubeDLInfo = { | |
ce33919c C |
12 | name?: string |
13 | description?: string | |
14 | category?: number | |
15 | licence?: number | |
16 | nsfw?: boolean | |
17 | tags?: string[] | |
18 | thumbnailUrl?: string | |
fbad87b0 C |
19 | } |
20 | ||
cc680494 C |
21 | const processOptions = { |
22 | maxBuffer: 1024 * 1024 * 10 // 10MB | |
23 | } | |
24 | ||
8704acf4 | 25 | function getYoutubeDLInfo (url: string, opts?: string[]): Promise<YoutubeDLInfo> { |
4f1f6f03 | 26 | return new Promise<YoutubeDLInfo>(async (res, rej) => { |
8704acf4 | 27 | const options = opts || [ '-j', '--flat-playlist' ] |
fbad87b0 | 28 | |
4f1f6f03 | 29 | const youtubeDL = await safeGetYoutubeDL() |
fbad87b0 C |
30 | youtubeDL.getInfo(url, options, (err, info) => { |
31 | if (err) return rej(err) | |
1a893f9c | 32 | if (info.is_live === true) return rej(new Error('Cannot download a live streaming.')) |
fbad87b0 | 33 | |
5d112d0c C |
34 | const obj = buildVideoInfo(normalizeObject(info)) |
35 | if (obj.name && obj.name.length < CONSTRAINTS_FIELDS.VIDEOS.NAME.min) obj.name += ' video' | |
fbad87b0 | 36 | |
5d112d0c | 37 | return res(obj) |
fbad87b0 C |
38 | }) |
39 | }) | |
40 | } | |
41 | ||
cf9166cf | 42 | function downloadYoutubeDLVideo (url: string, timeout: number) { |
ce33919c | 43 | const path = generateVideoTmpPath(url) |
cf9166cf | 44 | let timer |
fbad87b0 | 45 | |
ce33919c | 46 | logger.info('Importing youtubeDL video %s', url) |
fbad87b0 C |
47 | |
48 | const options = [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best', '-o', path ] | |
49 | ||
4f1f6f03 C |
50 | return new Promise<string>(async (res, rej) => { |
51 | const youtubeDL = await safeGetYoutubeDL() | |
cc680494 | 52 | youtubeDL.exec(url, options, processOptions, err => { |
cf9166cf C |
53 | clearTimeout(timer) |
54 | ||
55 | if (err) { | |
56 | remove(path) | |
57 | .catch(err => logger.error('Cannot delete path on YoutubeDL error.', { err })) | |
58 | ||
59 | return rej(err) | |
60 | } | |
fbad87b0 C |
61 | |
62 | return res(path) | |
63 | }) | |
cf9166cf C |
64 | |
65 | timer = setTimeout(async () => { | |
66 | await remove(path) | |
67 | ||
68 | return rej(new Error('YoutubeDL download timeout.')) | |
69 | }, timeout) | |
fbad87b0 C |
70 | }) |
71 | } | |
72 | ||
606c946e C |
73 | // Thanks: https://github.com/przemyslawpluta/node-youtube-dl/blob/master/lib/downloader.js |
74 | // We rewrote it to avoid sync calls | |
75 | async function updateYoutubeDLBinary () { | |
76 | logger.info('Updating youtubeDL binary.') | |
77 | ||
78 | const binDirectory = join(root(), 'node_modules', 'youtube-dl', 'bin') | |
79 | const bin = join(binDirectory, 'youtube-dl') | |
80 | const detailsPath = join(binDirectory, 'details') | |
81 | const url = 'https://yt-dl.org/downloads/latest/youtube-dl' | |
82 | ||
83 | await ensureDir(binDirectory) | |
84 | ||
85 | return new Promise(res => { | |
86 | request.get(url, { followRedirect: false }, (err, result) => { | |
87 | if (err) { | |
88 | logger.error('Cannot update youtube-dl.', { err }) | |
89 | return res() | |
90 | } | |
91 | ||
92 | if (result.statusCode !== 302) { | |
93 | logger.error('youtube-dl update error: did not get redirect for the latest version link. Status %d', result.statusCode) | |
94 | return res() | |
95 | } | |
96 | ||
97 | const url = result.headers.location | |
98 | const downloadFile = request.get(url) | |
99 | const newVersion = /yt-dl\.org\/downloads\/(\d{4}\.\d\d\.\d\d(\.\d)?)\/youtube-dl/.exec(url)[ 1 ] | |
100 | ||
101 | downloadFile.on('response', result => { | |
102 | if (result.statusCode !== 200) { | |
103 | logger.error('Cannot update youtube-dl: new version response is not 200, it\'s %d.', result.statusCode) | |
104 | return res() | |
105 | } | |
106 | ||
107 | downloadFile.pipe(createWriteStream(bin, { mode: 493 })) | |
108 | }) | |
109 | ||
110 | downloadFile.on('error', err => { | |
111 | logger.error('youtube-dl update error.', { err }) | |
112 | return res() | |
113 | }) | |
114 | ||
115 | downloadFile.on('end', () => { | |
116 | const details = JSON.stringify({ version: newVersion, path: bin, exec: 'youtube-dl' }) | |
117 | writeFile(detailsPath, details, { encoding: 'utf8' }, err => { | |
118 | if (err) { | |
119 | logger.error('youtube-dl update error: cannot write details.', { err }) | |
120 | return res() | |
121 | } | |
122 | ||
123 | logger.info('youtube-dl updated to version %s.', newVersion) | |
124 | return res() | |
125 | }) | |
126 | }) | |
127 | }) | |
128 | }) | |
129 | } | |
130 | ||
4f1f6f03 C |
131 | async function safeGetYoutubeDL () { |
132 | let youtubeDL | |
133 | ||
134 | try { | |
135 | youtubeDL = require('youtube-dl') | |
136 | } catch (e) { | |
137 | // Download binary | |
606c946e | 138 | await updateYoutubeDLBinary() |
4f1f6f03 C |
139 | youtubeDL = require('youtube-dl') |
140 | } | |
141 | ||
142 | return youtubeDL | |
143 | } | |
144 | ||
8704acf4 RK |
145 | // --------------------------------------------------------------------------- |
146 | ||
147 | export { | |
0491173a | 148 | updateYoutubeDLBinary, |
8704acf4 RK |
149 | downloadYoutubeDLVideo, |
150 | getYoutubeDLInfo, | |
151 | safeGetYoutubeDL | |
152 | } | |
153 | ||
154 | // --------------------------------------------------------------------------- | |
155 | ||
fbad87b0 C |
156 | function normalizeObject (obj: any) { |
157 | const newObj: any = {} | |
158 | ||
159 | for (const key of Object.keys(obj)) { | |
160 | // Deprecated key | |
161 | if (key === 'resolution') continue | |
162 | ||
163 | const value = obj[key] | |
164 | ||
165 | if (typeof value === 'string') { | |
166 | newObj[key] = value.normalize() | |
167 | } else { | |
168 | newObj[key] = value | |
169 | } | |
170 | } | |
171 | ||
172 | return newObj | |
173 | } | |
174 | ||
175 | function buildVideoInfo (obj: any) { | |
176 | return { | |
177 | name: titleTruncation(obj.title), | |
178 | description: descriptionTruncation(obj.description), | |
179 | category: getCategory(obj.categories), | |
180 | licence: getLicence(obj.license), | |
181 | nsfw: isNSFW(obj), | |
182 | tags: getTags(obj.tags), | |
183 | thumbnailUrl: obj.thumbnail || undefined | |
184 | } | |
185 | } | |
186 | ||
187 | function titleTruncation (title: string) { | |
188 | return truncate(title, { | |
189 | 'length': CONSTRAINTS_FIELDS.VIDEOS.NAME.max, | |
190 | 'separator': /,? +/, | |
191 | 'omission': ' […]' | |
192 | }) | |
193 | } | |
194 | ||
195 | function descriptionTruncation (description: string) { | |
ed31c059 | 196 | if (!description || description.length < CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.min) return undefined |
fbad87b0 C |
197 | |
198 | return truncate(description, { | |
199 | 'length': CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max, | |
200 | 'separator': /,? +/, | |
201 | 'omission': ' […]' | |
202 | }) | |
203 | } | |
204 | ||
205 | function isNSFW (info: any) { | |
206 | return info.age_limit && info.age_limit >= 16 | |
207 | } | |
208 | ||
209 | function getTags (tags: any) { | |
210 | if (Array.isArray(tags) === false) return [] | |
211 | ||
212 | return tags | |
213 | .filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min) | |
214 | .map(t => t.normalize()) | |
215 | .slice(0, 5) | |
216 | } | |
217 | ||
218 | function getLicence (licence: string) { | |
219 | if (!licence) return undefined | |
220 | ||
590fb506 | 221 | if (licence.indexOf('Creative Commons Attribution') !== -1) return 1 |
fbad87b0 C |
222 | |
223 | return undefined | |
224 | } | |
225 | ||
226 | function getCategory (categories: string[]) { | |
227 | if (!categories) return undefined | |
228 | ||
229 | const categoryString = categories[0] | |
230 | if (!categoryString || typeof categoryString !== 'string') return undefined | |
231 | ||
232 | if (categoryString === 'News & Politics') return 11 | |
233 | ||
234 | for (const key of Object.keys(VIDEO_CATEGORIES)) { | |
235 | const category = VIDEO_CATEGORIES[key] | |
236 | if (categoryString.toLowerCase() === category.toLowerCase()) return parseInt(key, 10) | |
237 | } | |
238 | ||
239 | return undefined | |
240 | } |