Audio and Video Recording
Introduction
During video calls, live streaming, and online teaching, users often need to record and save videos for subsequent on-demand viewing by other users. ZEGO provides various recording solutions to meet recording needs in different scenarios.
| Recording Solution | Introduction | Applicable Scenarios |
|---|---|---|
| Client-side Local Recording | Record your (local user's) audio and video through the client SDK and save it locally. | Need to record your (local user's) audio and video and save it to a local device (mobile phone, computer, or other terminal devices). Does not support recording audio and video of other users. Calling method: Start recording by calling the client SDK interface. |
| Audio Raw Data Recording | Complete audio recording by obtaining the raw data of audio and saving the raw data locally. |
|
| Cloud Recording | Record through ZEGO cloud services and save the audio and video to your activated cloud storage. |
Calling method: Start recording by calling ZEGO server-side API interface. |
| CDN Recording | Record the live streamed audio and video and save it to ZEGO CDN. |
Calling method: After you enable CDN recording in the ZEGOCLOUD Console, global recording is enabled by default without calling the server-side API interface; if you need selective recording, please call the ZEGO server-side API interface to start recording. |
| Local Server-side Recording | Record through a server deployed by the developer. |
Calling method: Start recording by calling the server interface built by the developer. |
Solution 1: Client-side Local Recording
During a call or live streaming, record and store the audio and video data you (local user) preview to a local device, saving it as an mp4 or other format file. Does not support recording audio and video of other users.
Applicable Scenarios
- Pure local recording: Record local preview without stream publishing.
- Record while live streaming: Record while stream publishing, saving the entire live streaming process to a local file.
- Record short videos during live streaming: During live streaming, you can record a short video segment at any time and save it locally.
- Only record what you (local user) preview.
Supported Formats
- Audio and video: FLV, MP4
- Audio only: AAC
Sample Source Code
Please refer to Download Sample Source Code to get the source code.
For related source code, please check the files in the "/ZegoExpressExample/Examples/Others/RecordCapture" directory.
Prerequisites
Before implementing the recording feature, please ensure:
- You have created a project in the ZEGOCLOUD Console and applied for a valid AppID and AppSign. For details, please refer to Console - Project Information.
- You have integrated ZEGO Express SDK in the project and implemented basic audio and video publishing and playing functions. For details, please refer to Quick Start - Integration and Quick Start - Implementation Flow.
Usage Steps
The calling flow of the recording feature interface is shown in the following figure:
Set recording callback
Developers need to call setDataRecordEventHandler to set the callback for the recording feature to receive recording information during the recording process.
You will receive the set recording callback information only after calling startRecordingCapturedData to start recording.
// Set recording callback
[[ZegoExpressEngine sharedEngine] setDataRecordEventHandler:self];Developers can use this callback onCapturedDataRecordStateUpdate to determine the status of file recording or provide UI prompts on the user interface, etc.
Developers can use this callback onCapturedDataRecordProgressUpdate to determine the progress of file recording or provide UI prompts on the user interface, etc.
// Developers can handle logic for recording process status changes based on error codes or recording status here, such as providing UI prompts on the interface
- (void)onCapturedDataRecordStateUpdate:(ZegoDataRecordState)state errorCode:(int)errorCode config:(ZegoDataRecordConfig *)config channel:(ZegoPublishChannel)channel {
NSLog(@" Record state update, state: %d, errorCode: %d, file path: %@, record type: %d", (int)state, errorCode, config.filePath, (int)config.recordType);
}
// Developers can handle logic for recording process progress changes based on recording progress here, such as providing UI prompts on the interface
- (void)onCapturedDataRecordProgressUpdate:(ZegoDataRecordProgress *)progress config:(ZegoDataRecordConfig *)config channel:(ZegoPublishChannel)channel {
NSLog(@" Record progress update, duration: %llu, file size: %llu", progress.duration, progress.currentFileSize);
}Start recording
Developers need to call startRecordingCapturedData to start recording.
- ZegoPublishChannel specifies the media channel for recording. If only publishing one stream or only doing local preview, "ZegoPublishChannelMain" should be used.
- ZegoDataRecordConfig specifies the recording configuration. You can specify the recording path and the recording type ZegoDataRecordType .
If you wish to record audio and video, you should specify it as "ZegoDataRecordTypeDefault" or "ZegoDataRecordTypeAudioAndVideo". If you wish to record audio only, select "ZegoDataRecordTypeOnlyAudio". If you only record pure video, select "ZegoDataRecordTypeOnlyVideo".
- In the recording configuration specified in ZegoDataRecordConfig, the file extension of the file path must end with ".mp4", ".flv", or ".aac" to specify the format of the recording file.
- The recording type ZegoDataRecordType is unrelated to the file extension.
// Interface calls such as login room, start publishing stream, start preview, etc.
...
// This example looks for the Document path
NSArray *docsPathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *docsPath = [docsPathArray objectAtIndex:0];
// Create recording configuration
ZegoDataRecordConfig *config = [[ZegoDataRecordConfig alloc] init];
// Set recording file path, specify mp4 format
config.filePath = [docsPath stringByAppendingPathComponent:@"ZGRecordCapture.mp4"];
// Set to record audio and video
config.recordType = ZegoDataRecordTypeAudioAndVideo;
// Start recording, use main channel for recording
[[ZegoExpressEngine sharedEngine] startRecordingCapturedData:config channel:ZegoPublishChannelMain];Stop recording
Developers need to call stopRecordingCapturedData to end recording.
During recording, if you leave the Room logoutRoom, the SDK will actively stop recording internally.
[[ZegoExpressEngine sharedEngine] stopRecordingCapturedData:ZegoPublishChannelMain];Play recording files
For playing recording files, there are two optional methods.
-
Call the system's default player for playback.
-
Call the ZegoMediaPlayer component in ZegoExpressEngine for playback. For details, please refer to Media Player.
FAQ
- What formats of packaged files are supported for local audio and video recording?
Currently audio and video support FLV and MP4, and audio only supports AAC. The format is specified by the file extension. FLV format only supports H.264 encoding, MP4 format supports H.264 and H.265 encoding. If not met, there will be no picture or no audio.
- What are the requirements for storage path?
The storage path can be any file path that the application has permission to read and write. If a directory path is passed, the callback will return file write failure.
- How to get the size of the recording file during the recording process?
The recording duration and recording file size can be obtained in the recording progress callback onCapturedDataRecordProgressUpdate.
- How does the SDK handle passing a file path that already exists?
It will not cause an error, but the original file content will be cleared before writing.
- What should I pay attention to when recording with external capture?
When using external capture, you also need to call the SDK's startPreview method to start before recording.
- Why does the recorded video have green edges?
Users need to pay attention not to modify the video encoding resolution during the recording process. Enable flow control (when enabled, the encoding resolution will be dynamically adjusted according to the network environment).
- Why is there a momentary stutter in the recorded video when stopping stream publishing during the recording process?
This is due to the SDK's internal processing method and currently cannot be resolved. Users need to try to avoid this operation as much as possible.
- Does local media recording support recording sounds from "ZegoMediaPlayer/ZegoAudioPlayer/Mixing functionality"?
You can record sounds from the above input sources, but only sounds mixed into the stream publishing will be recorded. If played locally only, they will not be recorded into the file.
- Why can't the file generated by local recording be opened (cannot play normally)?
You need to first confirm whether the end recording interface was called to properly end the recording. If the recording is not properly ended, the generated recording file cannot be opened.
Solution 2: Audio Raw Data Recording
Complete audio recording by obtaining the raw data of audio and saving the raw data locally.
Applicable Scenarios
- Only need to record audio.
- Only need to save locally.
- Need to record your or others' audio.
Usage Steps
Please refer to Get the Raw Audio Data.
Solution 3: Cloud Recording
Cloud recording records audio and video by calling ZEGO server-side API and saves them to your specified CDN. It supports recording audio and video streams of any user in video calls or live streaming, and provides various disaster recovery and security mechanisms.
Applicable Scenarios
Suitable for various scenarios requiring recording functionality, including but not limited to audio and video calls, live streaming, online classrooms, online meetings, etc.
Usage Steps
Please refer to Cloud Recording.
Solution 4: CDN Recording
CDN recording refers to saving content being live streamed through CDN to the corresponding CDN. You need to publish audio and video to CDN for live streaming before you can use this method for recording.
Applicable Scenarios
Developers need to publish audio and video to CDN for live streaming before they can use this method for recording. If developers are only conducting video calls or co-hosting and have not published audio and video to CDN, they cannot use this feature.
After you enable CDN recording in the ZEGOCLOUD Console, global recording is enabled by default without calling the server-side API interface; if you need selective recording, please call the ZEGO server-side API interface to start recording.
Usage Steps
Please refer to Start CDN Recording.
Solution 5: Local Server-side Recording
Developers integrate ZEGO's server-side recording plugin themselves and deploy it on their own servers. Then developers start recording by accessing their own server interfaces. For details, please refer to Local Server-side Recording.
Applicable Scenarios
This solution is suitable if you have your own server-side development team and are capable of deploying dedicated servers for audio and video recording and can handle large-scale concurrency issues.
Usage Steps
Please refer to Local Server-side Recording.
