如何将二进制视频数据保存到天蓝色斑点? [英] How to save binary video data to azure blob?

查看:92
本文介绍了如何将二进制视频数据保存到天蓝色斑点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently using this code to select a file from local disk to my 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>

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

This sends (what I am assuming is) binary data in the form of this:

���
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 ?)

I am using Azure, and now trying to send this to Microsoft Video Indexer, which says to send the data as multipart/form-data in the body. (see https://api-portal.videoindexer.ai/docs/services/Operations/operations/Upload-Video?)

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

I tried sending the binary data in the body, but it said it required string/buffer.

然后我尝试将主体中的二进制数据作为var body = Buffer.from(req.body,'binary')

I then tried sending the binary data in the body as var body = Buffer.from(req.body,'binary')

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

Which sent, but VI responded saying that there was an issue indexing the data, perhaps as I sent with the wrong encoding?

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

To work around this, I am now trying to save that binary data to a block blob first, then I will call that url after, however I am having trouble saving binary data to Azure block blob using:

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.

I tried this, without the contentSettings at first but that saved the data as contentType: application/octet-stream which wasn't opening as a video. I then added contentType, and lasted tried adding contentEncoding as well.

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

This saved the correct contentType but still the video could not be opened.

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

Does anyone know how to encode the data correctly to either send in the first instance straight to video indexer, or secondly to save binary data to Azure blob storage?

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

Thanks for any pointers, apologies if I left anything out.

推荐答案

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

According to my test, if you want to use Azure function to upload file to Azure blob, please refer to the following code.

前端

<!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(); 


};

您还可以从

You also can access the video from the bloburl

这篇关于如何将二进制视频数据保存到天蓝色斑点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆