Quality added

This commit is contained in:
killer069
2021-09-24 12:49:39 +05:30
parent be840ce18b
commit f304d6bcbd
8 changed files with 92 additions and 88 deletions

View File

@@ -22,7 +22,7 @@ interface SoundCloudTrackDeprecated {
type: 'track';
}
interface SoundCloudTrackFormat {
export interface SoundCloudTrackFormat {
url: string;
preset: string;
duration: number;
@@ -90,13 +90,13 @@ export class SoundCloudTrack {
id: this.id,
type: this.type,
url: this.url,
fetched : this.fetched,
fetched: this.fetched,
durationInMs: this.durationInMs,
durationInSec: this.durationInSec,
publisher: this.publisher,
formats: this.formats,
thumbnail: this.thumbnail,
user : this.user
user: this.user
};
}
}
@@ -169,13 +169,13 @@ export class SoundCloudPlaylist {
await Promise.allSettled(work);
}
get total_tracks(){
let count = 0
get total_tracks() {
let count = 0;
this.tracks.forEach((track) => {
if(track instanceof SoundCloudTrack) count++
else return
})
return count
if (track instanceof SoundCloudTrack) count++;
else return;
});
return count;
}
toJSON() {
@@ -183,19 +183,19 @@ export class SoundCloudPlaylist {
name: this.name,
id: this.id,
type: this.type,
sub_type : this.sub_type,
sub_type: this.sub_type,
url: this.url,
durationInMs: this.durationInMs,
durationInSec: this.durationInSec,
tracksCount : this.tracksCount,
user : this.user,
tracks : this.tracks
tracksCount: this.tracksCount,
user: this.user,
tracks: this.tracks
};
}
}
export class Stream {
stream : PassThrough;
stream: PassThrough;
type: StreamType;
private url: string;
private playing_count: number;

View File

@@ -1,7 +1,7 @@
import fs from 'fs';
import { StreamType } from '../YouTube/stream';
import { request } from '../YouTube/utils/request';
import { SoundCloudPlaylist, SoundCloudTrack, Stream } from './classes';
import { SoundCloudPlaylist, SoundCloudTrack, SoundCloudTrackFormat, Stream } from './classes';
let soundData: SoundDataOptions;
if (fs.existsSync('.data/soundcloud.data')) {
@@ -33,23 +33,31 @@ export async function soundcloud(url: string): Promise<SoundCloudTrack | SoundCl
else return new SoundCloudPlaylist(json_data, soundData.client_id);
}
export async function stream(url: string): Promise<Stream> {
export async function stream(url: string, quality? : number): Promise<Stream> {
const data = await soundcloud(url);
if (data instanceof SoundCloudPlaylist) throw new Error("Streams can't be created from Playlist url");
const req_url = data.formats[data.formats.length - 1].url + '?client_id=' + soundData.client_id;
const HLSformats = parseHlsFormats(data.formats)
if(!quality) quality = HLSformats.length - 1;
else if(quality <= 0) quality = 0;
else if(quality > HLSformats.length) quality = HLSformats.length - 1;
const req_url = HLSformats[quality].url + '?client_id=' + soundData.client_id;
const s_data = JSON.parse(await request(req_url));
const type = data.formats[data.formats.length - 1].format.mime_type.startsWith('audio/ogg')
const type = HLSformats[quality].format.mime_type.startsWith('audio/ogg')
? StreamType.OggOpus
: StreamType.Arbitrary;
return new Stream(s_data.url, type);
}
export async function stream_from_info(data: SoundCloudTrack): Promise<Stream> {
const req_url = data.formats[data.formats.length - 1].url + '?client_id=' + soundData.client_id;
export async function stream_from_info(data: SoundCloudTrack, quality? :number): Promise<Stream> {
const HLSformats = parseHlsFormats(data.formats)
if(!quality) quality = HLSformats.length - 1;
else if(quality <= 0) quality = 0;
else if(quality > HLSformats.length) quality = HLSformats.length - 1;
const req_url = HLSformats[quality].url + '?client_id=' + soundData.client_id;
const s_data = JSON.parse(await request(req_url));
const type = data.formats[data.formats.length - 1].format.mime_type.startsWith('audio/ogg')
const type = HLSformats[quality].format.mime_type.startsWith('audio/ogg')
? StreamType.OggOpus
: StreamType.Arbitrary;
return new Stream(s_data.url, type);
@@ -77,3 +85,11 @@ export async function so_validate(url: string): Promise<false | 'track' | 'playl
else if (json_data.kind === 'playlist') return 'playlist';
else return false;
}
function parseHlsFormats(data : SoundCloudTrackFormat[]){
const result: SoundCloudTrackFormat[] = [];
data.forEach((format) => {
if(format.format.protocol === 'hls') result.push(format)
});
return result;
}