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): Promise<YoutubeDLInfo> {
18 return new Promise<YoutubeDLInfo>(async (res, rej) => {
19 const options = [ '-j', '--flat-playlist' ]
21 const youtubeDL = await safeGetYoutubeDL()
22 youtubeDL.getInfo(url, options, (err, info) => {
23 if (err) return rej(err)
25 const obj = normalizeObject(info)
27 return res(buildVideoInfo(obj))
32 function downloadYoutubeDLVideo (url: string) {
33 const path = generateVideoTmpPath(url)
35 logger.info('Importing youtubeDL video %s', url)
37 const options = [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best', '-o', path ]
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)
49 // ---------------------------------------------------------------------------
52 downloadYoutubeDLVideo,
56 // ---------------------------------------------------------------------------
58 async function safeGetYoutubeDL () {
62 youtubeDL = require('youtube-dl')
65 await YoutubeDlUpdateScheduler.Instance.execute()
66 youtubeDL = require('youtube-dl')
72 function normalizeObject (obj: any) {
73 const newObj: any = {}
75 for (const key of Object.keys(obj)) {
77 if (key === 'resolution') continue
79 const value = obj[key]
81 if (typeof value === 'string') {
82 newObj[key] = value.normalize()
91 function buildVideoInfo (obj: any) {
93 name: titleTruncation(obj.title),
94 description: descriptionTruncation(obj.description),
95 category: getCategory(obj.categories),
96 licence: getLicence(obj.license),
98 tags: getTags(obj.tags),
99 thumbnailUrl: obj.thumbnail || undefined
103 function titleTruncation (title: string) {
104 return truncate(title, {
105 'length': CONSTRAINTS_FIELDS.VIDEOS.NAME.max,
111 function descriptionTruncation (description: string) {
112 if (!description || description.length < CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.min) return undefined
114 return truncate(description, {
115 'length': CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max,
121 function isNSFW (info: any) {
122 return info.age_limit && info.age_limit >= 16
125 function getTags (tags: any) {
126 if (Array.isArray(tags) === false) return []
129 .filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min)
130 .map(t => t.normalize())
134 function getLicence (licence: string) {
135 if (!licence) return undefined
137 if (licence.indexOf('Creative Commons Attribution') !== -1) return 1
142 function getCategory (categories: string[]) {
143 if (!categories) return undefined
145 const categoryString = categories[0]
146 if (!categoryString || typeof categoryString !== 'string') return undefined
148 if (categoryString === 'News & Politics') return 11
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)