• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Media Data Multiplexing
2
3You can call the native APIs provided by the AVMuxer module to multiplex 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 multiplexing formats, see [AVCodec Supported Formats](avcodec-support-formats.md#media-data-multiplexing).
6
7<!--RP2--><!--RP2End-->
8
9**Usage Scenario**
10
11- Video and audio recording
12
13  After you encode audio and video streams, multiplex them into files.
14
15- Audio and video editing
16
17  After you edit audio and video, multiplex them into files.
18
19- Audio and video transcoding
20
21  After you transcode audio and video, multiplex 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 multiplexing. It uses the MP4 format as an example.
41
42For details about the keys to be configured for different container formats, see [AVCodec Supported Formats](avcodec-support-formats.md#media-data-multiplexing).
43
441. Add the header files.
45
46   ```c++
47   #include <multimedia/player_framework/native_avmuxer.h>
48   #include <multimedia/player_framework/native_avcodec_base.h>
49   #include <multimedia/player_framework/native_avformat.h>
50   #include <multimedia/player_framework/native_avbuffer.h>
51   #include <fcntl.h>
52   ```
53
542. Call **OH_AVMuxer_Create()** to create an **OH_AVMuxer** instance.
55
56   ```c++
57   // Set the container format to MP4.
58   OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
59   // Create an FD in read/write mode.
60   int32_t fd = open("test.mp4", O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
61   OH_AVMuxer *muxer = OH_AVMuxer_Create(fd, format);
62   ```
63
643. (Optional) Call **OH_AVMuxer_SetRotation()** to set the rotation angle.
65
66   ```c++
67   // Set the rotation angle when a video image needs to be rotated.
68   OH_AVMuxer_SetRotation(muxer, 0);
69   ```
70
714. Add file-level data.
72   ```c++
73   OH_AVFormat *format = OH_AVFormat_Create(); // Use OH_AVFormat_Create to create a format.
74   OH_AVFormat_SetStringValue(format, OH_MD_KEY_CREATION_TIME, "2024-12-28T00:00:00:000000Z"); // Set the creation time (UTC time in ISO 8601 format).
75   int ret = OH_AVMuxer_SetFormat(muxer, format); // Set format data to the muxer.
76   if (ret != AV_ERR_OK) {
77      // Failed to set the format because no valid key data to be written is found.
78   }
79   OH_AVFormat_Destroy(format); // Destroy the format.
80   ```
81
825. Add an audio track.
83
84   **Method 1: Use OH_AVFormat_Create to create the format.**
85
86   ```c++
87   int audioTrackId = -1;
88   uint8_t *buffer = ...; // Encoding configuration data. If there is no configuration data, leave the parameter unspecified.
89   size_t size = ...;  // Length of the encoding configuration data. Set this parameter based on project requirements.
90   OH_AVFormat *formatAudio = OH_AVFormat_Create(); // Call OH_AVFormat_Create to create a format. The following showcases how to multiplex an AAC-LC audio with the sampling rate of 44100 Hz and two audio channels.
91   OH_AVFormat_SetStringValue(formatAudio, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC); // Mandatory.
92   OH_AVFormat_SetIntValue(formatAudio, OH_MD_KEY_AUD_SAMPLE_RATE, 44100); // Mandatory.
93   OH_AVFormat_SetIntValue(formatAudio, OH_MD_KEY_AUD_CHANNEL_COUNT, 2); // Mandatory.
94   OH_AVFormat_SetIntValue(formatAudio, OH_MD_KEY_PROFILE, AAC_PROFILE_LC); // Optional.
95   OH_AVFormat_SetBuffer(formatAudio, OH_MD_KEY_CODEC_CONFIG, buffer, size); // Optional.
96
97   int ret = OH_AVMuxer_AddTrack(muxer, &audioTrackId, formatAudio);
98   if (ret != AV_ERR_OK || audioTrackId < 0) {
99       // Failure to add the audio track.
100   }
101   OH_AVFormat_Destroy (formatAudio); // Destroy the format.
102   ```
103
104   **Method 2: Use OH_AVFormat_CreateAudioFormat to create the format.**
105
106   ```c++
107   int audioTrackId = -1;
108   uint8_t *buffer = ...; // Encoding configuration data. If there is no configuration data, leave the parameter unspecified.
109   size_t size = ...;  // Length of the encoding configuration data. Set this parameter based on project requirements.
110   OH_AVFormat *formatAudio = OH_AVFormat_CreateAudioFormat(OH_AVCODEC_MIMETYPE_AUDIO_AAC, 44100, 2);
111   OH_AVFormat_SetIntValue(formatAudio, OH_MD_KEY_PROFILE, AAC_PROFILE_LC); // Optional.
112   OH_AVFormat_SetBuffer(formatAudio, OH_MD_KEY_CODEC_CONFIG, buffer, size); // Optional.
113
114   int ret = OH_AVMuxer_AddTrack(muxer, &audioTrackId, formatAudio);
115   if (ret != AV_ERR_OK || audioTrackId < 0) {
116       // Failure to add the audio track.
117   }
118   OH_AVFormat_Destroy (formatAudio); // Destroy the format.
119   ```
120
1216. Add a video track.
122
123   **Method 1: Use OH_AVFormat_Create to create the format.**
124
125   ```c++
126   int videoTrackId = -1;
127   uint8_t *buffer = ...; // Encoding configuration data. If there is no configuration data, leave the parameter unspecified.
128   size_t size = ...;  // Length of the encoding configuration data. Set this parameter based on project requirements.
129   OH_AVFormat *formatVideo = OH_AVFormat_Create();
130   OH_AVFormat_SetStringValue(formatVideo, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_VIDEO_AVC); // Mandatory.
131   OH_AVFormat_SetIntValue(formatVideo, OH_MD_KEY_WIDTH, 1280); // Mandatory.
132   OH_AVFormat_SetIntValue(formatVideo, OH_MD_KEY_HEIGHT, 720); // Mandatory.
133   OH_AVFormat_SetBuffer(formatVideo, OH_MD_KEY_CODEC_CONFIG, buffer, size); // Optional.
134
135   int ret = OH_AVMuxer_AddTrack(muxer, &videoTrackId, formatVideo);
136   if (ret != AV_ERR_OK || videoTrackId < 0) {
137       // Failure to add the video track.
138   }
139   OH_AVFormat_Destroy(formatVideo); // Destroy the format.
140   ```
141
142   **Method 2: Use OH_AVFormat_CreateVideoFormat to create the format.**
143
144   ```c++
145   int videoTrackId = -1;
146   uint8_t *buffer = ...; // Encoding configuration data. If there is no configuration data, leave the parameter unspecified.
147   size_t size = ...;  // Length of the encoding configuration data. Set this parameter based on project requirements.
148   OH_AVFormat *formatVideo = OH_AVFormat_CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_AVC, 1280, 720);
149   OH_AVFormat_SetBuffer(formatVideo, OH_MD_KEY_CODEC_CONFIG, buffer, size); // Optional.
150
151   int ret = OH_AVMuxer_AddTrack(muxer, &videoTrackId, formatVideo);
152   if (ret != AV_ERR_OK || videoTrackId < 0) {
153       // Failure to add the video track.
154   }
155   OH_AVFormat_Destroy(formatVideo); // Destroy the format.
156   ```
157
1587. Add a cover track.
159
160   **Method 1: Use OH_AVFormat_Create to create the format.**
161
162   ```c++
163   int coverTrackId = -1;
164   OH_AVFormat *formatCover = OH_AVFormat_Create();
165   OH_AVFormat_SetStringValue(formatCover, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_IMAGE_JPG);
166   OH_AVFormat_SetIntValue(formatCover, OH_MD_KEY_WIDTH, 1280);
167   OH_AVFormat_SetIntValue(formatCover, OH_MD_KEY_HEIGHT, 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
176   **Method 2: Use OH_AVFormat_CreateVideoFormat to create the format.**
177
178   ```c++
179   int coverTrackId = -1;
180   OH_AVFormat *formatCover = OH_AVFormat_CreateVideoFormat(OH_AVCODEC_MIMETYPE_IMAGE_JPG, 1280, 720);
181
182   int ret = OH_AVMuxer_AddTrack(muxer, &coverTrackId, formatCover);
183   if (ret != AV_ERR_OK || coverTrackId < 0) {
184       // Failure to add the cover track.
185   }
186   OH_AVFormat_Destroy(formatCover); // Destroy the format.
187   ```
188
1898. Call **OH_AVMuxer_Start()** to start multiplexing.
190
191   ```c++
192   // Call Start() to write the file header. After this API is called, you cannot set media parameters or add tracks.
193   if (OH_AVMuxer_Start(muxer) != AV_ERR_OK) {
194       // Handle exceptions.
195   }
196   ```
197
1989. Call **OH_AVMuxer_WriteSampleBuffer()** to write data.
199
200   The encapsulated data includes video, audio, and cover data.
201
202   ```c++
203   // Data can be written only after Start() is called.
204   int size = ...;
205   OH_AVBuffer *sample = OH_AVBuffer_Create(size); // Create an AVBuffer instance.
206   // Write data to the sample buffer by using OH_AVBuffer_GetAddr(sample). For details, see the usage of OH_AVBuffer.
207   // Multiplex the cover. One image must be written at a time.
208
209   // Set buffer information.
210   OH_AVCodecBufferAttr info = {0};
211   info.pts =...; // Playback start time of the current data, in microseconds. The relative time is used.
212   info.size = size; // Length of the current data.
213   info.offset = 0; // Offset. Generally, the value is 0.
214   info.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME; // Flag of the current data. For details, see OH_AVCodecBufferFlags.
215   info.flags |= AVCODEC_BUFFER_FLAGS_CODEC_DATA; // The AVC in annex-b format contains the codec configuration flag.
216   OH_AVBuffer_SetBufferAttr(sample, &info); // Set the buffer attribute.
217   int trackId = audioTrackId; // Select the audio or video track to be written.
218
219   int ret = OH_AVMuxer_WriteSampleBuffer(muxer, trackId, sample);
220   if (ret != AV_ERR_OK) {
221       // Handle exceptions.
222   }
223   ```
224
22510. Call **OH_AVMuxer_Stop()** to stop multiplexing.
226
227    ```c++
228    // Call Stop() to write the file trailer. After this API is called, you cannot write media data.
229    if (OH_AVMuxer_Stop(muxer) != AV_ERR_OK) {
230        // Handle exceptions.
231    }
232    ```
233
23411. Call **OH_AVMuxer_Destroy()** to release the instance.
235
236    Do not repeatedly destroy the instance. Otherwise, the program may crash.
237
238    ```c++
239    if (OH_AVMuxer_Destroy(muxer) != AV_ERR_OK) {
240        // Handle exceptions.
241    }
242    muxer = NULL;
243    close(fd); // Close the FD.
244    ```