【问题标题】:How to upload video to youtube from firebase cloud function如何从 Firebase 云功能将视频上传到 youtube
【发布时间】:2020-10-29 13:12:09
【问题描述】:

有没有办法将视频从 firebase 云功能上传到 youtube?
如果可能,如果我们使用 Firebase 云功能上传 2 Gb 数据,是否会收取 2 Gb 数据传输更改 0.12 美元*2 的费用

【问题讨论】:

    标签: node.js google-cloud-functions youtube-api


    【解决方案1】:

    您可以将 Nodejs 客户端库用于 Youtube API。我建议您通过Quickstart for this client library 并关注YouTube API Samples,您可以在其中找到有关如何上传视频的示例:

    'use strict';
    
    /**
     * Usage: node upload.js PATH_TO_VIDEO_FILE
     */
    
    const fs = require('fs');
    const path = require('path');
    const readline = require('readline');
    const {google} = require('googleapis');
    const {authenticate} = require('@google-cloud/local-auth');
    
    // initialize the Youtube API library
    const youtube = google.youtube('v3');
    
    // very basic example of uploading a video to youtube
    async function runSample(fileName) {
      // Obtain user credentials to use for the request
      const auth = await authenticate({
        keyfilePath: path.join(__dirname, '../oauth2.keys.json'),
        scopes: [
          'https://www.googleapis.com/auth/youtube.upload',
          'https://www.googleapis.com/auth/youtube',
        ],
      });
      google.options({auth});
    
      const fileSize = fs.statSync(fileName).size;
      const res = await youtube.videos.insert(
        {
          part: 'id,snippet,status',
          notifySubscribers: false,
          requestBody: {
            snippet: {
              title: 'Node.js YouTube Upload Test',
              description: 'Testing YouTube upload via Google APIs Node.js Client',
            },
            status: {
              privacyStatus: 'private',
            },
          },
          media: {
            body: fs.createReadStream(fileName),
          },
        },
        {
          // Use the `onUploadProgress` event from Axios to track the
          // number of bytes uploaded to this point.
          onUploadProgress: evt => {
            const progress = (evt.bytesRead / fileSize) * 100;
            readline.clearLine(process.stdout, 0);
            readline.cursorTo(process.stdout, 0, null);
            process.stdout.write(`${Math.round(progress)}% complete`);
          },
        }
      );
      console.log('\n\n');
      console.log(res.data);
      return res.data;
    }
    
    if (module === require.main) {
      const fileName = process.argv[2];
      runSample(fileName).catch(console.error);
    }
    
    module.exports = runSample;
    

    关于定价,Youtube API 是免费的,但您的费用将取决于您选择的Firebase plan

    【讨论】:

      猜你喜欢
      • 2021-06-16
      • 2021-10-28
      • 2011-07-24
      • 2020-08-01
      • 2012-09-16
      • 2020-07-05
      • 1970-01-01
      • 2015-02-01
      • 2017-12-21
      相关资源
      最近更新 更多