openharmony 鸿蒙 arkts-apis-media-AVTranscoder

2026-08-25 浏览 (1)

Interface (AVTranscoder)

AVTranscoder is a transcoding management class. It provides APIs to transcode videos. Before calling any API in AVTranscoder, you must use createAVTranscoder() to create an AVTranscoder instance.

For details about the AVTranscoder demo, see Using AVTranscoder for Transcoding.

NOTE

  • The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
  • The initial APIs of this interface are supported since API version 12.

Modules to Import

import { media } from '@kit.MediaKit';

Properties

Atomic service API: This API can be used in atomic services since API version 22.

System capability: SystemCapability.Multimedia.Media.AVTranscoder

NameTypeRead-OnlyOptionalDescription
fdSrc12+AVFileDescriptorNoNoSource media file descriptor, which specifies the data source.
Example:
There is a media file that stores continuous assets, the address offset is 0, and the byte length is 100. Its file descriptor is AVFileDescriptor { fd = resourceHandle; offset = 0; length = 100; }.
NOTE
- After the resource handle (FD) is transferred to an AVTranscoder instance, do not use the resource handle to perform other read and write operations, including but not limited to transferring this handle to other AVPlayer, AVMetadataExtractor, AVImageGenerator, or AVTranscoder instance.
- Competition occurs when multiple AVTranscoders use the same resource handle to read and write files at the same time, resulting in errors in obtaining data.
fdDst12+numberNoNoDestination media file descriptor, which specifies the data source. After creating an AVTranscoder instance, you must set both fdSrc and fdDst.
NOTE
- After the resource handle (FD) is transferred to an AVTranscoder instance, do not use the resource handle to perform other read and write operations, including but not limited to transferring this handle to other AVPlayer, AVMetadataExtractor, AVImageGenerator, or AVTranscoder instance.
- Competition occurs when multiple AVTranscoders use the same resource handle to read and write files at the same time, resulting in errors in obtaining data.

prepare12+

prepare(config: AVTranscoderConfig): Promise<void>

Sets video transcoding parameters. This API uses a promise to return the result.

Atomic service API: This API can be used in atomic services since API version 22.

System capability: SystemCapability.Multimedia.Media.AVTranscoder

Parameters

NameTypeMandatoryDescription
configAVTranscoderConfigYesVideo transcoding parameters to set.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Media Error Codes.

IDError Message
401The parameter check failed. Return by promise.
5400102Operation not allowed. Return by promise.
5400103IO error. Return by promise.
5400105Service died. Return by promise.
5400106Unsupported format. Returned by promise.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { media } from '@kit.MediaKit';

async function test() {
  // Create an AVTranscoder instance.
  let avTranscoder = await media.createAVTranscoder();
  // Configure the parameters based on those supported by the hardware device.
  let avTranscoderConfig: media.AVTranscoderConfig = {
    audioBitrate : 200000,
    audioCodec : media.CodecMimeType.AUDIO_AAC,
    fileFormat : media.ContainerFormatType.CFT_MPEG_4,
    videoBitrate : 3000000,
    videoCodec : media.CodecMimeType.VIDEO_AVC,
  };

  avTranscoder.prepare(avTranscoderConfig).then(() => {
    console.info('prepare success');
  }).catch((err: BusinessError) => {
    console.error('prepare failed and catch error is ' + err.message);
  });
}

start12+

start(): Promise<void>

Starts video transcoding. This API uses a promise to return the result.

This API can be called only after the prepare() API is called.

Atomic service API: This API can be used in atomic services since API version 22.

System capability: SystemCapability.Multimedia.Media.AVTranscoder

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
5400102Operation not allowed. Return by promise.
5400103IO error. Return by promise.
5400105Service died. Return by promise.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { media } from '@kit.MediaKit';

async function test() {
  // Create an AVTranscoder instance.
  let avTranscoder = await media.createAVTranscoder();
  avTranscoder.start().then(() => {
    console.info('start AVTranscoder success');
  }).catch((err: BusinessError) => {
    console.error('start AVTranscoder failed and catch error is ' + err.message);
  });
}

pause12+

pause(): Promise<void>

Pauses video transcoding. This API uses a promise to return the result.

This API can be called only after the start() API is called. You can call resume() to resume transcoding.

Atomic service API: This API can be used in atomic services since API version 22.

System capability: SystemCapability.Multimedia.Media.AVTranscoder

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
5400102Operation not allowed. Return by promise.
5400103IO error. Return by promise.
5400105Service died. Return by promise.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { media } from '@kit.MediaKit';

async function test() {
  // Create an AVTranscoder instance.
  let avTranscoder = await media.createAVTranscoder();
  avTranscoder.pause().then(() => {
    console.info('pause AVTranscoder success');
  }).catch((err: BusinessError) => {
    console.error('pause AVTranscoder failed and catch error is ' + err.message);
  });
}

resume12+

resume(): Promise<void>

Resumes video transcoding. This API uses a promise to return the result.

This API can be called only after the pause() API is called.

Atomic service API: This API can be used in atomic services since API version 22.

System capability: SystemCapability.Multimedia.Media.AVTranscoder

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
5400102Operation not allowed. Return by promise.
5400103IO error. Return by promise.
5400105Service died. Return by promise.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { media } from '@kit.MediaKit';

async function test() {
  // Create an AVTranscoder instance.
  let avTranscoder = await media.createAVTranscoder();
  avTranscoder.resume().then(() => {
    console.info('resume AVTranscoder success');
  }).catch((err: BusinessError) => {
    console.error('resume AVTranscoder failed and catch error is ' + err.message);
  });
}

cancel12+

cancel(): Promise<void>

Cancels video transcoding. This API uses a promise to return the result.

This API can be called only after the prepare(), start(), pause(), or resume() API is called.

Atomic service API: This API can be used in atomic services since API version 22.

System capability: SystemCapability.Multimedia.Media.AVTranscoder

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
5400102Operation not allowed. Return by promise.
5400103IO error. Return by promise.
5400105Service died. Return by promise.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { media } from '@kit.MediaKit';

async function test() {
  // Create an AVTranscoder instance.
  let avTranscoder = await media.createAVTranscoder();
  avTranscoder.cancel().then(() => {
    console.info('cancel AVTranscoder success');
  }).catch((err: BusinessError) => {
    console.error('cancel AVTranscoder failed and catch error is ' + err.message);
  });
}

release12+

release(): Promise<void>

Releases video transcoding resources. This API uses a promise to return the result.

After the resources are released, you can no longer perform any operation on the AVTranscoder instance.

Atomic service API: This API can be used in atomic services since API version 22.

System capability: SystemCapability.Multimedia.Media.AVTranscoder

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Media Error Codes.

IDError Message
5400102Operation not allowed. Return by promise.
5400105Service died. Return by promise.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { media } from '@kit.MediaKit';

async function test() {
  // Create an AVTranscoder instance.
  let avTranscoder = await media.createAVTranscoder();
  avTranscoder.release().then(() => {
    console.info('release AVTranscoder success');
  }).catch((err: BusinessError) => {
    console.error('release AVTranscoder failed and catch error is ' + err.message);
  });
}

on('progressUpdate')12+

on(type:'progressUpdate', callback: Callback<number>):void

Subscribes to transcoding progress updates. An application can subscribe to only one transcoding progress update event. When the application initiates multiple subscriptions to this event, the last subscription is applied. This API uses an asynchronous callback to return the result.

Atomic service API: This API can be used in atomic services since API version 22.

System capability: SystemCapability.Multimedia.Media.AVTranscoder

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'progressUpdate' in this case. This event is triggered by the system during transcoding.
callbackCallback<number>YesCallback used to return the progress update event. The number parameter in the function indicates the current transcoding progress.

Example

import { media } from '@kit.MediaKit';

async function test() {
  // Create an AVTranscoder instance.
  let avTranscoder = await media.createAVTranscoder();
  avTranscoder.on('progressUpdate', (progress: number) => {
    console.info('avTranscoder progressUpdate = ' + progress);
  });
}

off('progressUpdate')12+

off(type:'progressUpdate', callback?: Callback<number>): void

Unsubscribes from transcoding progress updates.

Atomic service API: This API can be used in atomic services since API version 22.

System capability: SystemCapability.Multimedia.Media.AVTranscoder

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'progressUpdate' in this case.
callbackCallback<number>NoCalled that has been registered to listen for progress updates. You are advised to use the default value because only the last registered callback is retained in the current callback mechanism.

Example

import { media } from '@kit.MediaKit';

async function test() {
  // Create an AVTranscoder instance.
  let avTranscoder = await media.createAVTranscoder();
  avTranscoder.off('progressUpdate');
}

on('error')12+

on(type: 'error', callback: ErrorCallback): void

Subscribes to AVTranscoder errors. If this event is reported, call release() to exit the transcoding. This API uses an asynchronous callback to return the result.

An application can subscribe to only one AVTranscoder error event. When the application initiates multiple subscriptions to this event, the last subscription is applied.

Atomic service API: This API can be used in atomic services since API version 22.

System capability: SystemCapability.Multimedia.Media.AVTranscoder

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'error' in this case.
This event is triggered when an error occurs during recording.
callbackErrorCallbackYesCallback invoked when the event is triggered.

Error codes

For details about the error codes, see Universal Error Codes and Media Error Codes.

IDError Message
401The parameter check failed.
801Capability not supported.
5400101No memory.
5400102Operation not allowed.
5400103I/O error.
5400104Time out.
5400105Service died.
5400106Unsupported format.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { media } from '@kit.MediaKit';

async function test() {
  // Create an AVTranscoder instance.
  let avTranscoder = await media.createAVTranscoder();
  avTranscoder.on('error', (err: BusinessError) => {
    console.info('case avTranscoder.on(error) called, errMessage is ' + err.message);
  });
}

off('error')12+

off(type:'error', callback?: ErrorCallback): void

Unsubscribes from AVTranscoder errors. After the unsubscription, your application can no longer receive AVTranscoder errors.

Atomic service API: This API can be used in atomic services since API version 22.

System capability: SystemCapability.Multimedia.Media.AVTranscoder

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'error' in this case.
This event is triggered when an error occurs during transcoding.
callbackErrorCallbackNoCallback that has been registered to listen for AVTranscoder errors.

Example

import { media } from '@kit.MediaKit';

async function test() {
  // Create an AVTranscoder instance.
  let avTranscoder = await media.createAVTranscoder();
  avTranscoder.off('error');
}

on('complete')12+

on(type: 'complete', callback: Callback<void>): void

Subscribes to the event indicating that transcoding is complete. An application can subscribe to only one transcoding progress update event. When the application initiates multiple subscriptions to this event, the last subscription is applied. This API uses an asynchronous callback to return the result.

When this event is reported, the current transcoding operation is complete. You need to call release() to exit the transcoding.

Atomic service API: This API can be used in atomic services since API version 22.

System capability: SystemCapability.Multimedia.Media.AVTranscoder

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'complete' in this case. This event is triggered by the system during transcoding.
callbackCallback<void>YesCallback used to return the event callback method.

Example

import { media } from '@kit.MediaKit';

async function test() {
  let avTranscoder: media.AVTranscoder|undefined = undefined;
  // Create an AVTranscoder instance.
  avTranscoder = await media.createAVTranscoder();
  avTranscoder.on('complete', async () => {
    console.info('avTranscoder complete');
    if (avTranscoder != undefined) {
      // Listen for transcoding completion events.
      // Ensure that avTranscoder.release() has released the AVTranscoder instance before you proceed with forwarding, uploading, or storing the transcoded file.
      await avTranscoder.release();
      avTranscoder = undefined;
    }
  });
}

off('complete')12+

off(type:'complete', callback?: Callback<void>): void

Unsubscribes from the event indicating that transcoding is complete.

Atomic service API: This API can be used in atomic services since API version 22.

System capability: SystemCapability.Multimedia.Media.AVTranscoder

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'complete' in this case.
callbackCallback<void>NoCallback that has been registered to listen for transcoding completion events.

Example

import { media } from '@kit.MediaKit';

async function test() {
  // Create an AVTranscoder instance.
  let avTranscoder = await media.createAVTranscoder();
  avTranscoder.off('complete');
}

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 capi-avrecorder-oh-avrecorder-range

openharmony 鸿蒙 errorcode-media

openharmony 鸿蒙 capi-avplayer

openharmony 鸿蒙 capi-avplayer-base-h

openharmony 鸿蒙 capi-avimage-generator-h

openharmony 鸿蒙 capi-avscreencapture-oh-rect

openharmony 鸿蒙 capi-videoprocessing-videoprocessing-callback

openharmony 鸿蒙 capi-avsinkbase

openharmony 鸿蒙 capi-avmetadataextractor

openharmony 鸿蒙 capi-avscreencapture-oh-multidisplaycapability

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/04OmR1fa