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