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
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| fdSrc12+ | AVFileDescriptor | No | No | Source 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+ | number | No | No | Destination 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
| Name | Type | Mandatory | Description |
|---|---|---|---|
| config | AVTranscoderConfig | Yes | Video transcoding parameters to set. |
Return value
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Error codes
For details about the error codes, see Universal Error Codes and Media Error Codes.
| ID | Error Message |
|---|---|
| 401 | The parameter check failed. Return by promise. |
| 5400102 | Operation not allowed. Return by promise. |
| 5400103 | IO error. Return by promise. |
| 5400105 | Service died. Return by promise. |
| 5400106 | Unsupported 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
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Error codes
For details about the error codes, see Media Error Codes.
| ID | Error Message |
|---|---|
| 5400102 | Operation not allowed. Return by promise. |
| 5400103 | IO error. Return by promise. |
| 5400105 | Service 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
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Error codes
For details about the error codes, see Media Error Codes.
| ID | Error Message |
|---|---|
| 5400102 | Operation not allowed. Return by promise. |
| 5400103 | IO error. Return by promise. |
| 5400105 | Service 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
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Error codes
For details about the error codes, see Media Error Codes.
| ID | Error Message |
|---|---|
| 5400102 | Operation not allowed. Return by promise. |
| 5400103 | IO error. Return by promise. |
| 5400105 | Service 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
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Error codes
For details about the error codes, see Media Error Codes.
| ID | Error Message |
|---|---|
| 5400102 | Operation not allowed. Return by promise. |
| 5400103 | IO error. Return by promise. |
| 5400105 | Service 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
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Error codes
For details about the error codes, see Media Error Codes.
| ID | Error Message |
|---|---|
| 5400102 | Operation not allowed. Return by promise. |
| 5400105 | Service 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
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type, which is 'progressUpdate' in this case. This event is triggered by the system during transcoding. |
| callback | Callback<number> | Yes | Callback 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
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type, which is 'progressUpdate' in this case. |
| callback | Callback<number> | No | Called 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
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type, which is 'error' in this case. This event is triggered when an error occurs during recording. |
| callback | ErrorCallback | Yes | Callback invoked when the event is triggered. |
Error codes
For details about the error codes, see Universal Error Codes and Media Error Codes.
| ID | Error Message |
|---|---|
| 401 | The parameter check failed. |
| 801 | Capability not supported. |
| 5400101 | No memory. |
| 5400102 | Operation not allowed. |
| 5400103 | I/O error. |
| 5400104 | Time out. |
| 5400105 | Service died. |
| 5400106 | Unsupported 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
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type, which is 'error' in this case. This event is triggered when an error occurs during transcoding. |
| callback | ErrorCallback | No | Callback 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
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type, which is 'complete' in this case. This event is triggered by the system during transcoding. |
| callback | Callback<void> | Yes | Callback 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
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type, which is 'complete' in this case. |
| callback | Callback<void> | No | Callback 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-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