Last active
September 9, 2018 22:32
-
-
Save cchudant/954557ed30180a5ba1d016181a184528 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ffmpeg = require('fluent-ffmpeg') | |
const fetch = require('node-fetch') | |
const FormData = require('form-data') | |
const { PassThrough } = require('stream') | |
const apiUrl = 'https://bayfiles.com/api/upload' | |
const idreg = /^(https?:\/\/[^\/]+)\/([0-9A-Za-z_-]+)/ | |
const namereg = /([^\/]+)$/ | |
async function fileInfo(url) { | |
const [, site, id] = idreg.exec(url) | |
const res = await fetch(`${site}/api/v2/file/${id}/info`) | |
const json = await res.json() | |
return json | |
} | |
async function rawUrl(fullUrl) { | |
const res = await fetch(fullUrl) | |
const text = await res.text() | |
const [, url] = /<a (?:.+ )?href="(https:\/\/cdn-\d{2}.+)">/.exec(text) | |
return url | |
} | |
async function download(url) { | |
const res = await fetch(url) | |
return res | |
} | |
async function upload(stream, name) { | |
const file = name + '.webm' | |
const form = new FormData(/*{ pauseStreams: false, maxDataSize: 100 }*/) | |
form.hasKnownLength = () => false // form-data hard fix | |
form.on('data', data => console.log(data.length, data)) | |
form.on('end', () => console.log('end')) | |
form.append('file', stream, { filename: file, filepath: file, contentType: 'video/webm' }) | |
const res = await fetch(apiUrl, { | |
method: 'POST', | |
body: form | |
}) | |
return await res.json() | |
} | |
const url = | |
'https://bayfiles.com/ibSbjfh9b7/_Cervoz_Kill_La_Kill_01_-_Bluray.720p.10bits_Jap-Fr.Aac_Fr.sub_77E69C61_mkv' | |
;(async function() { | |
const raw = await rawUrl(url) | |
console.log(raw) | |
const [, name] = namereg.exec(raw) | |
console.log('Name', name) | |
const stream = new PassThrough() | |
const dl = await download(raw) | |
const dlp = new Promise((res, rej) => | |
dl.body.once('end', res).once('error', rej) | |
) | |
// stream.on('data', data => console.log(data.length, data)) | |
// stream.on('end', data => console.log('end')) | |
const up = upload(stream, name) | |
const ffp = new Promise((res, rej) => | |
ffmpeg() | |
.input(dl.body) | |
.inputFormat('matroska') | |
// .frames(120) | |
.output(stream) | |
.outputFormat('webm') | |
.once('error', rej) | |
.once('end', res) | |
.on('stderr', console.log) | |
.run() | |
) | |
await Promise.all([ | |
dlp.then(() => console.log('download finished!')), | |
ffp.then(() => console.log('ffmpeg finished!')), | |
up.then(json => console.log('upload finished!', JSON.stringify(json))) | |
]) | |
})().catch(console.error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment