Description
Hello, as we all know, when requesting 'audio only' from node-ytdl, it will gives us a URL which is a chunk of 5 seconds audio playable directly from the browser. so the problem is we have to make a new request every < 5 seconds to get new audio. I tried to implement a mechanism where using Axios in a loop with a circular buffer, when getting a response, data will be pushed to the buffer and then write it to a WriteStream (a local file). The result was a bit unreliable, i was able to play it on VLC not on other players because data headers was also pushed with the chunks. when playing, i can hear duplicate samples ex. the first 15s are the same as the original first 5s request but duplicated, which made me think that YouTube will gives you the same audio if the request was not buffered or played completely or the request time was a bit early, like if we make 2 requests under 2 seconds we will get the same response. Anyway let's see what we can accomplish with that and with the help of anyone who is interested playing audio from a live stream because we all want to save some bandwidth and want to listen to music without watching while working. Here is some of my implementations:
const duration = 120;
const bufferSize = sampleRate * duration;
const buf = new CircularBuffer(bufferSize);`
async function callAxios(url) {
try {
let response = await axios( { method: 'get', url: url, responseType:'arraybuffer' });
return response.data
}catch (err) {
console.log(err)
}
}
async function loopThrough(url) {
let streaming = true;
// Loop through listings until they don't exist
do {
const data = await callAXios(url);
if (data) {
buf.push(data);
console.log(buf.size());
// streaming = false;
writeStream.write(buf.deq(), function () {
})
}else{
streaming = false;
writeStream.end();
}
} while (streaming)
}
mimeType: 'audio/mp4; codecs="mp4a.40.2"',
qualityLabel: null,
bitrate: 144000,
audioBitrate: 128,
itag: 140,
url: 'https://r2---sn-vbxgv-cxtz.googlevideo.com/videoplayback?expire=1621879552&ei=oJarYL2YI_K1xN8P9aCe-A8&ip=MYIP&id=KvRVky0r7YM.3&itag=140&source=yt_live_broadcast&requiressl=yes&mh=mz&mm=44%2C29&mn=sn-vbxgv-cxtz%2Csn-25glene6&ms=lva%2Crdu&mv=m&mvi=2&pl=24&initcwndbps=222500&vprv=1&live=1&hang=1&noclen=1&mime=audio%2Fmp4&ns=Yl-cSuGcwe5MjklshWtfjmsF&gir=yes&mt=1621857627&fvip=4&keepalive=yes&fexp=24001373%2C24007246&beids=9466587&c=WEB&n=3-RD52cbC59tTArA&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Clive%2Chang%2Cnoclen%2Cmime%2Cns%2Cgir&sig=AOq0QJ8wRQIgbPiNUb8oGioz2IucWT_lVzPGUucvx49z-CgsReJUCXYCIQDCS3pueUwS2i8LMVeWhzQ-SWPlQO0u6odkPgSOAz-4pA%3D%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRQIhAOhfxTRYA7MruZNdjdvfyjgnO-sm2VkQ69aOQkilx7zbAiBZG4LESU2pZScRCExSTXIXNZ8BcZs2bzElAvl835lwIw%3D%3D&ratebypass=yes',
lastModified: '1621825709929201',
quality: 'tiny',
projectionType: 'RECTANGULAR',
targetDurationSec: 5,
maxDvrDurationSec: 43200,
highReplication: true,
audioQuality: 'AUDIO_QUALITY_MEDIUM',
audioSampleRate: '48000',
audioChannels: 2,
hasVideo: false,
hasAudio: true,
container: 'mp4',
codecs: 'mp4a.40.2',
videoCodec: null,
audioCodec: 'mp4a.40.2',
isLive: true,
isHLS: false,
isDashMPD: false