|
美摄SDK For Android
3.6.0
|
假设拍摄的视频是竖着拍摄分辨率1280*720,我们生成720*720的视频。
1)创建timeline
\if IOS
NvsVideoResolution videoEditRes;
videoEditRes.imageWidth = 720;
videoEditRes.imageHeight = 720;
videoEditRes.imagePAR = (NvsRational){1, 1};
NvsRational videoFps = {25, 1};
NvsAudioResolution audioEditRes;
audioEditRes.sampleRate = 48000;
audioEditRes.channelCount = 2;
audioEditRes.sampleFormat = NvsAudSmpFmt_S16;
//创建时间线
m_timeline = [streamingContext createTimeline:&videoEditRes videoFps:&videoFps audioEditRes:&audioEditRes];
\endif
\if ANDROID
NvsVideoResolution videoEditRes = new NvsVideoResolution();
videoEditRes.imageWidth = 720;
videoEditRes.imageHeight = 720;
videoEditRes.imagePAR = new NvsRational(1, 1);
NvsRational videoFps = new NvsRational(25, 1);
NvsAudioResolution audioEditRes = new NvsAudioResolution();
audioEditRes.sampleRate = 48000;
audioEditRes.channelCount = 2;
//创建时间线
m_timeline = streamingContext.createTimeline(videoEditRes, videoFps, audioEditRes);
\endif
2)创建轨道和片段,path是片段的绝对路径。
\if IOS
NvsVideoTrack videoTrack = [m_timeline appendVideoTrack];
NvsVideoClip clip = [videoTrack appendClip:path];
\endif
\if ANDROID
NvsVideoTrack videoTrack = m_timeline.appendVideoTrack();
NvsVideoClip clip = videoTrack.appendClip(path);
\endif
3)放大视频。
\if IOS
[clip setPan:0 andScan:1];
\endif
\if ANDROID
clip.setPanAndScan(0, 1);
\endif
详细设置参见摇摄与扫描(Pan and Scan)
4)生成视频,path是生成视频的路径。
\if IOS
[m_streamingContext compileTimeline:m_timeline startTime:0 endTime:m_timeline.duration outputFilePath:path videoResolutionGrade:COMPILE_VIDEO_RESOLUTION_GRADE_720
videoBitrateGrade:COMPILE_BITRATE_GRADE_HIGH flags:0];
\endif
\if ANDROID
m_streamingContext.compileTimeline(m_timeline, 0, m_timeline.getDuration(), path, NvsStreamingContext.COMPILE_VIDEO_RESOLUTION_GRADE_720
, NvsStreamingContext.COMPILE_BITRATE_GRADE_HIGH, 0);
\endif
1)创建timeline、轨道、片段同问题1。
2)添加美肤特技。
\if IOS
[clip appendBeautyFx];
\endif
\if ANDROID
clip.appendBeautyFx();
\endif
3)生成视频。
1)在创建轨道和片段的时候,添加多个素材创建出来多个片段。
\if IOS
NvsVideoTrack videoTrack = [m_timeline appendVideoTrack];
NvsVideoClip clip1 = [videoTrack appendClip:path1];
NvsVideoClip clip2 = [videoTrack appendClip:path2];
NvsVideoClip clip3 = [videoTrack appendClip:path3];
NvsVideoClip clip4 = [videoTrack appendClip:path4];
NvsVideoClip clip5 = [videoTrack appendClip:path5];
\endif
\if ANDROID
NvsVideoTrack videoTrack = m_timeline.appendVideoTrack();
NvsVideoClip clip1 = videoTrack.appendClip(path1);
NvsVideoClip clip2 = videoTrack.appendClip(path2);
NvsVideoClip clip3 = videoTrack.appendClip(path3);
NvsVideoClip clip4 = videoTrack.appendClip(path4);
NvsVideoClip clip5 = videoTrack.appendClip(path5);
\endif
2)生成视频。
\if IOS
[m_streamingContext compileTimeline:m_timeline startTime:0 endTime:m_timeline.duration outputFilePath:path videoResolutionGrade:COMPILE_VIDEO_RESOLUTION_GRADE_720
videoBitrateGrade:COMPILE_BITRATE_GRADE_HIGH flags:0];
\endif
\if ANDROID
m_streamingContext.compileTimeline(m_timeline, 0, m_timeline.getDuration(), path, NvsStreamingContext.COMPILE_VIDEO_RESOLUTION_GRADE_720
, NvsStreamingContext.COMPILE_BITRATE_GRADE_HIGH, 0));
\endif
即可生成1个文件。
简单的画中画,用两个不同分辨率的素材例如一个横着拍摄的素材和一个竖着拍摄的素材,分别添加到两个轨道上,就可以看到两个素材叠加在一起的效果。另外Transform 2D特技可以实现视频的放大缩小、旋转、给视频加透明度。
\if IOS
NvsVideoTrack videoTrack1 = [m_timeline appendVideoTrack];
NvsVideoTrack videoTrack2 = [m_timeline appendVideoTrack];
NvsVideoClip clip1 = [videoTrack1 appendClip:path1];
NvsVideoClip clip2 = [videoTrack2 appendClip:path2];
\endif
\if ANDROID
NvsVideoTrack track1 = m_timeline.appendVideoTrack();
NvsVideoTrack track2 = m_timeline.appendVideoTrack();
NvsVideoClip clip1 = track1.appendClip(path1);
NvsVideoClip clip2 = track2.appendClip(path2);
\endif
添加水印有两种方式:一是通过贴纸功能来实现的,需要用户发给一张带水印的图片,我们来进行制作。制作完的水印文件是UUID为文件名,.animatedsticker为扩展名的一个文件。有了这个文件,就可以通过我们的API实现添加水印的功能。
\if IOS
NSMutableString *m_stickerId;
NSString *packagePath = [appPath stringByAppendingPathComponent:@"89740AEA-80D6-432A-B6DE-E7F6539C4121.animatedsticker"];
NvsAssetPackageManagerError error = [m_streamingContext.assetPackageManager installAssetPackage:packagePath license:nil type:NvsAssetPackageType_VideoFx sync:YES assetPackageId:m_stickerId];
if (error != NvsAssetPackageManagerError_NoError && error != NvsAssetPackageManagerError_AlreadyInstalled) {
NSLog(@"Failed to install video fx package!");
package1Valid = false;
}
[m_timeline addAnimatedSticker:0 duration:m_timeline.duration animatedStickerPackageId:_stickerPackageId];
\endif
\if ANDROID
StringBuilder m_stickerId = new StringBuilder();
packagePath = "assets:/89740AEA-80D6-432A-B6DE-E7F6539C4121.animatedsticker";
error = m_streamingContext.getAssetPackageManager().installAssetPackage(packagePath, null, NvsAssetPackageManager.ASSET_PACKAGE_TYPE_ANIMATEDSTICKER, true, m_stickerId);
if (error != NvsAssetPackageManager.ASSET_PACKAGE_MANAGER_ERROR_NO_ERROR
&& error != NvsAssetPackageManager.ASSET_PACKAGE_MANAGER_ERROR_ALREADY_INSTALLED) {
Log.e(TAG, "Failed to install sticker package!");
}
m_timeline.addAnimatedSticker(0, m_timeline.getDuration(),m_stickerId.toString());
\endif
二是通过NvsTimeline类里的addWatermark()接口添加水印。
\if IOS
[m_timeline addWatermark:path displayWidth:0 displayHeight:0 opacity:1 position:NvsTimelineWatermarkPosition_TopRight marginX:0 marginY:0];//path是水印文件的路径,可以是为PNG或JPG文件,或者是.caf格式文件
\endif
\if ANDROID
m_TimeLine.addWatermark(path, 0, 0, 1, NvsTimeline.NvsTimelineWatermarkPosition_TopLeft, 0, 0);//path是水印文件的路径,可以是PNG或JPG文件,或者是.caf格式文件
\endif
检查NvsStreamingContext类里的connectCapturePreviewWithLiveWindow()有没有调用或者调用出了问题,亦或是在调用startCapturePreivew()之后,调用了NvsStreamingContext上的stop()。同样,从录制界面到播出界面,播出黑屏,可能是调用playbackTimeline()后,又调用NvsStreamingContext的stop()。还有可能是NvsStreamingContext上的connectTimelineWithLiveWindow方法没有调用或调用出了问题。
NvsClor类成员是Float类型,R,G,B,A取值是0到1,如果所给颜色值是100,100,100,则需要分别除以255。
调用playbackTimeline播出需要预览一段时间,为了避免这个问题,需要先调用seekTimeline到0的位置,再播出,就不会闪黑了。
可能是有些手机播放器不支持自动旋转,会造成视频播放时画面方向不正常,因而导致用户对录制的视频播产生误导。
使用代码混淆时,注意避免对下面的几个类进行混淆,正确避免方式如下:
-keep class com.cdv.** {*;}
-keep class com.meicam.** {*;}
单独使用effectsdk时,需要对下面的类进行混淆,正确避免方式如下:
-keep class com.cdv.effect.** {*;}
-keep class com.meicam.effect.** {*;}
使用H265进行视频拍摄的用法如下:
\if IOS
NSMutableDictionary *config = [[NSMutableDictionary alloc] init];
[config setValue:@"hevc" forKey:NVS_COMPILE_VIDEO_ENCODEC_NAME];
[context startRecording:filePath withFlags:0 withRecordConfigurations:config];
\endif
\if ANDROID
Hashtable<String, Object> config = new Hashtable<>();
config.put(NvsStreamingContext.COMPILE_VIDEO_ENCODER_NAME, "hevc"); //h265方式
context.startRecording(filePath, 0, config);
\endif
使用H265进行视频生成的用法如下:
\if IOS
NSMutableDictionary *config = [[NSMutableDictionary alloc] init];
[config setValue:@"hevc" forKey:NVS_COMPILE_VIDEO_ENCODEC_NAME];//h265方式
context.compileConfigurations = config;//调用compileTimeline API之前设置
[context compileTimeline:timeline startTime:0 endTime:timeline.duration outputFilePath:ouputPath videoResolutionGrade:NvsCompileVideoResolutionGrade720 videoBitrateGrade:NvsCompileBitrateGradeHigh flags:0];
\endif
\if ANDROID
Hashtable<String, Object> config = new Hashtable<>();
config.put(NvsStreamingContext.COMPILE_VIDEO_ENCODER_NAME, "hevc"); //h265方式
context.setCompileConfigurations(config);//调用compileTimeline API之前设置
context.compileTimeline(timeline, startTime, endTime, compileVideoPath, NvsStreamingContext.COMPILE_VIDEO_RESOLUTION_GRADE_720, NvsStreamingContext.COMPILE_BITRATE_GRADE_HIGH,0);
\endif
1.8.17