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'
7 export type YoutubeDLInfo = {
17 function getYoutubeDLInfo (url: string, opts?: string[]): Promise<YoutubeDLInfo> {
18 return new Promise<YoutubeDLInfo>(async (res, rej) => {
19 const options = opts || [ '-j', '--flat-playlist' ]
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.'))
26 const obj = buildVideoInfo(normalizeObject(info))
27 if (obj.name && obj.name.length < CONSTRAINTS_FIELDS.VIDEOS.NAME.min) obj.name += ' video'
34 function downloadYoutubeDLVideo (url: string) {
35 const path = generateVideoTmpPath(url)
37 logger.info('Importing youtubeDL video %s', url)
39 const options = [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best', '-o', path ]
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)
51 async function safeGetYoutubeDL () {
55 youtubeDL = require('youtube-dl')
58 await YoutubeDlUpdateScheduler.Instance.execute()
59 youtubeDL = require('youtube-dl')
65 // ---------------------------------------------------------------------------
68 downloadYoutubeDLVideo,
73 // ---------------------------------------------------------------------------
75 function normalizeObject (obj: any) {
76 const newObj: any = {}
78 for (const key of Object.keys(obj)) {
80 if (key === 'resolution') continue
82 const value = obj[key]
84 if (typeof value === 'string') {
85 newObj[key] = value.normalize()
94 function buildVideoInfo (obj: any) {
96 name: titleTruncation(obj.title),
97 description: descriptionTruncation(obj.description),
98 category: getCategory(obj.categories),
99 licence: getLicence(obj.license),
101 tags: getTags(obj.tags),
102 thumbnailUrl: obj.thumbnail || undefined
106 function titleTruncation (title: string) {
107 return truncate(title, {
108 'length': CONSTRAINTS_FIELDS.VIDEOS.NAME.max,
114 function descriptionTruncation (description: string) {
115 if (!description || description.length < CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.min) return undefined
117 return truncate(description, {
118 'length': CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max,
124 function isNSFW (info: any) {
125 return info.age_limit && info.age_limit >= 16
128 function getTags (tags: any) {
129 if (Array.isArray(tags) === false) return []
132 .filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min)
133 .map(t => t.normalize())
137 function getLicence (licence: string) {
138 if (!licence) return undefined
140 if (licence.indexOf('Creative Commons Attribution') !== -1) return 1
145 function getCategory (categories: string[]) {
146 if (!categories) return undefined
148 const categoryString = categories[0]
149 if (!categoryString || typeof categoryString !== 'string') return undefined
151 if (categoryString === 'News & Politics') return 11
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)