音视频录制开发指导
使用场景
音视频录制的主要功能是录制音视频,并根据设置的编码格式、采样率、码率等参数封装输出文件。
接口说明
音视频录制API接口如下,具体的API详见接口文档。
表 1 音视频录制API接口
| 类名 | 接口名 | 功能 |
|---|---|---|
| Recorder | int32_t SetVideoSource(VideoSourceType source, int32_t &sourceId) | 设置录制视频源 |
| Recorder | int32_t SetVideoEncoder(int32_t sourceId, VideoCodecFormat encoder) | 设置录制的视频编码器类型 |
| Recorder | int32_t SetVideoSize(int32_t sourceId, int32_t width, int32_t height) | 设置录制的视频宽和高 |
| Recorder | int32_t SetVideoFrameRate(int32_t sourceId, int32_t frameRate) | 设置要录制的视频帧率 |
| Recorder | int32_t SetVideoEncodingBitRate(int32_t sourceId, int32_t rate) | 设置录制视频编码的码率 |
| Recorder | int32_t SetCaptureRate(int32_t sourceId, double fps) | 设置视频帧的捕获帧率 |
| Recorder | std::shared_ptr |
获取对应输入源的surface |
| Recorder | int32_t SetAudioSource(AudioSourceType source, int32_t &sourceId) | 设置录制音频源 |
| Recorder | int32_t SetAudioEncoder(int32_t sourceId, AudioCodecFormat encoder) | 设置录制的音频编码器类型 |
| Recorder | int32_t SetAudioSampleRate(int32_t sourceId, int32_t rate) | 设置录制的音频采样率 |
| Recorder | int32_t SetAudioChannels(int32_t sourceId, int32_t num) | 设置要录制的音频通道数 |
| Recorder | int32_t SetAudioEncodingBitRate(int32_t sourceId, int32_t bitRate) | 设置录制音频编码的码率 |
| Recorder | int32_t SetMaxDuration(int32_t duration) | 设置录制文件的最大时长 |
| Recorder | int32_t SetOutputFormat(OutputFormatType format) | 设置输出文件格式 |
| Recorder | int32_t SetOutputPath(const string &path); | 设置输出文件保存路径 |
| Recorder | int32_t SetOutputFile(int32_t fd) | 设置输出文件的fd |
| Recorder | int32_t SetNextOutputFile(int32_t fd); | 设置下一个输出文件的fd |
| Recorder | int32_t SetMaxFileSize(int64_t size) | 设置录制会话的最大文件大小 |
| Recorder | int32_t SetRecorderCallback(const std::shared_ptr |
注册录制侦听器回调 |
| Recorder | int32_t Prepare() | 准备录制 |
| Recorder | int32_t Start() | 开始录制 |
| Recorder | int32_t Pause() | 暂停录制 |
| Recorder | int32_t Resume() | 恢复录制 |
| Recorder | int32_t Stop(bool block) | 停止录制 |
| Recorder | int32_t Reset(); | 重置录制 |
| Recorder | int32_t Release() | 释放录制资源 |
| Recorder | int32_t SetFileSplitDuration(FileSplitType type, int64_t timestamp, uint32_t duration) | 设置切分录像 |
| Recorder | int32_t SetParameter(int32_t sourceId, const Format &format) | 设置录制的扩展参数 |
约束与限制
无。
开发步骤
- 创建Recorder实例。
```
- Recorder *recorder = new Recorder(); ```
- 设置Recorder参数,包括设置音视频源信息,音视频编码格式,采样率,码率,视频宽高等信息。
```
- int32_t sampleRate = 48000;
- int32_t channelCount = 1;
- AudioCodecFormat audioFormat = AAC_LC;
- AudioSourceType inputSource = AUDIO_MIC;
- int32_t audioEncodingBitRate = sampleRate;
- VideoSourceType source = VIDEO_SOURCE_SURFACE_ES;
- int32_t frameRate = 30;
- double fps = 30;
- int32_t rate = 4096;
- int32_t sourceId = 0;
- int32_t audioSourceId = 0;
- int32_t width = 1920;
- int32_t height = 1080;
- VideoCodecFormat encoder = H264;
- recorder->SetVideoSource(source, sourceId ); // 设置视频源,获得sourceId
- recorder->SetVideoEncoder(sourceId, encoder); // 设置视频编码格式
- recorder->SetVideoSize(sourceId, width, height); // 设置视频宽高
- recorder->SetVideoFrameRate(sourceId, frameRate); // 设置视频帧率
- recorder->SetVideoEncodingBitRate(sourceId, rate); // 设置视频编码码率
- recorder->SetCaptureRate(sourceId, fps); // 设置视频帧的捕获帧率
- recorder->SetAudioSource(inputSource, audioSourceId); // 设置音频源,获得audioSourceId
- recorder->SetAudioEncoder(audioSourceId, audioFormat); // 设置音频编码格式
- recorder->SetAudioSampleRate(audioSourceId, sampleRate); // 设置音频采样率
- recorder->SetAudioChannels(audioSourceId, channelCount); // 设置音频通道数
- recorder->SetAudioEncodingBitRate(audioSourceId, audioEncodingBitRate); // 设置音频编码码率 ```
- 准备录制,Recorder进行录制前的准备工作。
```
- recorder->Prepare(); // 准备录制 ```
- 开始录制,Recorder会根据设置的音频源和视频源进行录制。
```
- recorder->Start(); // 开始录制 ```
- 结束录制,释放资源。
```
- recorder->Stop(); // 停止录制
- recorder->Release(); // 释放录制资源 ```
