Friday, April 1, 2016

Downloading a wav file using Horseman and PhantomJS losing data quality

Leave a Comment

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

Any suggestions welcome!

1 Answers

Answers 1

Why don't you use Buffer for base64 encoding and decoding:

function base64Encode(plainData) {     return new Buffer(plainData).toString('base64'); }  function base64Decode(encodedData) {     return new Buffer(encodedData, 'base64').toString(); } 

in your script:

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; })... 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment