目录

How do I synthesize images into a video?

You can use the AVCodec Kit to synthesize images into a video. The procedure is as follows:

  1. On the ArkTS side, use ImageSource to decode images into PixelMap objects. For details, see Using ImageSource to Decode Images.
  2. Pass the PixelMap object to the native side through the NDK cross-language interaction process.
  3. On the native side, obtain the PixelMap object passed from the ArkTS side through OH_PixelmapNative_ConvertPixelmapNativeFromNapi and save the object to the OH_PixelmapNative variable.
  4. Obtain the pixel information of the PixelMap, including the width, height, line stride, pixel format, and whether HDR is supported, through OH_PixelmapImageInfo_Create and OH_PixelmapNative_GetImageInfo. The get APIs are OH_PixelmapImageInfo_GetWidth, OH_PixelmapImageInfo_GetHeight, and OH_PixelmapImageInfo_GetPixelFormat. For details, see Using Image_NativeModule for PixelMap Operations.
  5. Read the image pixel data by using the OH_PixelmapNative_ReadPixels and write the data to the buffer based on the pixel format of the PixelMap.
  6. Create a VideoEncoder and a media encapsulator Muxer based on the pixel information, and initialize the encoding environment, including encoding parameter setting and callback registration (OnNeedInputBuffer and OnNewOutputBuffer). For details about the development, see Sample: AVCodec Based Video Codec.
  7. Start the encoding input thread and encoding output thread. In the input thread, write the PixelMap data in the buffer into the video encoder by calling OH_VideoEncoder_PushInputBuffer. In the output thread, write the data processed by the video encoder into the media encapsulator by calling OH_AVMuxer_WriteSampleBuffer, so that the encapsulator can encapsulate the video file. Finally, release the encoder buffer by calling OH_VideoEncoder_FreeOutputBuffer and implement rotation.