diff options
author | Chocobozzz <me@florianbigard.com> | 2018-08-16 09:45:51 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-08-16 09:45:51 +0200 |
commit | 4f1f6f038389ce9cdf0c77dfccdc63efc6948101 (patch) | |
tree | 8b00e3fedc2590613c1ad66a92dc7b394b03590a /server/helpers/youtube-dl.ts | |
parent | 8569a870e4cd662026dbf3f439c0bb460262b2b1 (diff) | |
download | PeerTube-4f1f6f038389ce9cdf0c77dfccdc63efc6948101.tar.gz PeerTube-4f1f6f038389ce9cdf0c77dfccdc63efc6948101.tar.zst PeerTube-4f1f6f038389ce9cdf0c77dfccdc63efc6948101.zip |
Ensure youtubedl binary exists in ydl helper
Diffstat (limited to 'server/helpers/youtube-dl.ts')
-rw-r--r-- | server/helpers/youtube-dl.ts | 22 |
1 files changed, 19 insertions, 3 deletions
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 @@ | |||
1 | import * as youtubeDL from 'youtube-dl' | ||
2 | import { truncate } from 'lodash' | 1 | import { truncate } from 'lodash' |
3 | import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES } from '../initializers' | 2 | import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES } from '../initializers' |
4 | import { logger } from './logger' | 3 | import { logger } from './logger' |
5 | import { generateVideoTmpPath } from './utils' | 4 | import { generateVideoTmpPath } from './utils' |
5 | import { YoutubeDlUpdateScheduler } from '../lib/schedulers/youtube-dl-update-scheduler' | ||
6 | 6 | ||
7 | export type YoutubeDLInfo = { | 7 | export type YoutubeDLInfo = { |
8 | name?: string | 8 | name?: string |
@@ -15,9 +15,10 @@ export type YoutubeDLInfo = { | |||
15 | } | 15 | } |
16 | 16 | ||
17 | function getYoutubeDLInfo (url: string): Promise<YoutubeDLInfo> { | 17 | function getYoutubeDLInfo (url: string): Promise<YoutubeDLInfo> { |
18 | return new Promise<YoutubeDLInfo>((res, rej) => { | 18 | return new Promise<YoutubeDLInfo>(async (res, rej) => { |
19 | const options = [ '-j', '--flat-playlist' ] | 19 | const options = [ '-j', '--flat-playlist' ] |
20 | 20 | ||
21 | const youtubeDL = await safeGetYoutubeDL() | ||
21 | youtubeDL.getInfo(url, options, (err, info) => { | 22 | youtubeDL.getInfo(url, options, (err, info) => { |
22 | if (err) return rej(err) | 23 | if (err) return rej(err) |
23 | 24 | ||
@@ -35,7 +36,8 @@ function downloadYoutubeDLVideo (url: string) { | |||
35 | 36 | ||
36 | const options = [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best', '-o', path ] | 37 | const options = [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best', '-o', path ] |
37 | 38 | ||
38 | return new Promise<string>((res, rej) => { | 39 | return new Promise<string>(async (res, rej) => { |
40 | const youtubeDL = await safeGetYoutubeDL() | ||
39 | youtubeDL.exec(url, options, async (err, output) => { | 41 | youtubeDL.exec(url, options, async (err, output) => { |
40 | if (err) return rej(err) | 42 | if (err) return rej(err) |
41 | 43 | ||
@@ -53,6 +55,20 @@ export { | |||
53 | 55 | ||
54 | // --------------------------------------------------------------------------- | 56 | // --------------------------------------------------------------------------- |
55 | 57 | ||
58 | async function safeGetYoutubeDL () { | ||
59 | let youtubeDL | ||
60 | |||
61 | try { | ||
62 | youtubeDL = require('youtube-dl') | ||
63 | } catch (e) { | ||
64 | // Download binary | ||
65 | await YoutubeDlUpdateScheduler.Instance.execute() | ||
66 | youtubeDL = require('youtube-dl') | ||
67 | } | ||
68 | |||
69 | return youtubeDL | ||
70 | } | ||
71 | |||
56 | function normalizeObject (obj: any) { | 72 | function normalizeObject (obj: any) { |
57 | const newObj: any = {} | 73 | const newObj: any = {} |
58 | 74 | ||