1# Camera Recording (ArkTS) 2 3As another important function of the camera application, video recording is the process of cyclic frame capture. To smooth video recording, you can follow step 4 in [Camera Photographing](camera-shooting.md) to set the resolution, flash, focal length, photo quality, and rotation angle. 4 5## How to Develop 6 7Read [Camera](../reference/apis-camera-kit/js-apis-camera.md) for the API reference. 8 91. Import the media module. The [APIs](../reference/apis-media-kit/js-apis-media.md) provided by this module are used to obtain the surface ID and create a photo output stream. 10 11 ```ts 12 import { BusinessError } from '@ohos.base'; 13 import media from '@ohos.multimedia.media'; 14 ``` 15 162. Create a surface. 17 18 Call **createAVRecorder()** of the media module to create an **AVRecorder** instance, and call [getInputSurface](../reference/apis-media-kit/js-apis-media.md#getinputsurface9) of the instance to obtain the surface ID, which is associated with the video output stream to process the stream data. 19 20 ```ts 21 async function getVideoSurfaceId(aVRecorderConfig: media.AVRecorderConfig): Promise<string | undefined> { // For details about aVRecorderConfig, see the next section. 22 let avRecorder: media.AVRecorder | undefined = undefined; 23 try { 24 avRecorder = await media.createAVRecorder(); 25 } catch (error) { 26 let err = error as BusinessError; 27 console.error(`createAVRecorder call failed. error code: ${err.code}`); 28 } 29 if (avRecorder === undefined) { 30 return undefined; 31 } 32 avRecorder.prepare(aVRecorderConfig, (err: BusinessError) => { 33 if (err == null) { 34 console.info('prepare success'); 35 } else { 36 console.error('prepare failed and error is ' + err.message); 37 } 38 }); 39 let videoSurfaceId = await avRecorder.getInputSurface(); 40 return videoSurfaceId; 41 } 42 ``` 43 443. Create a video output stream. 45 46 Obtain the video output streams supported by the current device from **videoProfiles** in the [CameraOutputCapability](../reference/apis-camera-kit/js-apis-camera.md#cameraoutputcapability) class. Then, define video recording parameters and use [createVideoOutput](../reference/apis-camera-kit/js-apis-camera.md#createvideooutput) to create a video output stream. 47 48 > **NOTE** 49 > 50 > The preview stream and video output stream must have the same aspect ratio of the resolution. For example, the aspect ratio in the code snippet below is 640:480 (which is equal to 4:3), then the aspect ratio of the resolution of the preview stream must also be 4:3. This means that the resolution can be 640:480, 960:720, 1440:1080, or the like. 51 52 ```ts 53 async function getVideoOutput(cameraManager: camera.CameraManager, videoSurfaceId: string, cameraOutputCapability: camera.CameraOutputCapability): Promise<camera.VideoOutput | undefined> { 54 let videoProfilesArray: Array<camera.VideoProfile> = cameraOutputCapability.videoProfiles; 55 if (!videoProfilesArray) { 56 console.error("createOutput videoProfilesArray == null || undefined"); 57 return undefined; 58 } 59 // AVRecorderProfile 60 let aVRecorderProfile: media.AVRecorderProfile = { 61 fileFormat: media.ContainerFormatType.CFT_MPEG_4, // Video file encapsulation format. Only MP4 is supported. 62 videoBitrate: 100000, // Video bit rate. 63 videoCodec: media.CodecMimeType.VIDEO_AVC, // Video file encoding format. AVC is supported. 64 videoFrameWidth: 640, // Video frame width. 65 videoFrameHeight: 480, // Video frame height. 66 videoFrameRate: 30 // Video frame rate. 67 }; 68 // Define video recording parameters. The ratio of the resolution width (videoFrameWidth) to the resolution height (videoFrameHeight) of the video output stream must be the same as that of the preview stream. 69 let aVRecorderConfig: media.AVRecorderConfig = { 70 videoSourceType: media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV, 71 profile: aVRecorderProfile, 72 url: 'fd://35', 73 rotation: 90 // 90° is the default vertical display angle. You can use other values based on project requirements. 74 }; 75 // Create an AVRecorder instance. 76 let avRecorder: media.AVRecorder | undefined = undefined; 77 try { 78 avRecorder = await media.createAVRecorder(); 79 } catch (error) { 80 let err = error as BusinessError; 81 console.error(`createAVRecorder call failed. error code: ${err.code}`); 82 } 83 if (avRecorder === undefined) { 84 return undefined; 85 } 86 // Set video recording parameters. 87 avRecorder.prepare(aVRecorderConfig); 88 // Create a VideoOutput instance. 89 let videoOutput: camera.VideoOutput | undefined = undefined; 90 // The width and height of the videoProfile object passed in by createVideoOutput must be the same as those of aVRecorderProfile. 91 let videoProfile: undefined | camera.VideoProfile = videoProfilesArray.find((profile: camera.VideoProfile) => { 92 return profile.size.width === aVRecorderProfile.videoFrameWidth && profile.size.height === aVRecorderProfile.videoFrameHeight; 93 }); 94 if (!videoProfile) { 95 console.error('videoProfile is not found'); 96 return; 97 } 98 try { 99 videoOutput = cameraManager.createVideoOutput(videoProfile, videoSurfaceId); 100 } catch (error) { 101 let err = error as BusinessError; 102 console.error('Failed to create the videoOutput instance. errorCode = ' + err.code); 103 } 104 return videoOutput; 105 } 106 ``` 107 1084. Start video recording. 109 110 Call [start](../reference/apis-camera-kit/js-apis-camera.md#start-1) of the **VideoOutput** instance to start the video output stream, and then call [start](../reference/apis-media-kit/js-apis-media.md#start9) of the **AVRecorder** instance to start recording. 111 112 ```ts 113 async function startVideo(videoOutput: camera.VideoOutput, avRecorder: media.AVRecorder): Promise<void> { 114 videoOutput.start(async (err: BusinessError) => { 115 if (err) { 116 console.error(`Failed to start the video output ${err.message}`); 117 return; 118 } 119 console.info('Callback invoked to indicate the video output start success.'); 120 }); 121 try { 122 await avRecorder.start(); 123 } catch (error) { 124 let err = error as BusinessError; 125 console.error(`avRecorder start error: ${JSON.stringify(err)}`); 126 } 127 } 128 ``` 129 1305. Stop video recording. 131 132 Call [stop](../reference/apis-media-kit/js-apis-media.md#stop9-3) of the **AVRecorder** instance to stop recording, and then call [stop](../reference/apis-camera-kit/js-apis-camera.md#stop-1) of the **VideoOutput** instance to stop the video output stream. 133 134 ```ts 135 async function stopVideo(videoOutput: camera.VideoOutput, avRecorder: media.AVRecorder): Promise<void> { 136 try { 137 await avRecorder.stop(); 138 } catch (error) { 139 let err = error as BusinessError; 140 console.error(`avRecorder stop error: ${JSON.stringify(err)}`); 141 } 142 videoOutput.stop((err: BusinessError) => { 143 if (err) { 144 console.error(`Failed to stop the video output ${err.message}`); 145 return; 146 } 147 console.info('Callback invoked to indicate the video output stop success.'); 148 }); 149 } 150 ``` 151 152 153## Status Listening 154 155During camera application development, you can listen for the status of the video output stream, including recording start, recording end, and video output errors. 156 157- Register the **'frameStart'** event to listen for recording start events. This event can be registered when a **VideoOutput** instance is created and is triggered when the bottom layer starts exposure for recording for the first time. Video recording starts as long as a result is returned. 158 159 ```ts 160 function onVideoOutputFrameStart(videoOutput: camera.VideoOutput): void { 161 videoOutput.on('frameStart', () => { 162 console.info('Video frame started'); 163 }); 164 } 165 ``` 166 167- Register the **'frameEnd'** event to listen for recording end events. This event can be registered when a **VideoOutput** instance is created and is triggered when the last frame of recording ends. Video recording ends as long as a result is returned. 168 169 ```ts 170 function onVideoOutputFrameEnd(videoOutput: camera.VideoOutput): void { 171 videoOutput.on('frameEnd', () => { 172 console.info('Video frame ended'); 173 }); 174 } 175 ``` 176 177- Register the **'error'** event to listen for video output errors. The callback function returns an error code when an API is incorrectly used. For details about the error code types, see [CameraErrorCode](../reference/apis-camera-kit/js-apis-camera.md#cameraerrorcode). 178 179 ```ts 180 function onVideoOutputError(videoOutput: camera.VideoOutput): void { 181 videoOutput.on('error', (error: BusinessError) => { 182 console.error(`Video output error code: ${error.code}`); 183 }); 184 } 185 ``` 186