node.js - 以 block 方式上传视频 api 调用

标签 node.js facebook-graph-api

我正在创建一个 npm 模块,以使用 block 上传在 facebook api 上上传视频。 我想知道是否应该使用 Node 流。
目前我正在这样做:
1.请求api获取chunk的大小。
2. 使用 unix split cmd 将原始视频分割成 block
3. 逐 block 发送。
4.删除所有 block

我用流进行了一些测试,输出 block 看起来不错。

var readable = fs.createReadStream('video.mp4');
var i = 0;
readable.on('readable', () => {
  var chunk;
  while(null !== (chunk = readable.read(1048576))) {
    var fd = fs.openSync('chunk' + i++, 'w');
    fs.writeSync(fd, chunk, 0, chunk.length);
  }
});

我应该使用 Stream 进行重构吗? 这是当前git repo的链接
感谢您抽出时间。

最佳答案

这是我一年前使用 Facebook Graph API 在 Node Js 中上传视频的代码:

// It publishes the video in chunks to Facebook until it finishes
var publishVideo = function(access_token, text, file) {

  var stats = fs.statSync(file.path);

  var formData = {
    access_token: access_token,
    upload_phase: 'start',
    file_size: stats.size
  };

  // First step, we send the video size
  request.post({ url: 'https://graph-video.facebook.com/v2.3/me/videos', form: formData, json: true },
  function(err, response, body) {
    if (!err) {
      // With the response, we start making the video transfer
      transferProcess(undefined, file, access_token, body, function(err, currentUploadSession) {
        if (!err) {

          var formData = {
            access_token: access_token,
            upload_phase: 'finish',
            upload_session_id: currentUploadSession,
            description: text
          };

          // Once the video transfer ended, we publish the video
          request.post({ url: 'https://graph-video.facebook.com/v2.3/me/videos', form: formData, json: true });
        }
      });
    }
  });
};

// It processes each part of the video until it finishes
var transferProcess = function(uploadSession, file, access_token, body, callback) {

  // First we generate a copy of the file in order to be independent to the original file
  // because it can have problems when opening it at the same time from other file
  var copyFileName = file.path + '-facebook';
  fse.copySync(file.path, copyFileName);

  // Once we have the copy, we open it
  var fd = fs.openSync(copyFileName, 'r');

  var bytesRead, data, bufferLength = 1000000000;
  var buffer = new Buffer(bufferLength);

  var length = body.end_offset - body.start_offset;

  // We read the amount of bytes specified from body.start_offset until length
  bytesRead = fs.readSync(fd, buffer, body.start_offset, length, null);
  data = bytesRead < bufferLength ? buffer.slice(0, bytesRead) : buffer;

  // We generate a file with the recently read data, and with a name of copyFileName-chunked-12313123
  var chunkFileName = copyFileName + '-chunked-' + body.start_offset;

  // We create the file so then we can read it and send it
  fs.writeFile(chunkFileName, data, function(err) {
    if (err) {
      callback(err);
    }
    else {

      var currentUploadSession = uploadSession ? uploadSession : body.upload_session_id;
      var startOffset = parseInt(body.start_offset);

      var formData = {
        upload_phase: 'transfer',
        start_offset: startOffset,
        upload_session_id: currentUploadSession,
        access_token: access_token
      };

      formData.video_file_chunk = fs.createReadStream(chunkFileName);

      // Once we have the file written, we upload it
      request.post({ url: 'https://graph-video.facebook.com/v2.3/me/videos',
      formData: formData, json: true }, function (err, response, body) {
        // If there was an error, we return it
        if (err || body.error) {
          callback(err ? err : body.error, null);
        }
        // If the lecture of the file has ended, facebook will send us the body.start_offset and the body.end_offset,
        // if they are the same, it means that we have ended uploading the video, so we return
        else if (body.start_offset === body.end_offset) {
          callback(err, currentUploadSession);
        }
        // Else, we keep reading the file
        else {
          transferProcess(currentUploadSession, file, access_token, body, callback);
        }
      });
    }
  });
};

希望您能使用它。

乔尔

关于node.js - 以 block 方式上传视频 api 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37690861/

相关文章:

facebook - 升级到 graph api 2.10 后出现意外的 "Cannot call API on behalf of this user"

php - (#322) 主题没有权限标记这张照片 - Facebook PHP SDK v4

node.js - 无法找到模块 - 使用 esm nodejs 应用程序开玩笑

javascript - Node.js 中的 Jquery 错误 '$ is not a function'

ios - 无法使用 facebook-graph-api version2.0 获取 facebook 好友列表

swift - 使用 Firebase 的 OAuth 问题通过 Facebook 登录(input_token 中的 App_id 与查看应用程序不匹配)

javascript - express.js 路由中的参数类型

javascript - 在 Jest 测试中,使用 requireActual 不需要模块的实际版本

javascript - 无法在node js中使用 sleep 或暂停

ios - 怎么知道是哪个FBRequest发起了请求didLoad : method?