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)
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 // ---------------------------------------------------------------------------
54 downloadYoutubeDLVideo,
58 // ---------------------------------------------------------------------------
60 async function safeGetYoutubeDL () {
64 youtubeDL = require('youtube-dl')
67 await YoutubeDlUpdateScheduler.Instance.execute()
68 youtubeDL = require('youtube-dl')
74 function normalizeObject (obj: any) {
75 const newObj: any = {}
77 for (const key of Object.keys(obj)) {
79 if (key === 'resolution') continue
81 const value = obj[key]
83 if (typeof value === 'string') {
84 newObj[key] = value.normalize()
93 function buildVideoInfo (obj: any) {
95 name: titleTruncation(obj.title),
96 description: descriptionTruncation(obj.description),
97 category: getCategory(obj.categories),
98 licence: getLicence(obj.license),
100 tags: getTags(obj.tags),
101 thumbnailUrl: obj.thumbnail || undefined
105 function titleTruncation (title: string) {
106 return truncate(title, {
107 'length': CONSTRAINTS_FIELDS.VIDEOS.NAME.max,
113 function descriptionTruncation (description: string) {
114 if (!description || description.length < CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.min) return undefined
116 return truncate(description, {
117 'length': CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max,
123 function isNSFW (info: any) {
124 return info.age_limit && info.age_limit >= 16
127 function getTags (tags: any) {
128 if (Array.isArray(tags) === false) return []
131 .filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min)
132 .map(t => t.normalize())
136 function getLicence (licence: string) {
137 if (!licence) return undefined
139 if (licence.indexOf('Creative Commons Attribution') !== -1) return 1
144 function getCategory (categories: string[]) {
145 if (!categories) return undefined
147 const categoryString = categories[0]
148 if (!categoryString || typeof categoryString !== 'string') return undefined
150 if (categoryString === 'News & Politics') return 11
152 for (const key of Object.keys(VIDEO_CATEGORIES)) {
153 const category = VIDEO_CATEGORIES[key]
154 if (categoryString.toLowerCase() === category.toLowerCase()) return parseInt(key, 10)