icytv-liq/liq/scripts/smart

78 lines
1.6 KiB
Plaintext
Raw Normal View History

2018-12-18 17:38:18 +00:00
#!/usr/bin/env node
'use strict'
const url = require('url')
const spawn = require('child_process').spawn
const os = require('os')
let arg = (process.argv[2] || '').trim()
function protocol (arg, handleCb) {
let yt = spawn('youtube-dl', ['--no-playlist', '--playlist-end', 1, '-j', '-f', 'best', arg])
let output = ''
yt.stdout.on('data', function (chunk) {
output += chunk.toString('utf8')
})
yt.on('close', function () {
let data = JSON.parse(output)
delete data.formats
fetchVideo(data, handleCb)
})
}
function fetchVideo (data, cb) {
let tempName = os.tmpdir() + '/tmp.yt.' + data.id + '.mkv'
let ffmpeg = spawn('ffmpeg', ['-hide_banner', '-i', data.url, '-codec:a', 'copy', '-codec:v', 'copy', '-y', tempName])
ffmpeg.stdout.pipe(process.stderr)
ffmpeg.stderr.pipe(process.stderr)
data.filename = data.url
data.filename = tempName
outputVideo(data, cb)
}
function formatter (o) {
if (Array.isArray(o)) {
o.forEach(formatter)
return
}
let list = []
for (let key in o) {
if (o.hasOwnProperty(key) && key !== 'source' && o[key] !== null && o[key] !== undefined) {
list.push(key + '=' + JSON.stringify(o[key]))
}
}
let out = ''
if (list.length > 0) {
out += 'annotate:' + list.join(',') + ':'
}
out += o.source
console.log(out)
}
function outputVideo (video, cb) {
cb({
title: video.title,
artist: video.uploader,
url: video.webpage_url,
art: video.thumbnail,
temporary: true,
source: video.filename
})
}
2019-01-11 05:42:16 +00:00
if (arg.indexOf('/') !== 0) {
2018-12-18 17:38:18 +00:00
protocol(arg, formatter)
} else {
console.log(arg)
}