如何将二进制视频数据保存到天蓝色斑点?

时间:2020-02-04 22:26:36

标签: javascript node.js buffer azure-blob-storage video-indexer

我当前正在使用此代码从本地磁盘中选择文件到我的api:

<script language="javascript" type="text/javascript">
$(document).ready(function(){

$(':file').on('change', function () {
  var file = this.files[0];

    if (file.type !== "video/mp4" && file.type!== "video/quicktime") {
        alert("Content must be video .mp4 or .mov")
    }

$(':button').on('click', function () {
    if (file.type == "video/mp4" || file.type == "video/quicktime"){
  $.ajax({
    // Your server script to process the upload
    url: 'azureAPI',
    type: 'POST',

    // Form data
    data: new FormData($('form')[0]),

    // Tell jQuery not to process data or worry about content-type
    // You *must* include these options!
    cache: false,
    contentType: false,
    processData: false,

    // Custom XMLHttpRequest
    xhr: function () {
      var myXhr = $.ajaxSettings.xhr();
      if (myXhr.upload) {
        // For handling the progress of the upload
        myXhr.upload.addEventListener('progress', function (e) {
          if (e.lengthComputable) {
            $('progress').attr({
              value: e.loaded,
              max: e.total,
            });
          }
        }, false);
      }
      return myXhr;
    }
  });
} else {
    alert ("File type must be .mp4 or .mov")
}
});
});
});
</script>

这将以以下形式发送(我假设是)二进制数据:

���
1!QAa"q2B���R�#3br��u�����S6C$%��5�cts�T&D4��U��d���e!1AQa2"q�#����3��B���X"��?��!=��W�u�ٗ�-2���?����ۯ�Կ�i���t����M���Y�-��-Vdϊ�P�<�<U#TY]K��dW
���

我正在使用Azure,现在尝试将其发送到Microsoft Video Indexer,后者说是将数据作为主体中的multipart / form-data发送。 (请参阅https://api-portal.videoindexer.ai/docs/services/Operations/operations/Upload-Video?)

我尝试在主体中发送二进制数据,但是它说它需要字符串/缓冲区。

然后我尝试以var body = Buffer.from(req.body,'binary')的形式在主体中发送二进制数据

哪个发送了,但是VI回答说可能是在索引数据时出现问题,也许是因为我发送的编码错误?

要解决此问题,我现在尝试首先将二进制数据保存到块Blob,然后再调用该URL,但是我在使用以下方法将二进制数据保存到Azure块Blob时遇到问题:

var buf = Buffer.from(req.body, 'binary');
blobService.createBlockBlobFromText(container, 'fileName.mp4', buf, {contentSettings: {contentType: 'video/mp4', contentEncoding: 'binary'}}, function (error, result, response) {
    if(!error){
        callback('uploaded');
    } else {
        callback('nope');
    }

});

我尝试了此操作,起初没有contentSettings,但是将数据另存为contentType: application/octet-stream,但并未将其作为视频打开。然后,我添加了contentType,最后尝试也添加了contentEncoding

这保存了正确的contentType,但仍无法打开视频。

有人知道如何正确编码数据,或者首先将其直接发送到视频索引器,或者其次将二进制数据保存到Azure blob存储吗?

感谢任何指点,如果我遗漏任何内容,我们深表歉意。

1 个答案:

答案 0 :(得分:1)

根据我的测试,如果要使用Azure函数将文件上传到Azure blob,请参考以下代码。

前端

<!DOCTYPE html>
<html>
  <script type="text/javascript"
 src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.4.1.js">
</script>
<body>

 <form enctype="multipart/form-data">
    <input name="file" type="file" accept="video/*"/>
    <input type="button" value="Upload" />
</form>
<progress></progress>

<script language="javascript" type="text/javascript">
$(document).ready(function(){

$(':file').on('change', function () {
  var file = this.files[0];
    if (file.type !== "video/mp4" && file.type!== "video/quicktime") {
        alert("Content must be video .mp4 or .mov")
    }

$(':button').on('click', function () {

  if (file.type == "video/mp4" || file.type == "video/quicktime"){

    $.ajax({
    // Your server script to process the upload
    url: '',
    type: 'POST',
    crossDomain: true,
    enctype: 'multipart/form-data',
    // Form data
    data: new FormData($('form')[0]),

    // Tell jQuery not to process data or worry about content-type
    // You *must* include these options!
    cache: false,
    contentType: false,
    processData: false,

    success :  function(data){console.log(data);},

    // Custom XMLHttpRequest
    xhr: function () {
      var myXhr = $.ajaxSettings.xhr();
      if (myXhr.upload) {
        // For handling the progress of the upload

        myXhr.upload.addEventListener('progress', function (e) {
          if (e.lengthComputable) {
            $('progress').attr({
              value: e.loaded,
              max: e.total,
            });
          }
        }, false);
      }
      return myXhr;
    }
  });
} else {
    alert ("File type must be .mp4 or .mov")
}
});





});



});

</script>

</body>
</html>

天蓝色功能

var multipart = require('parse-multipart')
var azure = require('azure-storage');
var getStream = require('into-stream')
module.exports = async function (context, request) {
    context.log('JavaScript HTTP trigger function processed a request.');
    // encode body to base64 string
    var bodyBuffer = Buffer.from(request.body);

    var boundary = multipart.getBoundary(request.headers['content-type']);
    // parse the body
    var parts = multipart.Parse(bodyBuffer, boundary);

    const accountname ="blobstorage0516";
            const key = "key";
            const containerName="test";
            var retryOperations = new azure.ExponentialRetryPolicyFilter();
            const blobClient  =azure.createBlobService(accountname,key).withFilter(retryOperations);
            blobClient.createContainerIfNotExists(containerName, function (error) {
                if (error) {
                  context.log(error);
                }
            });

            var options = {
                contentSettings:{contentType: parts[0].type},
                metadata: { fileName: parts[0].filename },
                blockSize:8*1024*1024,
                parallelOperationThreadCount:20,
                timeoutIntervalInMs:30*60*1000
              };
              var stream =getStream(parts[0].data)
              context.log("start")

              var result="ok"
            var speedsummary= blobClient.createBlockBlobFromStream(containerName,parts[0].filename,stream,parts[0].data.length,options,function (error) {


              if (error != null) {
               result=error
              } else {



              }})

               context.res = { body : { results : result}};
              context.done(); 


};

enter image description here enter image description here

您还可以从bloburl

访问视频