1 import * as youtubeDL from 'youtube-dl'
2 import { truncate } from 'lodash'
3 import { CONFIG, CONSTRAINTS_FIELDS, VIDEO_CATEGORIES } from '../initializers'
4 import { join } from 'path'
5 import * as crypto from 'crypto'
6 import { logger } from './logger'
8 export type YoutubeDLInfo = {
18 function getYoutubeDLInfo (url: string): Promise<YoutubeDLInfo> {
19 return new Promise<YoutubeDLInfo>((res, rej) => {
20 const options = [ '-j', '--flat-playlist' ]
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 hash = crypto.createHash('sha256').update(url).digest('hex')
34 const path = join(CONFIG.STORAGE.VIDEOS_DIR, hash + '-import.mp4')
36 logger.info('Importing video %s', url)
38 const options = [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best', '-o', path ]
40 return new Promise<string>((res, rej) => {
41 youtubeDL.exec(url, options, async (err, output) => {
42 if (err) return rej(err)
49 // ---------------------------------------------------------------------------
52 downloadYoutubeDLVideo,
56 // ---------------------------------------------------------------------------
58 function normalizeObject (obj: any) {
59 const newObj: any = {}
61 for (const key of Object.keys(obj)) {
63 if (key === 'resolution') continue
65 const value = obj[key]
67 if (typeof value === 'string') {
68 newObj[key] = value.normalize()
77 function buildVideoInfo (obj: any) {
79 name: titleTruncation(obj.title),
80 description: descriptionTruncation(obj.description),
81 category: getCategory(obj.categories),
82 licence: getLicence(obj.license),
84 tags: getTags(obj.tags),
85 thumbnailUrl: obj.thumbnail || undefined
89 function titleTruncation (title: string) {
90 return truncate(title, {
91 'length': CONSTRAINTS_FIELDS.VIDEOS.NAME.max,
97 function descriptionTruncation (description: string) {
98 if (!description || description.length < CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.min) return undefined
100 return truncate(description, {
101 'length': CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max,
107 function isNSFW (info: any) {
108 return info.age_limit && info.age_limit >= 16
111 function getTags (tags: any) {
112 if (Array.isArray(tags) === false) return []
115 .filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min)
116 .map(t => t.normalize())
120 function getLicence (licence: string) {
121 if (!licence) return undefined
123 if (licence.indexOf('Creative Commons Attribution licence') !== -1) return 1
128 function getCategory (categories: string[]) {
129 if (!categories) return undefined
131 const categoryString = categories[0]
132 if (!categoryString || typeof categoryString !== 'string') return undefined
134 if (categoryString === 'News & Politics') return 11
136 for (const key of Object.keys(VIDEO_CATEGORIES)) {
137 const category = VIDEO_CATEGORIES[key]
138 if (categoryString.toLowerCase() === category.toLowerCase()) return parseInt(key, 10)