javascript – 使用Node JS保存AR-Drone 2.0视频流

我正试图找到一种方法来保存从AR-Drone流式传输视频时产生的.png图像.以下使用felixge的ar-drone模块的代码成功地将视频流式传输到浏览器,但我想拍摄此源的屏幕截图并将其保存到我的驱动器中进行实时图像分析.

我正在尝试使用屏幕截图模块来保存连接到主机时显示在浏览器中的内容,但所有这一切都是保存我指定大小的空白白色图像(请参阅下面的代码).有没有更好的方法来保存AR-Drone视频中的静止帧(不保存.h264视频输入)?

谢谢.

 // Run this to receive a png image stream from your drone.
var arDrone = require('ar-drone'); //Call AR-drone module
var http = require('http'); //Allows video to be displayed to browser
var client = arDrone.createClient(); //Create AR-drone client--->IP address, video frame rate, image sizing

var pngStream = client.getPngStream(); //Fetch real-time video feed from quad
var fs         = require('fs');
var screenshot = require('node-webkit-screenshot');

client.config('general:navdata_options', 777060865); //turn on GPS
client.config('video:video_channel',3); //Switch feed to downward-facing camera

var lastPng;
pngStream 
  .on('error', console.log)
  .on('data', function(pngBuffer) {
lastPng = pngBuffer; //Store latest png still frame
});

var server = http.createServer(function(req, res) {
if (!lastPng) {
  res.writeHead(503);
  res.end('Did not receive any png data yet.');
  return;
}

res.writeHead(200, {'Content-Type': 'image/png'});
res.end(lastPng);
});

server.listen(8080, function() {
   console.log('Serving latest png on port 8080 ...');
})

screenshot({
   url : 'http://localhost:8080',
   width : 640,
   height : 360
})
.then(function(buffer){
   fs.writeFile('Drone.png', buffer, function(){ 
     console.log('Screenshot saved') ;
     screenshot.close();
  });
});

最佳答案 PngStream已经允许您访问PNG图像,您只需将它们写入文件即可.以下代码显示如何每5000毫秒将帧保存到文件:

var arDrone = require('ar-drone');
var client = arDrone.createClient();
var fs = require('fs');

var pngStream = client.getPngStream();
var frameCounter = 0;
var period = 5000; // Save a frame every 5000 ms.
var lastFrameTime = 0;

pngStream
  .on('error', console.log)
  .on('data', function(pngBuffer) {
    var now = (new Date()).getTime();
    if (now - lastFrameTime > period) {
      frameCounter++;
      lastFrameTime = now;
      console.log('Saving frame');
      fs.writeFile('frame' + frameCounter + '.png', pngBuffer, function(err) {
        if (err) {
          console.log('Error saving PNG: ' + err);
        }
      });
    }
  });

以下是运行代码时的样子:

[wiseman@Intercept node-ar-drone (master)]$node temp.js
Saving frame
Saving frame
Saving frame
[...]

该运行保存了frame1.png,frame2.png和frame3.png中的帧.这是frame1.png:

我也用向下的相机测试了代码,它的效果也很好.

点赞