【问题标题】:Pipe multiple JPG's into an animated GIF using Node JS使用 Node JS 将多个 JPG 导入动画 GIF
【发布时间】:2017-03-14 23:09:21
【问题描述】:

我正在尝试将 5 个特定的 JPG 文件名导入动画 GIF。我浏览了几个库并找到了Gif-Encoder。我不太擅长流。我似乎无法弄清楚如何将 RGB 结果从JPEG Decoder 传送到编码器的 addFrame() 方法中。

function createAnimatedGif(jpgPaths, animatedGifPath) {
    let encoder = new GifEncoder(1280, 720);

    let writeStream = fs.createWriteStream(animatedGifPath);
    encoder.pipe(writeStream);
    encoder.writeHeader();

    jpgPaths.forEach(filePath => {
        fs.createReadStream(filePath)
            .pipe(new JPEGDecoder)
            //.pipe(encoder);
            //.pipe(pixels => encoder.addFrame(pixels));
    });

    encoder.finish();
}

【问题讨论】:

    标签: javascript node.js encoding stream animated-gif


    【解决方案1】:

    必须使用读取流吗?我使用模块“get-pixels”取得了一些成功。

    var getPixels = require('get-pixels')
    var GifEncoder = require('gif-encoder');
    var gif = new GifEncoder(1280, 720);
    var file = require('fs').createWriteStream('img.gif');
    var pics = ['./pics/1.jpg', './pics/2.jpg', './pics/3.jpg'];
    
    gif.pipe(file);
    gif.setQuality(20);
    gif.setDelay(1000);
    gif.writeHeader();
    
    var addToGif = function(images, counter = 0) {
      getPixels(images[counter], function(err, pixels) {
        gif.addFrame(pixels.data);
        gif.read();
        if (counter === images.length - 1) {
          gif.finish();
        } else {
          addToGif(images, ++counter);
        }
      })
    }
    addToGif(pics);
    

    【讨论】:

    • 它可以在没有 gif.read() 的情况下工作...你能解释一下 gif.read() 的目的吗?有必要吗?谢谢。
    • gif-encoder documentation 中没有很好地记录它,但是如果您阅读“错误”事件的文档,它会显示Emits an Error when internal buffer is exceeded. This occurs when you do not read (either via .on('data') or .read()) and we cannot flush prepared data. 所以虽然不是严格需要,但如果我使用没有它的许多图像。
    猜你喜欢
    • 2017-06-13
    • 1970-01-01
    • 2018-03-24
    • 2021-06-24
    • 1970-01-01
    • 2011-04-28
    • 2012-11-19
    • 2018-09-13
    相关资源
    最近更新 更多