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