aadsm / jsmediatags

Media Tags Reader (ID3, MP4, FLAC)
Other
745 stars 128 forks source link

issue with an external file #114

Open frequeradio opened 5 years ago

frequeradio commented 5 years ago

Hello !

I have an issue while trying to read an external file

Here is my code :

var jsmediatags = require("jsmediatags");

new jsmediatags.Reader("http://20043.live.streamtheworld.com/LOS40_SC") .setTagsToRead(["title", "artist"]) .read({ onSuccess: function(tag) { console.log(tag); }, onError: function(error) { console.log(':(', error.type, error.info); } });

And i have this error

`F:\Node\titre>node first.js F:\Node\titre\node_modules\jsmediatags\build2\XhrFileReader.js:126 throw new Error("FIXME: Unknown Content-Range syntax: " + contentRange); ^

Error: FIXME: Unknown Content-Range syntax: 0-1023/2100000000 at XhrFileReader._parseContentRange (F:\Node\titre\node_modules\jsmediatags\build2\XhrFileReader.js:126:17) at Object.onSuccess (F:\Node\titre\node_modules\jsmediatags\build2\XhrFileReader.js:66:35) at XMLHttpRequest.onXHRLoad [as onload] (F:\Node\titre\node_modules\jsmediatags\build2\XhrFileReader.js:184:21) at XMLHttpRequestEventTarget.dispatchEvent (F:\Node\titre\node_modules\xhr2\lib\xhr2.js:64:18) at XMLHttpRequest._dispatchProgress (F:\Node\titre\node_modules\xhr2\lib\xhr2.js:555:12) at XMLHttpRequest._onHttpResponseEnd (F:\Node\titre\node_modules\xhr2\lib\xhr2.js:510:12) at IncomingMessage. (F:\Node\titre\node_modules\xhr2\lib\xhr2.js:469:24) at IncomingMessage.emit (events.js:205:15) at endReadableNT (_stream_readable.js:1154:12) at processTicksAndRejections (internal/process/task_queues.js:84:9)`

I'm pretty new with node so peraphs it's obvious

thanks !

Borewit commented 4 years ago

const mm = require('music-metadata');

const url = 'http://20043.live.streamtheworld.com/LOS40_SC';

const httpClient = new HttpClient()
const response = await httpClient.get(url);

mm.parseStream(response.stream, response.headers['content-type']).then(metadata => {
  // do something with the metadata
});

You can take any HTTP client, as long as it provides a stream. The one in this example is something like this (TypeScript):

import * as Stream from "stream";
import * as https from "https";
import * as http from "http";

export interface IHttpResponse {
  headers: { [id: string]: string; }
  stream: Stream.Readable;
}

export interface IHttpClient {
  get: (url: string) => Promise<IHttpResponse>;
}

export class HttpClient implements IHttpClient {

  public get(url: string): Promise<IHttpResponse> {
    return new Promise<IHttpResponse>((resolve, reject) => {
      const request = ((url.startsWith('https') ? https : http) as typeof http).get(url);
      request.on('response', resp => {
        resolve({
          headers: resp.headers as any,
          stream: resp
        });
      });
      request.on('abort', () => {
        reject(new Error('abort'));
      });
      request.on('error', err => {
        reject(err);
      });
    });
  }
}