tencent cloud

Feedback

Last updated: 2022-05-24 16:08:49

    Calculating Upload Signature

    Video upload from client refers to uploading local videos to the VOD platform by an end user of the application. For more information, please see Guide. This document describes how to generate a signature for upload from client.

    Overview

    The overall process for upload from client is as follows:

    To support upload from client, you need to build two backend services: signature distribution service and event notification receipt service.

    • The client first requests an upload signature from the signature distribution service.
    • The signature distribution service verifies whether the client user has the upload permission. If the verification passes, a signature will be generated and delivered; otherwise, an error code will be returned, and the upload process will end.
    • After receiving the signature, the client will use the upload feature integrated in the UGSV SDK to upload the video.
    • After the upload is completed, the VOD backend will send an upload completion event notification to your event notification receipt service.
    • If the signature distribution service specifies a video processing task flow in the signature, the VOD service will automatically process the video accordingly after the video is uploaded. Video processing in UGSV scenarios is generally AI-based porn detection.
    • After the video is processed, the VOD backend will send a task flow status change event notification to your event notification receipt service.

    At this point, the entire video upload and processing flow ends.

    Signature generation

    For more information on the signature for upload from client, please see Signature for Upload from Client.

    Signature distribution service implementation sample

    /**
    * Calculate a signature
    */
    function createFileUploadSignature({ timeStamp = 86400, procedure = '', classId = 0, oneTimeValid = 0, sourceContext = '' }) {
      // Determine the generation time and expiration time of the signature
      let current = parseInt((new Date()).getTime() / 1000)
      let expired = current + timeStamp;  // Signature validity period: 1 day
      // Enter the parameters into the parameter list
      let arg_list = {
          //required
          secretId: this.conf.SecretId,
          currentTimeStamp: current,
          expireTime: expired,
          random: Math.round(Math.random() * Math.pow(2, 32)),
          //opts
          procedure,
          classId,
          oneTimeValid,
          sourceContext
      }
      // Calculate the signature
      let orignal = querystring.stringify(arg_list);
      let orignal_buffer = new Buffer(orignal, "utf8");
      let hmac = crypto.createHmac("sha1", this.conf.SecretKey);
      let hmac_buffer = hmac.update(orignal_buffer).digest();
      let signature = Buffer.concat([hmac_buffer, orignal_buffer]).toString("base64");
      return signature;
    }
    /**
    * Respond to a signature request
    */
    function getUploadSignature(req, res) {
      res.json({
          code: 0,
          message: 'ok',
          data: {
              signature: gVodHelper.createFileUploadSignature({})
          }
      });
    }
    

    Connection Process

    Publishing short video

    Upload a .mp4 file to Tencent Video Cloud and get the online watch URL. Tencent Video Cloud can meet various video watch needs such as nearby scheduling, instant live streaming and playback, dynamic acceleration, and global connection, delivering a smooth watch experience.

    • Step 1. Use the TXUGCRecord API to shoot a short video, and a .mp4 short video file will be generated after the shoot ends and be called back.
    • Step 2. Your application applies for an upload signature ("credential" for the application to upload the .mp4 file to VOD) to your business server. To ensure the security, upload signatures should be distributed by your business server but not generated by the client application.
    • Step 3. Use the TXUGCPublish API to publish the video. After the video is successfully published, the SDK will call back the watch URL to you.

    Notes

    • You should never write the SecretID or SecretKey for upload signature calculation into the client code of the application, as their disclosure will cause security risks. If attackers get such information by cracking the application, they can misappropriate your traffic and storage service.
    • The correct practice is to generate a one-time upload signature by using the SecretID and SecretKey on your server and send the signature to the application. As the server is generally hard to be intruded, the security is guaranteed.
    • When publishing a short video, please make sure that the Signature field is correctly passed in; otherwise, the release will fail.

    Connection directions

    1. Select a video

    You can upload the shot or edited video as described in previous documents or upload a local video on your phone.

    2. Compress the video

    Use the TXVideoEditer.generateVideo(int videoCompressed, String videoOutputPath) API to compress the selected video. Four resolutions are supported for compression currently, and compression with customizable bitrate will be supported in the future.

    3. Publish the video

    Publish the generated .mp4 file to Tencent Cloud. The application needs to get the upload signature with a short validity period for file upload as instructed in Signature Distribution.
    TXUGCPublish (in TXUGCPublish.h) is used to publish .mp4 files to VOD so as to meet various video watch needs such as nearby scheduling, instant live streaming and playback, dynamic acceleration, and global connection.

    TXPublishParam * param = [[TXPublishParam alloc] init];
    param.signature = _signature;                                // Enter the upload signature calculated in step 4
    // Video file path generated by shoot, which can be obtained through the `onRecordComplete` callback of `TXVideoRecordListener`
    param.videoPath = _videoPath;  
    // Path of the first-frame video preview image generated by shoot. The value is the file path specified by calling `startRecord`. You can also specify a path and save the `UIImage` obtained in the `onRecordComplete` callback of `TXVideoRecordListener` to the specified path. It can be set to `nil`.
    param.coverPath = _coverPath; 
    TXUGCPublish *_ugcPublish = [[TXUGCPublish alloc] init];
    // Checkpoint restart is used for file release by default
    _ugcPublish.delegate = self;                                 // Set the `TXVideoPublishListener` callback
    [_ugcPublish publishVideo:param];
    

    The release process and result will be returned through the TXVideoPublishListener API (defined in the TXUGCPublishListener.h header file):

    • onPublishProgress is used to return the file release progress, the uploadBytes parameter indicates the number of uploaded bytes, and the totalBytes parameter indicates the total number of bytes that need to be uploaded.
    @optional
    -(void) onPublishProgress:(NSInteger)uploadBytes totalBytes: (NSInteger)totalBytes;
    
    • onPublishComplete is used to return the release result, the errCode and descMsg fields of TXPublishResult indicate the error code and error message respectively, videoURL indicates the VOD address of the short video, coverURL indicates the cloud storage address of the video cover, and videoId indicates the cloud storage ID the video file, with which you can call VOD's server APIs.
    @optional
    -(void) onPublishComplete:(TXPublishResult*)result;
    
    • Release result
      You can check the short video release result against the error code table.

    4. Play back the video

    After the video is successfully uploaded in step 3, the video fileId, playback URL, and cover URL will be returned. You can directly pass in the fileId or playback URL to the VOD player for playback.

    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support