Skip to content

Commit

Permalink
Change implementation
Browse files Browse the repository at this point in the history
using youtube-dl
  • Loading branch information
Kikobeats committed Apr 8, 2018
1 parent bd7dc2d commit b14e3a0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
51 changes: 26 additions & 25 deletions packages/metascraper-video-provider/index.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
'use strict'

const getSocialVideoUrl = require('get-social-video-url')
const { isUrl } = require('@metascraper/helpers')
const createBrowserless = require('browserless')
const youtubedl = require('youtube-dl')
const { promisify } = require('util')
const path = require('path')

const PREFERRED_VIDEO_QUALITY = [
'sd',
'hd',
'mobile'
]
const getVideoInfo = promisify(youtubedl.getInfo)

const getVideoUrl = async ({
browserless,
url,
preferredVideoQuality = PREFERRED_VIDEO_QUALITY
}) => {
const videoQualities = await getSocialVideoUrl({ url, browserless })
const videoQuality = preferredVideoQuality.find(videoQuality => isUrl(videoQualities[videoQuality]))
return videoQualities[videoQuality]
}
/**
* Get a Video source quality not too high
*/
const getVideoUrl = ({formats}) => {
const urls = formats
.map(item => item.url)
.filter(url => path.extname(url).startsWith('.mp4'))

module.exports = (opts = {}) => {
const { launchOpts = {}, preferredVideoQuality } = opts
const index = Math.round(urls.length / 2) - 1
return urls[index]
}

const fn = async ({url}) => {
const browserless = await createBrowserless(launchOpts)
const result = await getVideoUrl({url, browserless, preferredVideoQuality})
const browserInstance = await browserless.browser
await browserInstance.close()
return result
const getVideoProvider = async url => {
try {
const info = await getVideoInfo(url)
const videoUrl = getVideoUrl(info)
return isUrl(videoUrl) && videoUrl
} catch (err) {
return false
}
}

return { video: fn }
module.exports = () => {
return {
video: ({url}) => getVideoProvider(url)
}
}

module.exports.getVideoUrl = getVideoUrl
4 changes: 1 addition & 3 deletions packages/metascraper-video-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
},
"dependencies": {
"@metascraper/helpers": "^3.9.3",
"browserless": "~3.5.0",
"get-social-video-url": "~1.0.1",
"puppeteer": "~1.2.0"
"youtube-dl": "~1.12.2"
},
"devDependencies": {
"lodash": "latest",
Expand Down

0 comments on commit b14e3a0

Please sign in to comment.