# Video Processing

Video compression and manipulation.

# Manifest Declaration

You need to declare the use of this API in the manifest's features member:

{"name": "hap.io.Video"}

# Module Import

Before using this service in a component, you need to import the module in the script section of the UX document.

import Video from '@hap.io.Video' 

Or

let Video = require("@hap.io.Video")

# Methods

You can create video compression tasks, invoking the constructor.

Once you have created the task instance, you can start the compression and abort it using the following methods on the Video instance:

This service has the following methods to access information about the videos:

# Constructor({uri,height,width,bitrate,framerate})

Note

You can only create one compression task for each object simultaneously.

# Arguments

This constructor requires an object with the following attributes:

  • uri (string). Mandatory attribute with the URI of the original video file. The URI is could be obtained from media.takeVideo or media.pickVideo.
  • height (number). Optional attribute with the height after compression, in pixels (by default will use the height of the original video).
  • width (number). Video width after compression, in pixels (by default will use the width of the original video).
  • bitrate (number). Optional attribute with bit rate of the compressed video, in bit/s. The default value is half of the original video bit rate. Restricted by the hardware platform, the actual bit rate after compression may not be the same as the configured bit rate.
  • framerate (number). Optional attribute with the drame rate of the compressed video, in fps. The default value is the original video frame rate. If the original video frame rate cannot be obtained, the default value 30 is used.

Example:

var mVideoTask = new Video({ 
  // For details about supported URI types, please refer to https://developer.huawei.com/consumer/en/doc/development/quickApp-References/quickapp-filestructure. 
  // The URI is obtained from media.takeVideo or media.pickVideo. 
  uri:'internal://xxx' 
})

# compressVideo({success,fail,complete})

Method to compress the video.

# Arguments

This method requires an object with the following attributes:

  • success (function(res)). Optional callback function corresponding to the successful execution. The argument of the callback function is an object with the following members:
    • uri (string) with the URI of the compressed video file.
    • name (string) with the video file name.
    • size (number) with the video file size, in bytes.
  • fail (function(msg,code)). Optional callback function corresponding to a failed execution. The possible errors of the callback function are:
    • 200: API exception.
    • 202: Invalid parameter.
    • 300: I/O error.
    • 1001: Invalid original video.
    • 205: The compression task has been created.
    • 203: Instance not found.
  • complete (function). Optional callback function corresponding to the end of the execution.

Example:

mVideoTask.compressVideo({ 
  success: function (res) { 
    console.log(`success: ${res.uri}`) 
  }, 
  fail: function (data, code) { 
    console.log(`fail code:${code}, msg:${data}`) 
  }, 
})

# abort()

Method to cancel and interrupt a compression task.

Example:

mVideoTask.abort() 
mVideoTask = null

# getVideoInfo({uri,success,fail,complete})

Method to get information about a video.

# Arguments

This method requires an object with the following attributes:

  • uri (string). Mandatory attribute with the URI of the video file. The value can be obtained from media.takeVideo or media.pickVideo, or generated by compressVideo().
  • success (function(res)). Optional callback function corresponding to the successful execution. The argument of the callback function is an object with the following members:
    • uri (string) with the URI of video file.
    • name (string) with the video file name.
    • size (number) with the video file size, in bytes.
    • height (number) with the video height, in pixels.
    • width (number) with the video width, in pixels.
    • bitrate (number) with the video bit rate, in bit/s.
    • framerate (number) with the video frame rate, in fps.
    • duration (number) with the video duration, in seconds.
  • fail (function(msg,code)). Optional callback function corresponding to a failed execution.
  • complete (function). Optional callback function corresponding to the end of the execution.

Example:

media.takeVideo({
    success: function (data) {
        Video.getVideoInfo({
            uri: data.uri,
            success: function (videoInfo) {
                const infoVideo = {
                    size: videoInfo.size,
                    width: videoInfo.width,
                    bitrate: videoInfo.bitrate,
                    framerate: videoInfo.framerate,
                    height: videoInfo.height
                }
                console.log(JSON.stringify(infoVideo))
            }
        })
    }
})

# getVideoThumbnail({uri,success,fail,complete})

Method to extract a thumbnail from a video.

# Arguments

This method requires an object with the following attributes:

  • uri (string). Mandatory attribute with the URI of the video file.
  • success (function(res)). Optional callback function corresponding to the successful execution. The argument of the callback function is an object with the following member:
    • uri (string) with the URI of thumbnail of the video.
  • fail (function(msg,code)). Optional callback function corresponding to a failed execution.
  • complete (function). Optional callback function corresponding to the end of the execution.

Example:

media.takeVideo({
    success: function (data) {
        Video.getVideoThumbnail({
            uri: data.uri,
            success: function ({ uri }) {
                console.log(`The thumbnail is: ${uri}`)
            }
        })
    }
})

# Events

This service provides the following listeners:

# onProgressUpdate(function({progress,totalBytesSent,totalBytesExpectedToSend}))

Method to listen to conversion progress change events.

# Arguments

This method requires a callback function with an object as argument that has the following member:

  • progress (number). Upload progress, in percentage and updated every second.

Example:

mVideoTask.onprogressupdate = function(res) { 
    console.log(`current progress: ${res.progress}`) 
}