]>
Commit | Line | Data |
---|---|---|
fbad87b0 C |
1 | import * as youtubeDL from 'youtube-dl' |
2 | import { truncate } from 'lodash' | |
ce33919c | 3 | import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES } from '../initializers' |
fbad87b0 | 4 | import { logger } from './logger' |
ce33919c | 5 | import { generateVideoTmpPath } from './utils' |
fbad87b0 C |
6 | |
7 | export type YoutubeDLInfo = { | |
ce33919c C |
8 | name?: string |
9 | description?: string | |
10 | category?: number | |
11 | licence?: number | |
12 | nsfw?: boolean | |
13 | tags?: string[] | |
14 | thumbnailUrl?: string | |
fbad87b0 C |
15 | } |
16 | ||
17 | function getYoutubeDLInfo (url: string): Promise<YoutubeDLInfo> { | |
18 | return new Promise<YoutubeDLInfo>((res, rej) => { | |
19 | const options = [ '-j', '--flat-playlist' ] | |
20 | ||
21 | youtubeDL.getInfo(url, options, (err, info) => { | |
22 | if (err) return rej(err) | |
23 | ||
24 | const obj = normalizeObject(info) | |
25 | ||
26 | return res(buildVideoInfo(obj)) | |
27 | }) | |
28 | }) | |
29 | } | |
30 | ||
31 | function downloadYoutubeDLVideo (url: string) { | |
ce33919c | 32 | const path = generateVideoTmpPath(url) |
fbad87b0 | 33 | |
ce33919c | 34 | logger.info('Importing youtubeDL video %s', url) |
fbad87b0 C |
35 | |
36 | const options = [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best', '-o', path ] | |
37 | ||
38 | return new Promise<string>((res, rej) => { | |
39 | youtubeDL.exec(url, options, async (err, output) => { | |
40 | if (err) return rej(err) | |
41 | ||
42 | return res(path) | |
43 | }) | |
44 | }) | |
45 | } | |
46 | ||
47 | // --------------------------------------------------------------------------- | |
48 | ||
49 | export { | |
50 | downloadYoutubeDLVideo, | |
51 | getYoutubeDLInfo | |
52 | } | |
53 | ||
54 | // --------------------------------------------------------------------------- | |
55 | ||
56 | function normalizeObject (obj: any) { | |
57 | const newObj: any = {} | |
58 | ||
59 | for (const key of Object.keys(obj)) { | |
60 | // Deprecated key | |
61 | if (key === 'resolution') continue | |
62 | ||
63 | const value = obj[key] | |
64 | ||
65 | if (typeof value === 'string') { | |
66 | newObj[key] = value.normalize() | |
67 | } else { | |
68 | newObj[key] = value | |
69 | } | |
70 | } | |
71 | ||
72 | return newObj | |
73 | } | |
74 | ||
75 | function buildVideoInfo (obj: any) { | |
76 | return { | |
77 | name: titleTruncation(obj.title), | |
78 | description: descriptionTruncation(obj.description), | |
79 | category: getCategory(obj.categories), | |
80 | licence: getLicence(obj.license), | |
81 | nsfw: isNSFW(obj), | |
82 | tags: getTags(obj.tags), | |
83 | thumbnailUrl: obj.thumbnail || undefined | |
84 | } | |
85 | } | |
86 | ||
87 | function titleTruncation (title: string) { | |
88 | return truncate(title, { | |
89 | 'length': CONSTRAINTS_FIELDS.VIDEOS.NAME.max, | |
90 | 'separator': /,? +/, | |
91 | 'omission': ' […]' | |
92 | }) | |
93 | } | |
94 | ||
95 | function descriptionTruncation (description: string) { | |
ed31c059 | 96 | if (!description || description.length < CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.min) return undefined |
fbad87b0 C |
97 | |
98 | return truncate(description, { | |
99 | 'length': CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max, | |
100 | 'separator': /,? +/, | |
101 | 'omission': ' […]' | |
102 | }) | |
103 | } | |
104 | ||
105 | function isNSFW (info: any) { | |
106 | return info.age_limit && info.age_limit >= 16 | |
107 | } | |
108 | ||
109 | function getTags (tags: any) { | |
110 | if (Array.isArray(tags) === false) return [] | |
111 | ||
112 | return tags | |
113 | .filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min) | |
114 | .map(t => t.normalize()) | |
115 | .slice(0, 5) | |
116 | } | |
117 | ||
118 | function getLicence (licence: string) { | |
119 | if (!licence) return undefined | |
120 | ||
590fb506 | 121 | if (licence.indexOf('Creative Commons Attribution') !== -1) return 1 |
fbad87b0 C |
122 | |
123 | return undefined | |
124 | } | |
125 | ||
126 | function getCategory (categories: string[]) { | |
127 | if (!categories) return undefined | |
128 | ||
129 | const categoryString = categories[0] | |
130 | if (!categoryString || typeof categoryString !== 'string') return undefined | |
131 | ||
132 | if (categoryString === 'News & Politics') return 11 | |
133 | ||
134 | for (const key of Object.keys(VIDEO_CATEGORIES)) { | |
135 | const category = VIDEO_CATEGORIES[key] | |
136 | if (categoryString.toLowerCase() === category.toLowerCase()) return parseInt(key, 10) | |
137 | } | |
138 | ||
139 | return undefined | |
140 | } |