85 lines
1.7 KiB
JavaScript
Executable File
85 lines
1.7 KiB
JavaScript
Executable File
#!/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 () {
|
|
try {
|
|
let data = JSON.parse(output)
|
|
} catch (e) {
|
|
console.error(e.message)
|
|
process.exit(1)
|
|
}
|
|
|
|
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
|
|
})
|
|
}
|
|
|
|
if (arg.indexOf('/') !== 0) {
|
|
protocol(arg, formatter)
|
|
} else {
|
|
console.log(arg)
|
|
}
|