• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Audio and Video Encapsulation
2
3You can call the native APIs provided by the **AVMuxer** module to encapsulate audio and video, that is, to store encoded audio and video data to a file in a certain format.
4
5Currently, the following encapsulation capabilities are supported:
6
7| Encapsulation Format| Video Codec Type       | Audio Codec Type  | Cover Type      |
8| -------- | --------------------- | ---------------- | -------------- |
9| mp4      | MPEG-4, AVC (H.264)| AAC, MPEG (MP3)| jpeg, png, bmp|
10| m4a      | MPEG-4, AVC (H.264)| AAC              | jpeg, png, bmp|
11
12**Usage Scenario**
13
14- Video and audio recording
15
16  After you encode audio and video streams, encapsulate them into files.
17
18- Audio and video editing
19
20  After you edit audio and video, encapsulate them into files.
21
22- Audio and video transcoding
23
24  After you transcode audio and video, encapsulate them into files.
25
26## How to Develop
27
28Read [AVMuxer](../reference/native-apis/_a_v_muxer.md) for the API reference.
29
30> **NOTE**
31>
32> To call the encapsulation APIs to write a local file, request the **ohos.permission.READ_MEDIA** and **ohos.permission.WRITE_MEDIA** permissions by following the instructions provided in [Applying for Permissions](../security/accesstoken-guidelines.md).
33
34### Linking the Dynamic Library in the CMake Script
35``` cmake
36target_link_libraries(sample PUBLIC libnative_media_avmuxer.so)
37```
38
39### How to Develop
40
41The following walks you through how to implement the entire process of audio and video encapsulation. It uses the MP4 format as an example.
42
431. Call **OH_AVMuxer_Create()** to create an **OH_AVMuxer** instance.
44
45   ``` c++
46   // Set the encapsulation format to MP4.
47   OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
48   // Create a File Descriptor (FD) in read/write mode.
49   int32_t fd = open("test.mp4", O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
50   OH_AVMuxer *muxer = OH_AVMuxer_Create(fd, format);
51   ```
52
532. (Optional) Call **OH_AVMuxer_SetRotation()** to set the rotation angle.
54
55   ``` c++
56   // Set the rotation angle when a video image needs to be rotated.
57   OH_AVMuxer_SetRotation(muxer, 0);
58   ```
59
603. Add an audio track.
61
62   **Method 1: Use OH_AVFormat_Create to create the format.**
63
64   ``` c++
65   int audioTrackId = -1;
66   OH_AVFormat *formatAudio = OH_AVFormat_Create();
67   OH_AVFormat_SetStringValue(formatAudio, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC); // Mandatory.
68   OH_AVFormat_SetIntValue(formatAudio, OH_MD_KEY_AUD_SAMPLE_RATE, 44100); // Mandatory.
69   OH_AVFormat_SetIntValue(formatAudio, OH_MD_KEY_AUD_CHANNEL_COUNT, 2); // Mandatory.
70
71   int ret = OH_AVMuxer_AddTrack(muxer, &audioTrackId, formatAudio);
72   if (ret != AV_ERR_OK || audioTrackId < 0) {
73       // Failure to add the audio track.
74   }
75   OH_AVFormat_Destroy (formatAudio); // Destroy the format.
76   ```
77
78   **Method 2: Use OH_AVFormat_CreateAudioFormat to create the format.**
79
80   ``` c++
81   int audioTrackId = -1;
82   OH_AVFormat *formatAudio = OH_AVFormat_CreateAudioFormat(OH_AVCODEC_MIMETYPE_AUDIO_AAC, 44100, 2);
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
914. Add a video track.
92
93   **Method 1: Use OH_AVFormat_Create to create the format.**
94
95   ``` c++
96   int videoTrackId = -1;
97   uint8_t *buffer = ...; // Encoding configuration data. If there is no configuration data, leave the parameter unspecified.
98   size_t size =...; // Length of the encoding configuration data. Set this parameter based on project requirements.
99   OH_AVFormat *formatVideo = OH_AVFormat_Create();
100   OH_AVFormat_SetStringValue(formatVideo, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_VIDEO_MPEG4); // Mandatory.
101   OH_AVFormat_SetIntValue(formatVideo, OH_MD_KEY_WIDTH, 1280); // Mandatory.
102   OH_AVFormat_SetIntValue(formatVideo, OH_MD_KEY_HEIGHT, 720); // Mandatory.
103   OH_AVFormat_SetBuffer(formatVideo, OH_MD_KEY_CODEC_CONFIG, buffer, size); // Optional
104
105   int ret = OH_AVMuxer_AddTrack(muxer, &videoTrackId, formatVideo);
106   if (ret != AV_ERR_OK || videoTrackId < 0) {
107       // Failure to add the video track.
108   }
109   OH_AVFormat_Destroy(formatVideo); // Destroy the format.
110   ```
111
112   **Method 2: Use OH_AVFormat_CreateVideoFormat to create the format.**
113
114   ``` c++
115   int videoTrackId = -1;
116   uint8_t *buffer = ...; // Encoding configuration data. If there is no configuration data, leave the parameter unspecified.
117   size_t size =...; // Length of the encoding configuration data. Set this parameter based on project requirements.
118   OH_AVFormat *formatVideo = OH_AVFormat_CreateVideoFormat(OH_AVCODEC_MIMETYPE_VIDEO_MPEG4, 1280, 720);
119   OH_AVFormat_SetBuffer(formatVideo, OH_MD_KEY_CODEC_CONFIG, buffer, size); // Optional
120
121   int ret = OH_AVMuxer_AddTrack(muxer, &videoTrackId, formatVideo);
122   if (ret != AV_ERR_OK || videoTrackId < 0) {
123       // Failure to add the video track.
124   }
125   OH_AVFormat_Destroy(formatVideo); // Destroy the format.
126   ```
127
1285. Add a cover track.
129
130   **Method 1: Use OH_AVFormat_Create to create the format.**
131
132   ``` c++
133   int coverTrackId = -1;
134   OH_AVFormat *formatCover = OH_AVFormat_Create();
135   OH_AVFormat_SetStringValue(formatCover, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_IMAGE_JPG);
136   OH_AVFormat_SetIntValue(formatCover, OH_MD_KEY_WIDTH, 1280);
137   OH_AVFormat_SetIntValue(formatCover, OH_MD_KEY_HEIGHT, 720);
138
139   int ret = OH_AVMuxer_AddTrack(muxer, &coverTrackId, formatCover);
140   if (ret != AV_ERR_OK || coverTrackId < 0) {
141       // Failure to add the cover track.
142   }
143   OH_AVFormat_Destroy(formatCover); // Destroy the format.
144   ```
145
146   **Method 2: Use OH_AVFormat_CreateVideoFormat to create the format.**
147
148   ``` c++
149   int coverTrackId = -1;
150   OH_AVFormat *formatCover = OH_AVFormat_CreateVideoFormat(OH_AVCODEC_MIMETYPE_IMAGE_JPG, 1280, 720);
151
152   int ret = OH_AVMuxer_AddTrack(muxer, &coverTrackId, formatCover);
153   if (ret != AV_ERR_OK || coverTrackId < 0) {
154       // Failure to add the cover track.
155   }
156   OH_AVFormat_Destroy(formatCover); // Destroy the format.
157   ```
158
1596. Call **OH_AVMuxer_Start()** to start encapsulation.
160
161   ``` c++
162   // Call Start() to write the file header. After this API is called, you cannot set media parameters or add tracks.
163   if (OH_AVMuxer_Start(muxer) != AV_ERR_OK) {
164       // Exception handling.
165   }
166   ```
167
1687. Call **OH_AVMuxer_WriteSample()** to write data, including video, audio, and cover data.
169
170   ``` c++
171   // Data can be written only after Start() is called.
172   int size = ...;
173   OH_AVMemory *sample = OH_AVMemory_Create (size); // Create an AVMemory instance.
174   // Write data to the sample buffer. For details, see the usage of OH_AVMemory.
175   // Encapsulate the cover. One image must be written at a time.
176
177   // Set buffer information.
178   OH_AVCodecBufferAttr info;
179   info.pts =...; // Playback start time of the current data, in microseconds.
180   info.size = size; // Length of the current data.
181   info.offset = 0; // Offset. Generally, the value is 0.
182   info.flags |= AVCODEC_BUFFER_FLAGS_SYNC_FRAME; // Flag of the current data. For details, see OH_AVCodecBufferFlags.
183   int trackId = audioTrackId; // Select the track to be written.
184
185   int ret = OH_AVMuxer_WriteSample(muxer, trackId, sample, info);
186   if (ret != AV_ERR_OK) {
187       // Exception handling.
188   }
189   ```
190
1918. Call **OH_AVMuxer_Stop()** to stop encapsulation.
192
193   ``` c++
194   // Call Stop() to write the file trailer. After this API is called, you cannot write media data.
195   if (OH_AVMuxer_Stop(muxer) != AV_ERR_OK) {
196       // Exception handling.
197   }
198   ```
199
2009. Call **OH_AVMuxer_Destroy()** to release the instance.
201
202   ``` c++
203   if (OH_AVMuxer_Destroy(muxer) != AV_ERR_OK) {
204       // Exception handling.
205   }
206   muxer = NULL;
207   close(fd); // Close the FD.
208   ```
209