Move YT related functions from player to youtube script

This commit is contained in:
Rafostar
2021-04-11 15:35:41 +02:00
parent 7cf86e92eb
commit 85804ea297
2 changed files with 54 additions and 46 deletions

View File

@@ -1,8 +1,6 @@
const { Gdk, Gio, GObject, Gst, GstClapper, Gtk } = imports.gi;
const ByteArray = imports.byteArray;
const Dash = imports.src.dash;
const Debug = imports.src.debug;
const FileOps = imports.src.fileOps;
const Misc = imports.src.misc;
const YouTube = imports.src.youtube;
const { PlayerBase } = imports.src.playerBase;
@@ -53,8 +51,14 @@ class ClapperPlayer extends PlayerBase
if(!isYouTubeUri)
return super.set_uri(uri);
this.getYouTubeUriAsync(videoId)
.then(ytUri => super.set_uri(ytUri))
if(!this.ytClient)
this.ytClient = new YouTube.YouTubeClient();
this.ytClient.getPlaybackDataAsync(videoId)
.then(data => {
this.customVideoTitle = data.title;
super.set_uri(data.uri);
})
.catch(debug);
return;
@@ -76,48 +80,6 @@ class ClapperPlayer extends PlayerBase
super.set_uri(uri);
}
async getYouTubeUriAsync(videoId)
{
if(!this.ytClient)
this.ytClient = new YouTube.YouTubeClient();
const info = await this.ytClient.getVideoInfoPromise(videoId).catch(debug);
if(!info)
throw new Error('no YouTube video info');
let videoUri = null;
const dashInfo = await this.ytClient.getDashInfoAsync(info).catch(debug);
if(dashInfo) {
debug('parsed video info to dash info');
const dash = Dash.generateDash(dashInfo);
if(dash) {
debug('got dash');
const dashFile = await FileOps.saveFilePromise(
'tmp', null, 'clapper.mpd', dash
).catch(debug);
if(dashFile)
videoUri = dashFile.get_uri();
}
}
if(!videoUri)
videoUri = this.ytClient.getBestCombinedUri(info);
if(!videoUri)
throw new Error('no YouTube video URI');
this.customVideoTitle = (info.videoDetails && info.videoDetails.title)
? Misc.decodeURIPlus(info.videoDetails.title)
: videoId;
return videoUri;
}
load_playlist_file(file)
{
const stream = new Gio.DataInputStream({

View File

@@ -1,4 +1,5 @@
const { GObject, Gst, Soup } = imports.gi;
const Dash = imports.src.dash;
const Debug = imports.src.debug;
const FileOps = imports.src.fileOps;
const Misc = imports.src.misc;
@@ -302,6 +303,51 @@ var YouTubeClient = GObject.registerClass({
});
}
async getPlaybackDataAsync(videoId)
{
const info = await this.getVideoInfoPromise(videoId).catch(debug);
if(!info)
throw new Error('no YouTube video info');
let uri = null;
const dashInfo = await this.getDashInfoAsync(info).catch(debug);
if(dashInfo) {
debug('parsed video info to dash info');
const dash = Dash.generateDash(dashInfo);
if(dash) {
debug('got dash data');
const dashFile = await FileOps.saveFilePromise(
'tmp', null, 'clapper.mpd', dash
).catch(debug);
if(dashFile)
uri = dashFile.get_uri();
debug('got dash file');
}
}
if(!uri)
uri = this.ytClient.getBestCombinedUri(info);
if(!uri)
throw new Error('no YouTube video URI');
debug(`final URI: ${uri}`);
const title = (info.videoDetails && info.videoDetails.title)
? Misc.decodeURIPlus(info.videoDetails.title)
: videoId;
debug(`title: ${title}`);
return { uri, title };
}
async getDashInfoAsync(info)
{
if(