Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add track metada informations #44

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/connection/voiceHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ class VoiceConnection {

if (streamInfo.exception) return streamInfo

return { stream: voiceUtils.createAudioResource(streamInfo.stream, urlInfo.format) }
return {
stream: voiceUtils.createAudioResource(streamInfo.stream, urlInfo.format),
bitrate: streamInfo.bitrate
}
}

async play(track, decodedTrack, noReplace) {
Expand Down Expand Up @@ -284,7 +287,13 @@ class VoiceConnection {

this.cache.url = urlInfo.url
this.cache.protocol = urlInfo.protocol
this.config.track = { encoded: track, info: decodedTrack }
this.config.track = {
encoded: track,
info: decodedTrack,
metadata: {
bitrate: resource.bitrate
}
}
this.config.paused = false

if (this.config.volume !== 100)
Expand Down
21 changes: 9 additions & 12 deletions src/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,26 +119,18 @@ function getTrackStream(decodedTrack, url, protocol, additionalData) {
let trueSource = [ 'pandora', 'spotify' ].includes(decodedTrack.sourceName) ? config.search.defaultSearchSource : decodedTrack.sourceName

if (trueSource === 'youtube' && protocol === 'hls') {
return resolve({
stream: await youtube.loadStream(url)
})
return resolve(await youtube.loadStream(url))
}

if (trueSource === 'deezer') {
return resolve({
stream: await deezer.loadTrack(decodedTrack.title, url, additionalData)
})
return resolve(await deezer.loadTrack(decodedTrack.title, url, additionalData))
}

if (trueSource === 'soundcloud') {
if (additionalData === true) {
trueSource = config.search.fallbackSearchSource
} else if (protocol === 'hls') {
const stream = await soundcloud.loadHLSStream(url)

return resolve({
stream
})
return resolve(await soundcloud.loadHLSStream(url))
}
}

Expand Down Expand Up @@ -182,8 +174,13 @@ function getTrackStream(decodedTrack, url, protocol, additionalData) {
})
})

console.log(res.headers['content-length'], decodedTrack.duration)

resolve({
stream
stream,
bitrate: decodedTrack.sourceName !== 'http' ? Math.round((res.headers['content-length'] * 8) / decodedTrack.duration / 1000) : null,
protocol,
size: res.headers['content-length']
})
}
} catch (err) {
Expand Down
7 changes: 6 additions & 1 deletion src/sources/deezer.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ function loadTrack(title, url, trackInfos) {
streamOnly: true
})

const bitrate = Math.round((res.headers['content-length'] * 8) / trackInfos.DURATION)

res.stream.on('end', () => stream.end())
res.stream.on('error', (error) => {
debugLog('retrieveStream', 4, { type: 2, sourceName: 'Deezer', query: title, message: error.message })
Expand Down Expand Up @@ -413,7 +415,10 @@ function loadTrack(title, url, trackInfos) {
}
}

resolve(stream)
resolve({
stream,
bitrate
})
})
})
}
Expand Down
4 changes: 1 addition & 3 deletions src/sources/http.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { debugLog, makeRequest, encodeTrack } from '../utils.js'

async function loadFrom(uri) {
const type = uri.startsWith('http://') ? 'http' : 'https'
debugLog('loadtracks', 4, { type: 1, loadType: 'track', sourceName: type, query: uri })

const data = await makeRequest(uri, { method: 'HEAD' })
Expand All @@ -19,7 +18,6 @@ async function loadFrom(uri) {
}
}


if (!data.headers || !data.headers['content-type']?.startsWith('audio/')) {
debugLog('loadtracks', 4, { type: 2, loadType: 'error', sourceName: type, query: uri, message: 'Url is not a playable stream.' })

Expand All @@ -44,7 +42,7 @@ async function loadFrom(uri) {
uri,
artworkUrl: null,
isrc: null,
sourceName: type
sourceName: 'http'
}

debugLog('loadtracks', 4, { type: 2, loadType: 'track', sourceName: type, track, query: uri })
Expand Down
12 changes: 9 additions & 3 deletions src/sources/soundcloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ async function retrieveStream(identifier, title) {
url: urlInfo.url,
protocol: urlInfo.protocol,
format: urlInfo.format,
additionalData: true
additionalData: true,
bitrate: urlInfo.bitrate
}
}
}
Expand All @@ -341,9 +342,14 @@ async function loadHLSStream(url) {
const streamHlsRedirect = await http1makeRequest(url, { method: 'GET' })

const stream = new PassThrough()
await loadHLS(streamHlsRedirect.body.url, stream)
const metadata = await loadHLS(streamHlsRedirect.body.url, stream)

return stream
return {
stream,
...metadata,
protocol: 'hls',
size: -1
}
}

async function loadFilters(url, protocol) {
Expand Down
42 changes: 29 additions & 13 deletions src/sources/youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ const ytContext = {
} : {}),
client: {
...(!config.search.sources.youtube.bypassAgeRestriction ? {
userAgent: 'com.google.android.youtube/19.13.34 (Linux; U; Android 14 gzip)',
userAgent: 'com.google.android.youtube/19.13.35 (Linux; U; Android 14 gzip)',
clientName: 'ANDROID',
clientVersion: '19.13.34',
clientVersion: '19.13.35',
deviceMake: 'Google',
deviceModel: 'Pixel 8',
onName: 'Android',
osVersion: '14',
gl: 'BR',
hl: 'pt',
timezone: 'America/Sao_Paulo'
} : {
clientName: 'TVHTML5_SIMPLY_EMBEDDED_PLAYER',
clientVersion: '2.0',
Expand Down Expand Up @@ -47,12 +54,12 @@ function _getBaseHost(type) {
function _switchClient(newClient) {
if (newClient === 'ANDROID') {
ytContext.client.clientName = 'ANDROID'
ytContext.client.clientVersion = '19.04.33'
ytContext.client.userAgent = 'com.google.android.youtube/19.04.33 (Linux; U; Android 14 gzip)'
ytContext.client.clientVersion = '19.13.35'
ytContext.client.userAgent = 'com.google.android.youtube/19.13.35 (Linux; U; Android 14 gzip)'
} else if (newClient === 'ANDROID_MUSIC') {
ytContext.client.clientName = 'ANDROID_MUSIC'
ytContext.client.clientVersion = '6.37.50'
ytContext.client.userAgent = 'com.google.android.apps.youtube.music/6.37.50 (Linux; U; Android 14 gzip)'
ytContext.client.clientVersion = '6.44.54'
ytContext.client.userAgent = 'com.google.android.apps.youtube.music/6.44.54 (Linux; U; Android 14 gzip)'
}
}

Expand Down Expand Up @@ -144,7 +151,8 @@ async function search(query, type, shouldLog) {
...(config.search.sources.youtube.authentication.enabled ? {
Authorization: config.search.sources.youtube.authentication.authorization,
Cookie: `SID=${config.search.sources.youtube.authentication.cookies.SID}; LOGIN_INFO=${config.search.sources.youtube.authentication.cookies.LOGIN_INFO}`
} : {})
} : {}),
'X-GOOG-API-FORMAT-VERSION': 2
},
body: {
context: ytContext,
Expand Down Expand Up @@ -263,7 +271,8 @@ async function loadFrom(query, type) {
...(config.search.sources.youtube.authentication.enabled ? {
Authorization: config.search.sources.youtube.authentication.authorization,
Cookie: `SID=${config.search.sources.youtube.authentication.cookies.SID}; LOGIN_INFO=${config.search.sources.youtube.authentication.cookies.LOGIN_INFO}`
} : {})
} : {}),
'X-GOOG-API-FORMAT-VERSION': 2
},
body: {
context: ytContext,
Expand Down Expand Up @@ -327,7 +336,8 @@ async function loadFrom(query, type) {
...(config.search.sources.youtube.authentication.enabled ? {
Authorization: config.search.sources.youtube.authentication.authorization,
Cookie: `SID=${config.search.sources.youtube.authentication.cookies.SID}; LOGIN_INFO=${config.search.sources.youtube.authentication.cookies.LOGIN_INFO}`
} : {})
} : {}),
'X-GOOG-API-FORMAT-VERSION': 2
},
body: {
context: ytContext,
Expand Down Expand Up @@ -439,7 +449,8 @@ async function loadFrom(query, type) {
...(config.search.sources.youtube.authentication.enabled ? {
Authorization: config.search.sources.youtube.authentication.authorization,
Cookie: `SID=${config.search.sources.youtube.authentication.cookies.SID}; LOGIN_INFO=${config.search.sources.youtube.authentication.cookies.LOGIN_INFO}`
} : {})
} : {}),
'X-GOOG-API-FORMAT-VERSION': 2
},
body: {
context: ytContext,
Expand Down Expand Up @@ -512,7 +523,8 @@ async function retrieveStream(identifier, type, title) {
...(config.search.sources.youtube.authentication.enabled ? {
Authorization: config.search.sources.youtube.authentication.authorization,
Cookie: `SID=${config.search.sources.youtube.authentication.cookies.SID}; LOGIN_INFO=${config.search.sources.youtube.authentication.cookies.LOGIN_INFO}`
} : {})
} : {}),
'X-GOOG-API-FORMAT-VERSION': 2
},
body: {
context: ytContext,
Expand Down Expand Up @@ -587,7 +599,8 @@ function loadLyrics(decodedTrack, language) {
...(config.search.sources.youtube.authentication.enabled ? {
Authorization: config.search.sources.youtube.authentication.authorization,
Cookie: `SID=${config.search.sources.youtube.authentication.cookies.SID}; LOGIN_INFO=${config.search.sources.youtube.authentication.cookies.LOGIN_INFO}`
} : {})
} : {}),
'X-GOOG-API-FORMAT-VERSION': 2
},
body: {
context: ytContext,
Expand Down Expand Up @@ -710,7 +723,10 @@ async function loadStream(url) {
const stream = new PassThrough()
await loadHLSPlaylist(url, stream)

resolve(stream)
resolve({
stream,
bitrate: -1
})
})
}

Expand Down
17 changes: 14 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,10 @@ export function loadHLS(url, stream, onceEnded) {
return new Promise(async (resolve) => {
const response = await http1makeRequest(url, { method: 'GET' })
const body = response.body.split('\n')
const metadata = {
bitrate: null
}
let nextLength = 0

body.nForEach(async (line, i) => {
return new Promise(async (resolveSegment) => {
Expand All @@ -884,6 +888,9 @@ export function loadHLS(url, stream, onceEnded) {
if (line.startsWith('#')) {
const tag = line.split(':')[0]

if (tag === '#EXTINF')
nextLength = parseFloat(line.split(':')[1].split(',')[0])

if (tag === '#EXT-X-ENDLIST') {
stream.end()

Expand All @@ -895,11 +902,17 @@ export function loadHLS(url, stream, onceEnded) {

const segment = await http1makeRequest(line, { method: 'GET', streamOnly: true })

if (metadata.bitrate === null && segment.headers['content-length'] && nextLength)
metadata.bitrate = Math.round((parseInt(segment.headers['content-length'] * 8) / nextLength))

if (!onceEnded)
resolve(metadata)

segment.stream.on('data', (chunk) => stream.write(chunk))

segment.stream.on('end', () => {
if (onceEnded && i === body.length - 2) {
resolve(true)
resolve(metadata)

segment.stream.destroy()
} else {
Expand All @@ -910,8 +923,6 @@ export function loadHLS(url, stream, onceEnded) {
})
})
})

if (!onceEnded) resolve(true)
})
}

Expand Down