fix soundcloud hls ripping

This commit is contained in:
aria
2024-07-07 21:56:01 -04:00
parent fbc2cd3fa8
commit 0b111f3b0f
3 changed files with 887 additions and 741 deletions

1499
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -156,8 +156,10 @@ export default class Soundcloud implements Streamer {
return { return {
type: 'track', type: 'track',
getStream: async () => { getStream: async (hq?: boolean) => {
if (!hq) hq = false
return await getStream( return await getStream(
hq,
api.media.transcodings, api.media.transcodings,
api.track_authorization, api.track_authorization,
client, client,
@@ -253,53 +255,43 @@ async function fetchKey(response: string) {
} }
async function getStream( async function getStream(
hq: boolean,
transcodings: Array<SoundcloudTranscoding>, transcodings: Array<SoundcloudTranscoding>,
trackAuth: string, trackAuth: string,
client: ScClient, client: ScClient,
oauthToken?: string | undefined oauthToken?: string | undefined
): Promise<GetStreamResponse> { ): Promise<GetStreamResponse> {
const progressive = transcodings.filter((x) => x.format?.protocol == 'progressive')[0] let filter = transcodings.filter((x) => x.quality == 'hq')
if (hq == true && filter.length == 0) throw new Error('Could not find HQ format.')
if (progressive?.url) { if (filter.length == 0) filter = transcodings.filter((x) => x.preset.startsWith('aac_')) // prioritize aac (go+)
const streamUrlResp = await fetch( if (filter.length == 0) filter = transcodings.filter((x) => x.preset.startsWith('opus_')) // then opus
`${progressive.url}?client_id=${client.id}&track_authorization=${trackAuth}`, if (filter.length == 0) filter = transcodings.filter((x) => x.preset.startsWith('mp3_')) // then mp3
{ if (filter.length == 0) throw new Error('Could not find applicable format.') // and this is just in case none of those exist
headers: headers(oauthToken)
} const transcoding = filter[0]
) const streamUrlResp = await fetch(
const json = <{ url: string }>await streamUrlResp.json() `${transcoding.url}?client_id=${client.id}&track_authorization=${trackAuth}`,
{
headers: headers(oauthToken)
}
)
const json = <{ url: string }>await streamUrlResp.json()
if (!json.url) throw new Error('Stream URL could not be retreieved.')
if (transcoding.format.protocol == 'progressive') {
const streamResp = await fetch(json.url) const streamResp = await fetch(json.url)
return { return {
mimeType: progressive.format.mime_type, mimeType: transcoding.format.mime_type,
sizeBytes: parseInt(<string>streamResp.headers.get('Content-Length')), sizeBytes: parseInt(<string>streamResp.headers.get('Content-Length')),
stream: <NodeJS.ReadableStream>streamResp.body stream: <NodeJS.ReadableStream>streamResp.body
} }
} else { } else {
const filteredTranscodings = transcodings.filter((x) => x?.preset.startsWith('opus_')) const container = transcoding.format.mime_type.split('/')[1].split(';')[0].split('+')[0]
let url
let selected
if (filteredTranscodings[filteredTranscodings.length - 1]?.url) {
url = filteredTranscodings[filteredTranscodings.length - 1]?.url
selected = filteredTranscodings[filteredTranscodings.length - 1]
} else {
url = transcodings[0]?.url
selected = transcodings[0]
}
const streamUrlResp = await fetch(
`${url}?client_id=${client.id}&track_authorization=${trackAuth}`,
{
headers: headers(oauthToken)
}
)
const json = <{ url: string }>await streamUrlResp.json()
const codec = selected.format.mime_type.split('/')[1].split(';')[0].split(' ')[0]
return { return {
mimeType: selected.format.mime_type, mimeType: transcoding.format.mime_type,
stream: await parseHls(json.url, codec) stream: (await parseHls(json.url, container))
} }
} }
} }

View File

@@ -2,7 +2,9 @@ import { Artist, Album, Track } from '../../types.js'
import { spawn } from 'child_process' import { spawn } from 'child_process'
import fetch from 'node-fetch' import fetch from 'node-fetch'
import { imageSize as sizeOf } from 'image-size' import { imageSize as sizeOf } from 'image-size'
import Stream from 'stream' import os from 'node:os'
import fs from 'node:fs'
import path from 'node:path'
async function parseCoverArtwork(url: string) { async function parseCoverArtwork(url: string) {
const resp = await fetch(url) const resp = await fetch(url)
@@ -101,49 +103,34 @@ export async function parseTrack(raw: RawTrack): Promise<Track> {
return track return track
} }
export async function parseHls(url: string, codec: string): Promise<NodeJS.ReadableStream> { export async function parseHls(url: string, container: string): Promise<NodeJS.ReadableStream> {
const m3u8 = await (await fetch(url)).text() return new Promise(async function(resolve, reject) {
const folder = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'lucida'))
const urls = <URL[]>[] const ffmpegProc = spawn('ffmpeg', [
for (const i in m3u8.split('\n')) { '-hide_banner',
if (m3u8.split('\n')[i].startsWith('#')) continue '-loglevel',
else urls.push(new URL(m3u8.split('\n')[i])) 'error',
} '-i',
url,
'-c:a',
'copy',
'-f',
container,
`${folder}/hls.${container}`
])
const stream = new Stream.Readable({ let err: string
// eslint-disable-next-line @typescript-eslint/no-empty-function
read() {} ffmpegProc.stderr.on('data', function(data) {
err = data.toString()
})
ffmpegProc.once('exit', function(code) {
if (code == 0) resolve(fs.createReadStream(`${folder}/hls.${container}`))
else reject((`FFMPEG HLS error: ${err}` || 'FFMPEG could not parse the HLS.'))
})
}) })
async function load() {
for (const url of urls) {
const resp = await fetch(url)
if (!resp.body) throw new Error('Response has no body')
for await (const chunk of resp.body) {
stream.push(chunk)
}
}
stream.push(null)
}
const ffmpegProc = spawn('ffmpeg', [
'-hide_banner',
'-loglevel',
'error',
'-i',
'-',
'-c:a',
'copy',
'-f',
codec,
'-'
])
stream.pipe(ffmpegProc.stdin)
ffmpegProc.stderr.pipe(process.stderr)
load()
return stream
} }
export interface RawSearchResults { export interface RawSearchResults {