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