如何捕获通过HTTP流式传输的mp3的前10秒

时间:2015-03-12 19:00:38

标签: node.js audio pcm icecast node-lame

免责声明:nodeJS和音频解析的新手

我正试图在node-icecast的帮助下通过expressJS应用程序代理数字广播流,效果很好。我正在收听收音机的mp3流,并通过node-lame将mp3解码为PCM,然后将其发送到扬声器。所有这些都直接来自github项目的自述示例:

var lame = require('lame');
var icecast = require('icecast');
var Speaker = require('speaker');

// URL to a known Icecast stream
var url = 'http://firewall.pulsradio.com';

// connect to the remote stream
icecast.get(url, function (res) {

// log the HTTP response headers
console.error(res.headers);

// log any "metadata" events that happen
res.on('metadata', function (metadata) {
  var parsed = icecast.parse(metadata);
  console.error(parsed);
});

// Let's play the music (assuming MP3 data).
// lame decodes and Speaker sends to speakers!
res.pipe(new lame.Decoder())
   .pipe(new Speaker());
});

我现在正尝试使用Doreso API设置服务来识别音乐。问题是我正在使用流而没有文件(我还不了解可读和可写的流,并且学习速度慢)。我一直在寻找一段时间来尝试编写流(理想情况下是内存),直到我有大约10秒钟的价值。然后我会将那部分音频传递给我的API,但是我不知道这是否可能或者知道从哪里开始切片10秒的流。我想可能尝试将流传递给ffmpeg,因为它有持续时间的-t选项,也许这可能会限制它,但是我还没有那个工作呢。

任何将流减少到10秒的建议都会很棒。谢谢!

更新了:改变了我的问题,因为我原本以为我正在使用PCM并转换为mp3 ;-)我把它倒退了。现在我只想切断流的一部分,而流仍在播放扬声器。

2 个答案:

答案 0 :(得分:2)

这并不容易......但是我本周末已经成功了。如果你们能指出如何改进这些代码,我会很高兴。我不太喜欢模拟"结束"一条小溪是否有类似"分离"或"重新布线"节点中流的管道部分?

首先,您应该创建自己的Writable Stream类,它本身会创建一个蹩脚的编码实例。该可写流将接收解码的PCM数据。

它的工作原理如下:

var stream = require('stream');
var util = require('util');
var fs = require('fs');
var lame = require('lame');
var streamifier = require('streamifier');
var WritableStreamBuffer = require("stream-buffers").WritableStreamBuffer;

var SliceStream = function(lameConfig) {

    stream.Writable.call(this);

    this.encoder = new lame.Encoder(lameConfig);

    // we need a stream buffer to buffer the PCM data
    this.buffer = new WritableStreamBuffer({
        initialSize: (1000 * 1024),      // start as 1 MiB.
        incrementAmount: (150 * 1024)    // grow by 150 KiB each time buffer overflows.
    });
};

util.inherits(SliceStream, stream.Writable);

// some attributes, initialization
SliceStream.prototype.writable = true;
SliceStream.prototype.encoder = null;
SliceStream.prototype.buffer = null;

// will be called each time the decoded steam emits "data" 
// together with a bunch of binary data as Buffer
SliceStream.prototype.write = function(buf) {

    //console.log('bytes recv: ', buf.length);

    this.buffer.write(buf);

    //console.log('buffer size: ', this.buffer.size());
};

// this method will invoke when the setTimeout function
// emits the simulated "end" event. Lets encode to MP3 again...
SliceStream.prototype.end = function(buf) {

    if (arguments.length) {
        this.buffer.write(buf);
    }
    this.writable = false;

    //console.log('buffer size: ' + this.buffer.size());

    // fetch binary data from buffer
    var PCMBuffer = this.buffer.getContents();

    // create a stream out of the binary buffer data
    streamifier.createReadStream(PCMBuffer).pipe(

        // and pipe it right into the MP3 encoder...
        this.encoder
    );

    // but dont forget to pipe the encoders output 
    // into a writable file stream
    this.encoder.pipe(
        fs.createWriteStream('./fooBar.mp3')
    );
};

现在您可以将已解码的流传输到SliceStream类的实例中,就像这样(除了其他管道之外):

icecast.get(streamUrl, function(res) {

    var lameEncoderConfig = {
        // input
        channels: 2,        // 2 channels (left and right)
        bitDepth: 16,       // 16-bit samples
        sampleRate: 44100,   // 44,100 Hz sample rate

        // output
        bitRate: 320,
        outSampleRate: 44100,
        mode: lame.STEREO // STEREO (default), JOINTSTEREO, DUALCHANNEL or MONO
    };
    var decodedStream = res.pipe(new lame.Decoder());

    // pipe decoded PCM stream into a SliceStream instance
    decodedStream.pipe(new SliceStream(lameEncoderConfig));

    // now play it...
    decodedStream.pipe(new Speaker());

    setTimeout(function() {

        // after 10 seconds, emulate an end of the stream.
        res.emit('end');

    }, 10 * 1000 /*milliseconds*/)
});

答案 1 :(得分:0)

我可以建议在10秒后使用removeListener吗?这将防止将来的事件通过听众发送。

dialog.show()