custom-media: add converter to CyTube Media object
This commit is contained in:
parent
8b7cdfd4c3
commit
f4ce2fe69d
5 changed files with 131 additions and 2 deletions
|
|
@ -1,6 +1,8 @@
|
|||
import { ValidationError } from './errors';
|
||||
import { parse as parseURL } from 'url';
|
||||
import net from 'net';
|
||||
import Media from './media';
|
||||
import { hash } from './util/hash';
|
||||
|
||||
const SOURCE_QUALITIES = new Set([
|
||||
240,
|
||||
|
|
@ -23,6 +25,40 @@ const SOURCE_CONTENT_TYPES = new Set([
|
|||
'video/webm'
|
||||
]);
|
||||
|
||||
export function convert(data) {
|
||||
validate(data);
|
||||
|
||||
if (data.live) data.duration = 0;
|
||||
|
||||
const sources = {};
|
||||
|
||||
for (let source of data.sources) {
|
||||
if (!sources.hasOwnProperty(source.quality))
|
||||
sources[source.quality] = [];
|
||||
|
||||
sources[source.quality].push({
|
||||
link: source.url,
|
||||
contentType: source.contentType,
|
||||
quality: source.quality
|
||||
});
|
||||
}
|
||||
|
||||
const meta = {
|
||||
direct: sources,
|
||||
textTracks: data.textTracks,
|
||||
thumbnail: data.thumbnail, // Currently ignored by Media
|
||||
live: !!data.live // Currently ignored by Media
|
||||
};
|
||||
|
||||
const id = hash('sha256', JSON.stringify([
|
||||
data.title,
|
||||
data.duration,
|
||||
meta
|
||||
]), 'base64');
|
||||
|
||||
return new Media(id, data.title, data.duration, 'cm', meta);
|
||||
}
|
||||
|
||||
export function validate(data) {
|
||||
if (typeof data.title !== 'string')
|
||||
throw new ValidationError('title must be a string');
|
||||
|
|
|
|||
|
|
@ -38,7 +38,8 @@ Media.prototype = {
|
|||
bitrate: this.meta.bitrate,
|
||||
scuri: this.meta.scuri,
|
||||
embed: this.meta.embed,
|
||||
gdrive_subtitles: this.meta.gdrive_subtitles
|
||||
gdrive_subtitles: this.meta.gdrive_subtitles,
|
||||
textTracks: this.meta.textTracks
|
||||
}
|
||||
};
|
||||
},
|
||||
|
|
|
|||
7
src/util/hash.js
Normal file
7
src/util/hash.js
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import { createHash } from 'crypto';
|
||||
|
||||
export function hash(algo, input, digest) {
|
||||
const h = createHash(algo);
|
||||
h.update(input);
|
||||
return h.digest(digest);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue