【问题标题】:How to create a Video URL Blob in NodeJS?如何在 NodeJS 中创建视频 URL Blob?
【发布时间】:2021-10-25 06:18:50
【问题描述】:

你能帮我在 NodeJS 中创建一个视频 URL Blob 吗?

    var xhr = new XMLHttpRequest();
    xhr.responseType = 'blob';

    xhr.onload = function() {
        var reader = new FileReader();

        reader.onloadend = function() {
            var byteCharacters = atob(reader.result.slice(reader.result.indexOf(',') + 1));

            var byteNumbers = new Array(byteCharacters.length);

            for (var i = 0; i < byteCharacters.length; i++) {
                byteNumbers[i] = byteCharacters.charCodeAt(i);
            }

            var byteArray = new Uint8Array(byteNumbers);
            var blob = new Blob([ byteArray ], { type: 'video/ogg' });
            var url = URL.createObjectURL(blob);

            console.log(url);
        };

        reader.readAsDataURL(xhr.response);
    };

    xhr.open(
        'GET',
        '...Video URL...'
    );
    xhr.send();

错误输出:

  throw new Error('cannot read as File: ' + JSON.stringify(file));   
  Error: cannot read as File: undefined

我使用过 XMLHttpRequest、URL、FileReader 和 Blob 包

请帮忙,谢谢

【问题讨论】:

    标签: node.js video xmlhttprequest blob filereader


    【解决方案1】:

    您可以使用 Azure Cloud 存储视频,然后使用 Azure BLOB

    【讨论】:

    • 使用 Azure 云
    • 和 Azure Blob。
    • 我没有使用 Azure 云
    • 但使用谷歌云
    • 创建 blob
    【解决方案2】:

    Node.JS 没有XMLHttpRequestFileReaderBlob 等,而是使用fshttp 模块和其他内置类,如Buffer

    尝试在服务器上使用这些浏览器功能,即使使用尝试模拟它们的软件包,也可能并不总是有效。事实上,许多模拟这些功能的库并不包含所有内容。例如xmlhttprequest节点包不使用.response,而只使用文本响应。

    在 Node.JS 中,不需要 blob URL(您甚至会在哪里使用它?),您只需直接与 Buffers 交互。

    // making a http request to fetch something
    const http = require("https");
    
    const request = https.request({
      method: "GET",
      url: "https://example.com/video.ogg"
    }, (res) => {
      const chunks = [];
    
      res.on("data", (chunk) => {
        chunks.push(chunk);
      });
    
      res.on("end", () => {
        // raw byte data
        const buffer = Buffer.concat(chunks);
    
        /*
         * Interact with Buffer here
         */
    
      });
    });
    
    request.end();
    

    【讨论】:

    • 错误出现:'errno:'ECONNREFUSED',代码:'ECONNREFUSED',系统调用:'connect',地址:'127.0.0.1',端口:443'
    • 我只是想制作一个像 (YouTube) 这样的应用程序,任何人都可以在其中上传自己的视频,所以,我需要 Blob 使视频只能通过网站访问。我想用 node.js 在后端以“blob:localhost:8080/10dcd82b-a53d-4799-82ee-0aaaa1a889d5”的格式制作 blob(因为前端暴露了一切!)。所以,我需要使用 node.js 在后端创建一个 blob,并将结果 blob 传递给前端并显示视频
    • Blob URLs 不能在后端创建。 Blob URL 与创建它的文档/网页相关联,并在关闭选项卡时被删除。它们只能由制作它的计算机/浏览器访问。
    • 任何私下创建 Blob 以使 URL 不暴露的解决方案?
    猜你喜欢
    • 1970-01-01
    • 2014-12-19
    • 2016-05-01
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    • 1970-01-01
    相关资源
    最近更新 更多