转码器API无法通过Firebase Cloud Functions工作

eyh26e7m  于 2023-08-07  发布在  其他
关注(0)|答案(1)|浏览(106)

我在我的Unity应用程序中使用Firebase。用户可以将视频上传到Firebase存储(它工作良好),然后我在Firebase Cloud Functions中触发转码视频(通过Google Cloud Platform Transcoder API)。这不是工作,我不知道为什么。
Google Cloud控制台云函数错误:“错误:3 INVALID_ARGUMENT:请求中的资源字段值无效。”
下面是我的代码:

const functions = require("firebase-functions");
    const admin = require("firebase-admin");
    admin.initializeApp();
    
    const db = admin.firestore();
    const { TranscoderServiceClient } = require('@google-cloud/video-transcoder');
    const { Storage } = require('@google-cloud/storage');
    const storage = new Storage();
    
//other functions..
//

    exports.transcodeVideo = functions.storage.object().onFinalize(async (object) => {
      try {
        const fileBucket = object.bucket;
        const filePath = object.name;
        const videoPath = `gs://${fileBucket}/${filePath}`;
    
        // Only if in videos/..
        if (!filePath.startsWith('videos/')) {
          console.log('Nem történt videó feltöltés az videos/ könyvtárba. A Cloud Function nem fut le.');
          return null;
        }
    
        console.log(`1. Transcode folyamat elindítva a videóhoz: ${videoPath}`);
    //videoPath is good!
    
        // A Google Cloud Transcoder API kliens inicializálása
        const transcoderClient = new TranscoderServiceClient();
    
        // Az új transcode elrendezés konfigurálása
        const jobTemplate = {
          jobConfig: {
            inputUri: videoPath,
            outputUri: 'gs://MY_APP.appspot.com/output/optimised.mp4', // THIS IS ONLY TEST NAME. MY_APP is my app.
            elementaryStreams: [
              {
                key: 'video_stream',
                videoStream: {
                  codec: 'h264',
                  heightPixels: 360, // Válaszd meg a kívánt alacsony SD felbontást
                  widthPixels: 640, // Válaszd meg a kívánt alacsony SD felbontást
                  bitrateBps: 1000000 // Válaszd meg a kívánt alacsony SD bitrátát
                }
              }
            ],
            muxStreams: [
              {
                key: 'sd_mux_stream',
                container: 'mp4'
              }
            ]
          }
        };
    
        console.log(`start transcode`); //After this get error in my console
        
        // Az új transcode elrendezés létrehozása és futtatása
        const [response] = await transcoderClient.createJob({
          job: jobTemplate
        });
    
        console.log(`Transcode folyamat elindítva a videóhoz: ${videoPath}`);
        //I don't see this in console, error before this.
    
        // A nem optimalizált videó törlése
        await storage.bucket(fileBucket).file(filePath).delete();
        console.log(`Nem optimalizált videó törölve: ${videoPath}`);
      } catch (error) {
        console.error('Hiba történt a transcode folyamat során:', error);
      }
    });

字符串
我不知道是什么问题..其他功能运行良好。云存储具有服务帐户权限。我使用“firebase deploy”命令,因为我的项目基地在firebase。我看到在堆栈溢出可能需要其他权限的“gcloud deploy...",但我使用firebase.也需要安装gcloud吗?Firebase存储=云存储,我认为这是需要工作的,但我不知道为什么总是得到这个错误。
请帮帮我谢谢,艾利安

toiithl6

toiithl61#

我并不精通Google Cloud Transponder,但看看Transcoder API Documentation和官方的sample,你需要向createJob()方法传递一个具有以下两个属性的对象:

{
  parent,
  job,
}

字符串
parent是表示创建和处理作业的父位置的字符串(格式:projects/{project}/locations/{location})。我不清楚是否需要,样本和文档在这方面不一致。
jobIJobTemplate类型的复合对象,具有以下属性:nameconfiglabels。反过来,config是一个IJobConfig类型的复合对象。
因此,您需要沿着以下方式(未经测试)执行一些操作:

const request = {
      parent: "....",
      job: {
        name: "...",
        config: {
            inputUri: videoPath,
            outputUri: 'gs://MY_APP.appspot.com/output/optimised.mp4',
            ...
        }
      }
   };

// ...

const [response] = await transcoderClient.createJob(request);
// ...

  • (希望它能帮助你,因为这个答案严格基于文档)*

相关问题