1# Media Data Muxing 2 3You can call the native APIs provided by the AVMuxer module to mux audio and video streams, that is, to store encoded audio and video data to a file in a certain format. 4 5For details about the supported muxing formats, see [AVCodec Supported Formats](avcodec-support-formats.md#media-data-muxing). 6 7<!--RP2--><!--RP2End--> 8 9**Usage Scenario** 10 11- Video and audio recording 12 13 After you encode audio and video streams, mux them into files. 14 15- Audio and video editing 16 17 After you edit audio and video, mux them into files. 18 19- Audio and video transcoding 20 21 After you transcode audio and video, mux them into files. 22 23## How to Develop 24 25Read [AVMuxer](../../reference/apis-avcodec-kit/_a_v_muxer.md) for the API reference. 26 27> **NOTE** 28> 29> To call the AVMuxer module to write a local file, request the **ohos.permission.READ_MEDIA** and **ohos.permission.WRITE_MEDIA** permissions by following the instructions provided in [Requesting User Authorization](../../security/AccessToken/request-user-authorization.md). 30 31### Linking the Dynamic Library in the CMake Script 32 33``` cmake 34target_link_libraries(sample PUBLIC libnative_media_avmuxer.so) 35target_link_libraries(sample PUBLIC libnative_media_core.so) 36``` 37 38### How to Develop 39 40The following walks you through how to implement the entire process of audio and video muxing. It uses the MP4 format as an example. 41 421. Add the header files. 43 44 ```c++ 45 #include <multimedia/player_framework/native_avmuxer.h> 46 #include <multimedia/player_framework/native_avcodec_base.h> 47 #include <multimedia/player_framework/native_avformat.h> 48 #include <multimedia/player_framework/native_avbuffer.h> 49 #include <fcntl.h> 50 ``` 51 522. Call **OH_AVMuxer_Create()** to create an **OH_AVMuxer** instance. 53 54 ```c++ 55 // Set the muxing format to MP4. 56 OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4; 57 // Create a File Descriptor (FD) in read/write mode. 58 int32_t fd = open("test.mp4", O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR); 59 OH_AVMuxer *muxer = OH_AVMuxer_Create(fd, format); 60 ``` 61 623. (Optional) Call **OH_AVMuxer_SetRotation()** to set the rotation angle. 63 64 ```c++ 65 // Set the rotation angle when a video image needs to be rotated. 66 OH_AVMuxer_SetRotation(muxer, 0); 67 ``` 68 694. Add an audio track. 70 71 **Method 1: Use OH_AVFormat_Create to create the format.** 72 73 ```c++ 74 int audioTrackId = -1; 75 uint8_t *buffer = ...; // Encoding configuration data. If there is no configuration data, leave the parameter unspecified. 76 size_t size =...; // Length of the encoding configuration data. Set this parameter based on project requirements. 77 OH_AVFormat *formatAudio = OH_AVFormat_Create (); // Call OH_AVFormat_Create to create a format. The following showcases how to mux an AAC-LC audio with the sampling rate of 44100 Hz and two audio channels. 78 OH_AVFormat_SetStringValue(formatAudio, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC); // Mandatory. 79 OH_AVFormat_SetIntValue(formatAudio, OH_MD_KEY_AUD_SAMPLE_RATE, 44100); // Mandatory. 80 OH_AVFormat_SetIntValue(formatAudio, OH_MD_KEY_AUD_CHANNEL_COUNT, 2); // Mandatory. 81 OH_AVFormat_SetIntValue(formatAudio, OH_MD_KEY_PROFILE, AAC_PROFILE_LC); // Optional. 82 OH_AVFormat_SetBuffer(formatAudio, OH_MD_KEY_CODEC_CONFIG, buffer, size); // Optional. 83 84 int ret = OH_AVMuxer_AddTrack(muxer, &audioTrackId, formatAudio); 85 if (ret != AV_ERR_OK || audioTrackId < 0) { 86 // Failure to add the audio track. 87 } 88 OH_AVFormat_Destroy (formatAudio); // Destroy the format. 89 ``` 90 91 **Method 2: Use OH_AVFormat_CreateAudioFormat to create the format.** 92 93 ```c++ 94 int audioTrackId = -1; 95 uint8_t *buffer = ...; // Encoding configuration data. If there is no configuration data, leave the parameter unspecified. 96 size_t size =...; // Length of the encoding configuration data. Set this parameter based on project requirements. 97 OH_AVFormat *formatAudio = OH_AVFormat_CreateAudioFormat(OH_AVCODEC_MIMETYPE_AUDIO_AAC, 44100, 2); 98 OH_AVFormat_SetIntValue(formatAudio, OH_MD_KEY_PROFILE, AAC_PROFILE_LC); // Optional. 99 OH_AVFormat_SetBuffer(formatAudio, OH_MD_KEY_CODEC_CONFIG, buffer, size); // Optional. 100 101 int ret = OH_AVMuxer_AddTrack(muxer, &audioTrackId, formatAudio); 102 if (ret != AV_ERR_OK || audioTrackId < 0) { 103 // Failure to add the audio track. 104 } 105 OH_AVFormat_Destroy (formatAudio); // Destroy the format. 106 ``` 107 1085. Add a video track. 109 110 **Method 1: Use OH_AVFormat_Create to create the format.** 111 112 ```c++ 113 int videoTrackId = -1; 114 uint8_t *buffer = ...; // Encoding configuration data. If there is no configuration data, leave the parameter unspecified. 115 size_t size =...; // Length of the encoding configuration data. Set this parameter based on project requirements. 116 OH_AVFormat *formatVideo = OH_AVFormat_Create(); 117 OH_AVFormat_SetStringValue(formatVideo, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_VIDEO_AVC); // Mandatory. 118 OH_AVFormat_SetIntValue(formatVideo, OH_MD_KEY_WIDTH, 1280); // Mandatory. 119 OH_AVFormat_SetIntValue(formatVideo, OH_MD_KEY_HEIGHT, 720); // Mandatory. 120 OH_AVFormat_SetBuffer(formatVideo, OH_MD_KEY_CODEC_CONFIG, buffer, size); // Optional 121 122 int ret = OH_AVMuxer_AddTrack(muxer, &videoTrackId, formatVideo); 123 if (ret != AV_ERR_OK || videoTrackId < 0) { 124 // Failure to add the video track. 125 } 126 OH_AVFormat_Destroy(formatVideo); // Destroy the format. 127 ``` 128 129 **Method 2: Use OH_AVFormat_CreateVideoFormat to create the format.** 130 131 ```c++ 132 int videoTrackId = -1; 133 uint8_t *buffer = ...; // Encoding configuration data. If there is no configuration data, leave the parameter unspecified. 134 size_t size =...; // Length of the encoding configuration data. Set this parameter based on project requirements. 135 OH_AVFormat *formatVideo = OH_AVFormat_CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, 1280, 720); 136 OH_AVFormat_SetBuffer(formatVideo, OH_MD_KEY_CODEC_CONFIG, buffer, size); // Optional 137 138 int ret = OH_AVMuxer_AddTrack(muxer, &videoTrackId, formatVideo); 139 if (ret != AV_ERR_OK || videoTrackId < 0) { 140 // Failure to add the video track. 141 } 142 OH_AVFormat_Destroy(formatVideo); // Destroy the format. 143 ``` 144 1456. Add a cover track. 146 147 **Method 1: Use OH_AVFormat_Create to create the format.** 148 149 ```c++ 150 int coverTrackId = -1; 151 OH_AVFormat *formatCover = OH_AVFormat_Create(); 152 OH_AVFormat_SetStringValue(formatCover, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_IMAGE_JPG); 153 OH_AVFormat_SetIntValue(formatCover, OH_MD_KEY_WIDTH, 1280); 154 OH_AVFormat_SetIntValue(formatCover, OH_MD_KEY_HEIGHT, 720); 155 156 int ret = OH_AVMuxer_AddTrack(muxer, &coverTrackId, formatCover); 157 if (ret != AV_ERR_OK || coverTrackId < 0) { 158 // Failure to add the cover track. 159 } 160 OH_AVFormat_Destroy(formatCover); // Destroy the format. 161 ``` 162 163 **Method 2: Use OH_AVFormat_CreateVideoFormat to create the format.** 164 165 ```c++ 166 int coverTrackId = -1; 167 OH_AVFormat *formatCover = OH_AVFormat_CreateVideoFormat(OH_AVCODEC_MIMETYPE_IMAGE_JPG, 1280, 720); 168 169 int ret = OH_AVMuxer_AddTrack(muxer, &coverTrackId, formatCover); 170 if (ret != AV_ERR_OK || coverTrackId < 0) { 171 // Failure to add the cover track. 172 } 173 OH_AVFormat_Destroy(formatCover); // Destroy the format. 174 ``` 175 1767. Call **OH_AVMuxer_Start()** to start muxing. 177 178 ```c++ 179 // Call Start() to write the file header. After this API is called, you cannot set media parameters or add tracks. 180 if (OH_AVMuxer_Start(muxer) != AV_ERR_OK) { 181 // Exception handling. 182 } 183 ``` 184 1858. Call **OH_AVMuxer_WriteSampleBuffer()** to write data. The encapsulated data includes video, audio, and cover data. 186 187 ```c++ 188 // Data can be written only after Start() is called. 189 int size = ...; 190 OH_AVBuffer *sample = OH_AVBuffer_Create(size); // Create an AVBuffer instance. 191 // Write data to the sample buffer by using OH_AVBuffer_GetAddr(sample). For details, see the usage of OH_AVBuffer. 192 // Mux the cover. One image must be written at a time. 193 194 // Set buffer information. 195 OH_AVCodecBufferAttr info = {0}; 196 info.pts =...; // Playback start time of the current data, in microseconds. The relative time is used. 197 info.size = size; // Length of the current data. 198 info.offset = 0; // Offset. Generally, the value is 0. 199 info.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME; // Flag of the current data. For details, see OH_AVCodecBufferFlags. 200 info.flags |= AVCODEC_BUFFER_FLAGS_CODEC_DATA; // The AVC in annex-b format contains the codec configuration flag. 201 OH_AVBuffer_SetBufferAttr(sample, &info); // Set the buffer attribute. 202 int trackId = audioTrackId; // Select the audio or video track to be written. 203 204 int ret = OH_AVMuxer_WriteSampleBuffer(muxer, trackId, sample); 205 if (ret != AV_ERR_OK) { 206 // Exception handling. 207 } 208 ``` 209 2109. Call **OH_AVMuxer_Stop()** to stop muxing. 211 212 ```c++ 213 // Call Stop() to write the file trailer. After this API is called, you cannot write media data. 214 if (OH_AVMuxer_Stop(muxer) != AV_ERR_OK) { 215 // Exception handling. 216 } 217 ``` 218 21910. Call **OH_AVMuxer_Destroy()** to release the instance. Do not repeatedly destroy the instance. Otherwise, the program may crash. 220 221 ```c++ 222 if (OH_AVMuxer_Destroy(muxer) != AV_ERR_OK) { 223 // Exception handling. 224 } 225 muxer = NULL; 226 close(fd); // Close the FD. 227 ``` 228