alashow / datmusic

Search and download free music from VK.
https://datmusic.xyz
MIT License
231 stars 58 forks source link

Possible to display Bitrate? #10

Closed Porco-Rosso closed 8 years ago

Porco-Rosso commented 8 years ago

Hi again,

I was wondering if you were considering a feature to show bitrates?

I understand that pulling the info for all the search queries would not be feasible, but seeing as you are already using php, you could perhaps calculate the bitrate of the current playing track?

I've looked high and low, and it seems impossible to accomplish in javascript.

alashow commented 8 years ago

Hi.

Basically, if you get file size in bytes from header of mp3 file (without downloading) and divide it to mp3 duration, you get bitrate (p.s you can get mp3 duration from already rendered audio list in DOM).

In download php there is already stream option to download and redirect to mp3 file (example: https://datmusic.xyz/stream/nRPk:X3E3gB).

You could do something like this, in javascript (duration is hard coded, get it from DOM). Run in console to see it in action:

function getFileSize(url, callback) {
    var request = new XMLHttpRequest();
    //get only header.
    request.open("HEAD", url, true);
    request.onreadystatechange = function() {
        if (this.readyState == this.DONE) {
            callback(parseInt(request.getResponseHeader("Content-Length")));
        }
    };
    request.send();
}

//Example: Coldplay - Adventure of a Lifetime, 04:18 duration (280 in seconds)
getFileSize("https://datmusic.xyz/stream/nRPk:X3E3gB", function(sizeInBytes){
    duration = 280; //seconds
    bitrate = sizeInBytes / duration;
    console.log("~" + bitrate + " kbps")
})

I did something like this on android client (source).

Porco-Rosso commented 8 years ago

Wow this is great! Thank you so much!

I also had the idea of fetching the filesize and calculating with the song length, however I kept running into CORS problems and the vk servers not allowing content-headers and such.

Going to put this into use as soon as I can!

Lastly I can't quite figure out if it is my perception, or is the api calls on datmusic faster then on my version: https://butane.stereostance.com ? I'm probably doing something horrendous in my javascript to the queries... Did you implement anything for speed? I see mention of cacheing, but that seems to be for the mp3s.

alashow commented 8 years ago

It might be because of proxy mode.

Porco-Rosso commented 8 years ago

Ok so this is driving me mad, the code you posted seems to work arbitrarily.

A few lucky times If I paste it into the console, it works, but most of the time I get:

XMLHttpRequest cannot load http://cs9-3v4.vk.me/p10/0355f5d7ee28e8.mp3?extra=MCfeKj6wbMkEFHthDdB9htOhc…." No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://butane.stereostance.com' is therefore not allowed access.

I can't seem to nail down what is the determining factor that makes it work occasionally. Also it has never run successfully when I insert the code it into my js file.

... and this is all without even trying to tackle the mixed content warnings when run over https. (vk servers sometimes seem to support https, sometimes not)

Porco-Rosso commented 8 years ago

screen shot 2016-05-16 at 10 53 22 am screen shot 2016-05-16 at 10 48 03 am

alashow commented 8 years ago

Sorry for late reply.

http://stackoverflow.com/questions/10636611/how-does-access-control-allow-origin-header-work

It might be because of vk servers are different: some of them allow other origins and some doesn't.

Possible solution might be adding my proxy implementation to your fork & deploy to your server & request from same server.

Porco-Rosso commented 8 years ago

Unfortunately, I'm back to thinking this is unfeasible without server-side scripting.

I also thought it might be because vk servers have different allow origin headers set, but that wouldn't explain why copying the same url from the non-working tab into the other tab works. Furthermore, if you look at the screenshots I posted above, they are both requests to your server, not vk. Yet I get different results...

I'm trying to avoid php, as I have even less knowledge than in javascript, and I am currently serving butane from rawgit.com.

Thanks for the help regardless. I'm learning a lot in the process.

alashow commented 8 years ago

Closing. Check https://datmusic.xyz for working version.