1e41f4b71Sopenharmony_ci# Media Data Muxing
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciYou 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.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciCurrently, the following muxer capabilities are supported:
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci| Muxing Format| Video Codec Type       | Audio Codec Type  | Cover Type      |
8e41f4b71Sopenharmony_ci| -------- | --------------------- | ---------------- | -------------- |
9e41f4b71Sopenharmony_ci| mp4      | AVC (H.264) <!--RP1--><!--RP1End-->    | AAC, MPEG (MP3)| jpeg, png, bmp|
10e41f4b71Sopenharmony_ci| m4a      | -                     | AAC              | jpeg, png, bmp|
11e41f4b71Sopenharmony_ci| mp3      | -                     | MPEG (MP3)     | -              |
12e41f4b71Sopenharmony_ci| amr      | -                     | AMR (AMR-NB and AMR-WB)| -             |
13e41f4b71Sopenharmony_ci| wav      | -                     | G.711Mu (pcm-mulaw)| -             |
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci> **NOTE**
16e41f4b71Sopenharmony_ci>
17e41f4b71Sopenharmony_ci> - When the container format is MP4 and the audio codec type is MPEG (MP3), the sampling rate must be greater than or equal to 16000 Hz. 
18e41f4b71Sopenharmony_ci> - When the container format is MP4 or M4A and the audio codec type is AAC, the number of audio channels ranges from 1 to 7.
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci<!--RP2--><!--RP2End-->
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci**Usage Scenario**
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci- Video and audio recording
25e41f4b71Sopenharmony_ci  
26e41f4b71Sopenharmony_ci  After you encode audio and video streams, mux them into files.
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci- Audio and video editing
29e41f4b71Sopenharmony_ci  
30e41f4b71Sopenharmony_ci  After you edit audio and video, mux them into files.
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci- Audio and video transcoding
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci  After you transcode audio and video, mux them into files.
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci## How to Develop
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ciRead [AVMuxer](../../reference/apis-avcodec-kit/_a_v_muxer.md) for the API reference.
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci> **NOTE**
41e41f4b71Sopenharmony_ci>
42e41f4b71Sopenharmony_ci> 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).
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci### Linking the Dynamic Library in the CMake Script
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci``` cmake
47e41f4b71Sopenharmony_citarget_link_libraries(sample PUBLIC libnative_media_avmuxer.so)
48e41f4b71Sopenharmony_citarget_link_libraries(sample PUBLIC libnative_media_core.so)
49e41f4b71Sopenharmony_ci```
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci### How to Develop
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ciThe following walks you through how to implement the entire process of audio and video muxing. It uses the MP4 format as an example.
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci1. Add the header files.
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ci   ```c++
58e41f4b71Sopenharmony_ci   #include <multimedia/player_framework/native_avmuxer.h>
59e41f4b71Sopenharmony_ci   #include <multimedia/player_framework/native_avcodec_base.h>
60e41f4b71Sopenharmony_ci   #include <multimedia/player_framework/native_avformat.h>
61e41f4b71Sopenharmony_ci   #include <multimedia/player_framework/native_avbuffer.h>
62e41f4b71Sopenharmony_ci   #include <fcntl.h>
63e41f4b71Sopenharmony_ci   ```
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_ci2. Call **OH_AVMuxer_Create()** to create an **OH_AVMuxer** instance.
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci   ```c++
68e41f4b71Sopenharmony_ci   // Set the muxing format to MP4.
69e41f4b71Sopenharmony_ci   OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
70e41f4b71Sopenharmony_ci   // Create a File Descriptor (FD) in read/write mode.
71e41f4b71Sopenharmony_ci   int32_t fd = open("test.mp4", O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
72e41f4b71Sopenharmony_ci   OH_AVMuxer *muxer = OH_AVMuxer_Create(fd, format);
73e41f4b71Sopenharmony_ci   ```
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci3. (Optional) Call **OH_AVMuxer_SetRotation()** to set the rotation angle.
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci   ```c++
78e41f4b71Sopenharmony_ci   // Set the rotation angle when a video image needs to be rotated.
79e41f4b71Sopenharmony_ci   OH_AVMuxer_SetRotation(muxer, 0);
80e41f4b71Sopenharmony_ci   ```
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci4. Add an audio track.
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci   **Method 1: Use OH_AVFormat_Create to create the format.**
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci   ```c++
87e41f4b71Sopenharmony_ci   int audioTrackId = -1;
88e41f4b71Sopenharmony_ci   uint8_t *buffer = ...; // Encoding configuration data. If there is no configuration data, leave the parameter unspecified.
89e41f4b71Sopenharmony_ci   size_t size =...; // Length of the encoding configuration data. Set this parameter based on project requirements.
90e41f4b71Sopenharmony_ci   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.
91e41f4b71Sopenharmony_ci   OH_AVFormat_SetStringValue(formatAudio, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC); // Mandatory.
92e41f4b71Sopenharmony_ci   OH_AVFormat_SetIntValue(formatAudio, OH_MD_KEY_AUD_SAMPLE_RATE, 44100); // Mandatory.
93e41f4b71Sopenharmony_ci   OH_AVFormat_SetIntValue(formatAudio, OH_MD_KEY_AUD_CHANNEL_COUNT, 2); // Mandatory.
94e41f4b71Sopenharmony_ci   OH_AVFormat_SetIntValue(formatAudio, OH_MD_KEY_PROFILE, AAC_PROFILE_LC); // Optional.
95e41f4b71Sopenharmony_ci   OH_AVFormat_SetBuffer(formatAudio, OH_MD_KEY_CODEC_CONFIG, buffer, size); // Optional.
96e41f4b71Sopenharmony_ci   
97e41f4b71Sopenharmony_ci   int ret = OH_AVMuxer_AddTrack(muxer, &audioTrackId, formatAudio);
98e41f4b71Sopenharmony_ci   if (ret != AV_ERR_OK || audioTrackId < 0) {
99e41f4b71Sopenharmony_ci       // Failure to add the audio track.
100e41f4b71Sopenharmony_ci   }
101e41f4b71Sopenharmony_ci   OH_AVFormat_Destroy (formatAudio); // Destroy the format.
102e41f4b71Sopenharmony_ci   ```
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci   **Method 2: Use OH_AVFormat_CreateAudioFormat to create the format.**
105e41f4b71Sopenharmony_ci
106e41f4b71Sopenharmony_ci   ```c++
107e41f4b71Sopenharmony_ci   int audioTrackId = -1;
108e41f4b71Sopenharmony_ci   uint8_t *buffer = ...; // Encoding configuration data. If there is no configuration data, leave the parameter unspecified.
109e41f4b71Sopenharmony_ci   size_t size =...; // Length of the encoding configuration data. Set this parameter based on project requirements.
110e41f4b71Sopenharmony_ci   OH_AVFormat *formatAudio = OH_AVFormat_CreateAudioFormat(OH_AVCODEC_MIMETYPE_AUDIO_AAC, 44100, 2);
111e41f4b71Sopenharmony_ci   OH_AVFormat_SetIntValue(formatAudio, OH_MD_KEY_PROFILE, AAC_PROFILE_LC); // Optional.
112e41f4b71Sopenharmony_ci   OH_AVFormat_SetBuffer(formatAudio, OH_MD_KEY_CODEC_CONFIG, buffer, size); // Optional.
113e41f4b71Sopenharmony_ci   
114e41f4b71Sopenharmony_ci   int ret = OH_AVMuxer_AddTrack(muxer, &audioTrackId, formatAudio);
115e41f4b71Sopenharmony_ci   if (ret != AV_ERR_OK || audioTrackId < 0) {
116e41f4b71Sopenharmony_ci       // Failure to add the audio track.
117e41f4b71Sopenharmony_ci   }
118e41f4b71Sopenharmony_ci   OH_AVFormat_Destroy (formatAudio); // Destroy the format.
119e41f4b71Sopenharmony_ci   ```
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci5. Add a video track.
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci   **Method 1: Use OH_AVFormat_Create to create the format.**
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ci   ```c++
126e41f4b71Sopenharmony_ci   int videoTrackId = -1;
127e41f4b71Sopenharmony_ci   uint8_t *buffer = ...; // Encoding configuration data. If there is no configuration data, leave the parameter unspecified.
128e41f4b71Sopenharmony_ci   size_t size =...; // Length of the encoding configuration data. Set this parameter based on project requirements.
129e41f4b71Sopenharmony_ci   OH_AVFormat *formatVideo = OH_AVFormat_Create();
130e41f4b71Sopenharmony_ci   OH_AVFormat_SetStringValue(formatVideo, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_VIDEO_AVC); // Mandatory.
131e41f4b71Sopenharmony_ci   OH_AVFormat_SetIntValue(formatVideo, OH_MD_KEY_WIDTH, 1280); // Mandatory.
132e41f4b71Sopenharmony_ci   OH_AVFormat_SetIntValue(formatVideo, OH_MD_KEY_HEIGHT, 720); // Mandatory.
133e41f4b71Sopenharmony_ci   OH_AVFormat_SetBuffer(formatVideo, OH_MD_KEY_CODEC_CONFIG, buffer, size); // Optional
134e41f4b71Sopenharmony_ci   
135e41f4b71Sopenharmony_ci   int ret = OH_AVMuxer_AddTrack(muxer, &videoTrackId, formatVideo);
136e41f4b71Sopenharmony_ci   if (ret != AV_ERR_OK || videoTrackId < 0) {
137e41f4b71Sopenharmony_ci       // Failure to add the video track.
138e41f4b71Sopenharmony_ci   }
139e41f4b71Sopenharmony_ci   OH_AVFormat_Destroy(formatVideo); // Destroy the format.
140e41f4b71Sopenharmony_ci   ```
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci   **Method 2: Use OH_AVFormat_CreateVideoFormat to create the format.**
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci   ```c++
145e41f4b71Sopenharmony_ci   int videoTrackId = -1;
146e41f4b71Sopenharmony_ci   uint8_t *buffer = ...; // Encoding configuration data. If there is no configuration data, leave the parameter unspecified.
147e41f4b71Sopenharmony_ci   size_t size =...; // Length of the encoding configuration data. Set this parameter based on project requirements.
148e41f4b71Sopenharmony_ci   OH_AVFormat *formatVideo = OH_AVFormat_CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, 1280, 720);
149e41f4b71Sopenharmony_ci   OH_AVFormat_SetBuffer(formatVideo, OH_MD_KEY_CODEC_CONFIG, buffer, size); // Optional
150e41f4b71Sopenharmony_ci   
151e41f4b71Sopenharmony_ci   int ret = OH_AVMuxer_AddTrack(muxer, &videoTrackId, formatVideo);
152e41f4b71Sopenharmony_ci   if (ret != AV_ERR_OK || videoTrackId < 0) {
153e41f4b71Sopenharmony_ci       // Failure to add the video track.
154e41f4b71Sopenharmony_ci   }
155e41f4b71Sopenharmony_ci   OH_AVFormat_Destroy(formatVideo); // Destroy the format.
156e41f4b71Sopenharmony_ci   ```
157e41f4b71Sopenharmony_ci
158e41f4b71Sopenharmony_ci6. Add a cover track.
159e41f4b71Sopenharmony_ci
160e41f4b71Sopenharmony_ci   **Method 1: Use OH_AVFormat_Create to create the format.**
161e41f4b71Sopenharmony_ci
162e41f4b71Sopenharmony_ci   ```c++
163e41f4b71Sopenharmony_ci   int coverTrackId = -1;
164e41f4b71Sopenharmony_ci   OH_AVFormat *formatCover = OH_AVFormat_Create();
165e41f4b71Sopenharmony_ci   OH_AVFormat_SetStringValue(formatCover, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_IMAGE_JPG);
166e41f4b71Sopenharmony_ci   OH_AVFormat_SetIntValue(formatCover, OH_MD_KEY_WIDTH, 1280);
167e41f4b71Sopenharmony_ci   OH_AVFormat_SetIntValue(formatCover, OH_MD_KEY_HEIGHT, 720);
168e41f4b71Sopenharmony_ci   
169e41f4b71Sopenharmony_ci   int ret = OH_AVMuxer_AddTrack(muxer, &coverTrackId, formatCover);
170e41f4b71Sopenharmony_ci   if (ret != AV_ERR_OK || coverTrackId < 0) {
171e41f4b71Sopenharmony_ci       // Failure to add the cover track.
172e41f4b71Sopenharmony_ci   }
173e41f4b71Sopenharmony_ci   OH_AVFormat_Destroy(formatCover); // Destroy the format.
174e41f4b71Sopenharmony_ci   ```
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ci   **Method 2: Use OH_AVFormat_CreateVideoFormat to create the format.**
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_ci   ```c++
179e41f4b71Sopenharmony_ci   int coverTrackId = -1;
180e41f4b71Sopenharmony_ci   OH_AVFormat *formatCover = OH_AVFormat_CreateVideoFormat(OH_AVCODEC_MIMETYPE_IMAGE_JPG, 1280, 720);
181e41f4b71Sopenharmony_ci   
182e41f4b71Sopenharmony_ci   int ret = OH_AVMuxer_AddTrack(muxer, &coverTrackId, formatCover);
183e41f4b71Sopenharmony_ci   if (ret != AV_ERR_OK || coverTrackId < 0) {
184e41f4b71Sopenharmony_ci       // Failure to add the cover track.
185e41f4b71Sopenharmony_ci   }
186e41f4b71Sopenharmony_ci   OH_AVFormat_Destroy(formatCover); // Destroy the format.
187e41f4b71Sopenharmony_ci   ```
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ci7. Call **OH_AVMuxer_Start()** to start muxing.
190e41f4b71Sopenharmony_ci
191e41f4b71Sopenharmony_ci   ```c++
192e41f4b71Sopenharmony_ci   // Call Start() to write the file header. After this API is called, you cannot set media parameters or add tracks.
193e41f4b71Sopenharmony_ci   if (OH_AVMuxer_Start(muxer) != AV_ERR_OK) {
194e41f4b71Sopenharmony_ci       // Exception handling.
195e41f4b71Sopenharmony_ci   }
196e41f4b71Sopenharmony_ci   ```
197e41f4b71Sopenharmony_ci
198e41f4b71Sopenharmony_ci8. Call **OH_AVMuxer_WriteSampleBuffer()** to write data. The encapsulated data includes video, audio, and cover data.
199e41f4b71Sopenharmony_ci
200e41f4b71Sopenharmony_ci   ```c++
201e41f4b71Sopenharmony_ci   // Data can be written only after Start() is called.
202e41f4b71Sopenharmony_ci   int size = ...;
203e41f4b71Sopenharmony_ci   OH_AVBuffer *sample = OH_AVBuffer_Create(size); // Create an AVBuffer instance.
204e41f4b71Sopenharmony_ci   // Write data to the sample buffer by using OH_AVBuffer_GetAddr(sample). For details, see the usage of OH_AVBuffer.
205e41f4b71Sopenharmony_ci   // Mux the cover. One image must be written at a time.
206e41f4b71Sopenharmony_ci   
207e41f4b71Sopenharmony_ci   // Set buffer information.
208e41f4b71Sopenharmony_ci   OH_AVCodecBufferAttr info = {0};
209e41f4b71Sopenharmony_ci   info.pts =...; // Playback start time of the current data, in microseconds. The relative time is used.
210e41f4b71Sopenharmony_ci   info.size = size; // Length of the current data.
211e41f4b71Sopenharmony_ci   info.offset = 0; // Offset. Generally, the value is 0.
212e41f4b71Sopenharmony_ci   info.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME; // Flag of the current data. For details, see OH_AVCodecBufferFlags.
213e41f4b71Sopenharmony_ci   info.flags |= AVCODEC_BUFFER_FLAGS_CODEC_DATA; // The AVC in annex-b format contains the codec configuration flag.
214e41f4b71Sopenharmony_ci   OH_AVBuffer_SetBufferAttr(sample, &info); // Set the buffer attribute.
215e41f4b71Sopenharmony_ci   int trackId = audioTrackId; // Select the audio or video track to be written.
216e41f4b71Sopenharmony_ci   
217e41f4b71Sopenharmony_ci   int ret = OH_AVMuxer_WriteSampleBuffer(muxer, trackId, sample);
218e41f4b71Sopenharmony_ci   if (ret != AV_ERR_OK) {
219e41f4b71Sopenharmony_ci       // Exception handling.
220e41f4b71Sopenharmony_ci   }
221e41f4b71Sopenharmony_ci   ```
222e41f4b71Sopenharmony_ci
223e41f4b71Sopenharmony_ci9. Call **OH_AVMuxer_Stop()** to stop muxing.
224e41f4b71Sopenharmony_ci
225e41f4b71Sopenharmony_ci   ```c++
226e41f4b71Sopenharmony_ci   // Call Stop() to write the file trailer. After this API is called, you cannot write media data.
227e41f4b71Sopenharmony_ci   if (OH_AVMuxer_Stop(muxer) != AV_ERR_OK) {
228e41f4b71Sopenharmony_ci       // Exception handling.
229e41f4b71Sopenharmony_ci   }
230e41f4b71Sopenharmony_ci   ```
231e41f4b71Sopenharmony_ci
232e41f4b71Sopenharmony_ci10. Call **OH_AVMuxer_Destroy()** to release the instance. Do not repeatedly destroy the instance. Otherwise, the program may crash.
233e41f4b71Sopenharmony_ci
234e41f4b71Sopenharmony_ci    ```c++
235e41f4b71Sopenharmony_ci    if (OH_AVMuxer_Destroy(muxer) != AV_ERR_OK) {
236e41f4b71Sopenharmony_ci        // Exception handling.
237e41f4b71Sopenharmony_ci    }
238e41f4b71Sopenharmony_ci    muxer = NULL;
239e41f4b71Sopenharmony_ci    close(fd); // Close the FD.
240e41f4b71Sopenharmony_ci    ```
241