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 = buildVideoInfo(normalizeObject(info))
26 if (obj.name && obj.name.length < CONSTRAINTS_FIELDS.VIDEOS.NAME.min) obj.name += ' video'
33 function downloadYoutubeDLVideo (url: string) {
34 const path = generateVideoTmpPath(url)
36 logger.info('Importing youtubeDL video %s', url)
38 const options = [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best', '-o', path ]
40 return new Promise<string>(async (res, rej) => {
41 const youtubeDL = await safeGetYoutubeDL()
42 youtubeDL.exec(url, options, async (err, output) => {
43 if (err) return rej(err)
50 // ---------------------------------------------------------------------------
53 downloadYoutubeDLVideo,
57 // ---------------------------------------------------------------------------
59 async function safeGetYoutubeDL () {
63 youtubeDL = require('youtube-dl')
66 await YoutubeDlUpdateScheduler.Instance.execute()
67 youtubeDL = require('youtube-dl')
73 function normalizeObject (obj: any) {
74 const newObj: any = {}
76 for (const key of Object.keys(obj)) {
78 if (key === 'resolution') continue
80 const value = obj[key]
82 if (typeof value === 'string') {
83 newObj[key] = value.normalize()
92 function buildVideoInfo (obj: any) {
94 name: titleTruncation(obj.title),
95 description: descriptionTruncation(obj.description),
96 category: getCategory(obj.categories),
97 licence: getLicence(obj.license),
99 tags: getTags(obj.tags),
100 thumbnailUrl: obj.thumbnail || undefined
104 function titleTruncation (title: string) {
105 return truncate(title, {
106 'length': CONSTRAINTS_FIELDS.VIDEOS.NAME.max,
112 function descriptionTruncation (description: string) {
113 if (!description || description.length < CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.min) return undefined
115 return truncate(description, {
116 'length': CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max,
122 function isNSFW (info: any) {
123 return info.age_limit && info.age_limit >= 16
126 function getTags (tags: any) {
127 if (Array.isArray(tags) === false) return []
130 .filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min)
131 .map(t => t.normalize())
135 function getLicence (licence: string) {
136 if (!licence) return undefined
138 if (licence.indexOf('Creative Commons Attribution') !== -1) return 1
143 function getCategory (categories: string[]) {
144 if (!categories) return undefined
146 const categoryString = categories[0]
147 if (!categoryString || typeof categoryString !== 'string') return undefined
149 if (categoryString === 'News & Politics') return 11
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)