From 4f1f6f038389ce9cdf0c77dfccdc63efc6948101 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 16 Aug 2018 09:45:51 +0200 Subject: Ensure youtubedl binary exists in ydl helper --- server/helpers/youtube-dl.ts | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'server/helpers') diff --git a/server/helpers/youtube-dl.ts b/server/helpers/youtube-dl.ts index 77986f407..0afc54fd2 100644 --- a/server/helpers/youtube-dl.ts +++ b/server/helpers/youtube-dl.ts @@ -1,8 +1,8 @@ -import * as youtubeDL from 'youtube-dl' import { truncate } from 'lodash' import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES } from '../initializers' import { logger } from './logger' import { generateVideoTmpPath } from './utils' +import { YoutubeDlUpdateScheduler } from '../lib/schedulers/youtube-dl-update-scheduler' export type YoutubeDLInfo = { name?: string @@ -15,9 +15,10 @@ export type YoutubeDLInfo = { } function getYoutubeDLInfo (url: string): Promise { - return new Promise((res, rej) => { + return new Promise(async (res, rej) => { const options = [ '-j', '--flat-playlist' ] + const youtubeDL = await safeGetYoutubeDL() youtubeDL.getInfo(url, options, (err, info) => { if (err) return rej(err) @@ -35,7 +36,8 @@ function downloadYoutubeDLVideo (url: string) { const options = [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best', '-o', path ] - return new Promise((res, rej) => { + return new Promise(async (res, rej) => { + const youtubeDL = await safeGetYoutubeDL() youtubeDL.exec(url, options, async (err, output) => { if (err) return rej(err) @@ -53,6 +55,20 @@ export { // --------------------------------------------------------------------------- +async function safeGetYoutubeDL () { + let youtubeDL + + try { + youtubeDL = require('youtube-dl') + } catch (e) { + // Download binary + await YoutubeDlUpdateScheduler.Instance.execute() + youtubeDL = require('youtube-dl') + } + + return youtubeDL +} + function normalizeObject (obj: any) { const newObj: any = {} -- cgit v1.2.3