Downloading a wav file using Horseman and PhantomJS losing data quality

Viewed 723

I'm using PhantomJS and HorsemanJS to download a wav file from a remote server. However when the file is base64 encoded and written to a new file it loses quality which makes it unusable. The audio is there but its distorted which leads me to think that its an encoding problem. I'm running on Ubuntu 14.04 using node v5

Below is my script any ideas maybe to improve the base64 encoding?

var Horseman = require('node-horseman');
var horseman = new Horseman({cookiesFile:'./cookiejar'});
var fs = require('fs');

horseman.on("urlChanged", function(url){
  console.log(url);
});

horseman.on('consoleMessage', function( msg ){
  console.log(msg);
});

horseman
  .userAgent("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36")
  .open('https://remoteserver.com/audo.aspx?guid=01439900-5361-4828-ad0e-945b56e9fe51')
  .waitForNextPage()
  .type('input[name="password"]', process.env.PASS)
  .type('input[name="username"]', process.env.UN)
  .click("button:contains('Login')")
  .waitForNextPage()
  .evaluate(function(){
    var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    function base64encode(str) {
        var out, i, len;
        var c1, c2, c3;

        len = str.length;
        i = 0;
        out = "";
        while(i < len) {
        c1 = str.charCodeAt(i++) & 0xff;
        if(i == len)
        {
            out += base64EncodeChars.charAt(c1 >> 2);
            out += base64EncodeChars.charAt((c1 & 0x3) << 4);
            out += "==";
            break;
        }
        c2 = str.charCodeAt(i++);
        if(i == len)
        {
            out += base64EncodeChars.charAt(c1 >> 2);
            out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
            out += base64EncodeChars.charAt((c2 & 0xF) << 2);
            out += "=";
            break;
        }
        c3 = str.charCodeAt(i++);
        out += base64EncodeChars.charAt(c1 >> 2);
        out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
        out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
        out += base64EncodeChars.charAt(c3 & 0x3F);
        }
        return out;
    }

    var url = $("a:contains('Uncompressed file')").attr("href");

    console.log(url);

    var out;
    $.ajax({
      'async' : false,
      'url' : url,
      'success' : function(data, status, xhr) {
        console.log(status);
        console.log(xhr.getResponseHeader('Content-Type'));
        out = base64encode(data);
      }
    });
    return out;
  })
  .then(function(out){
    fs.writeFile('./mydownloadedfile.txt', out, 'base64', function(){
      return horseman.close();
    });
  });

The content-type comes back as audio/wav

If the file is manually downloaded and played via the same player it plays fine, it is only when it goes through this process.

1 Answers
Related