Skip to content

Commit

Permalink
Use youtube-dl binary directly (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed May 17, 2018
1 parent d8a629f commit 2409771
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
26 changes: 14 additions & 12 deletions packages/metascraper-video-provider/index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
'use strict'

const { round, size, get, chain, find, isString } = require('lodash')
const {path: youtubeDlPath} = require('youtube-dl-installer')
const { isUrl, titleize } = require('@metascraper/helpers')
const parseDomain = require('parse-domain')
const youtubedl = require('youtube-dl')
const { promisify } = require('util')
const path = require('path')

const execFile = promisify(require('child_process').execFile)

const providers = require('./providers')

const getInfo = promisify(youtubedl.getInfo)
const getInfo = async url => {
const args = [ '--dump-json', '-f', 'best', url ]
const {stdout, stderr} = await execFile(youtubeDlPath, args)
return stderr === '' ? JSON.parse(stdout) : {}
}

const isSupportedProvided = url => providers.includes(parseDomain(url).domain)

let cachedVideoInfoUrl
let cachedVideoInfo

const isSupportedProvided = url => providers.includes(parseDomain(url).domain)

/**
* Get the video info.
* Avoid do more one request for the same URL.
*/
const getVideoInfo = async url => {
if (!isSupportedProvided(url)) return {}
if (url === cachedVideoInfoUrl) return cachedVideoInfo
try {
const info = await getInfo(url)
cachedVideoInfoUrl = url
cachedVideoInfo = info
return info
} catch (err) {
return {}
}
const info = await getInfo(url)
cachedVideoInfoUrl = url
cachedVideoInfo = info
return info
}

const isMp4 = format => format.ext === 'mp4' || path.extname(format.url).startsWith('.mp4')
Expand Down
2 changes: 1 addition & 1 deletion packages/metascraper-video-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@metascraper/helpers": "^3.10.8",
"lodash": "~4.17.10",
"parse-domain": "~2.0.0",
"youtube-dl": "~1.12.2"
"youtube-dl-installer": "~1.0.0"
},
"devDependencies": {
"mocha": "latest",
Expand Down

0 comments on commit 2409771

Please sign in to comment.