4.1 录制视频
设置录制时长
//单位:毫秒
PreviewRecorder.getInstance().setRecordTime(CameraParam.DEFAULT_RECORD_TIME);
开始录制
/**
* 开始录制
*
* @param width
* @param height
* @param outPutPath
*/
public void startRecord(int width, int height, String outPutPath) {
PreviewRecorder.getInstance()
.setRecordType(PreviewRecorder.RecordType.Video)
.setOutputPath(outPutPath)
.enableAudio(true)
.setRecordSize(width, height)
.setOnRecordListener(this)
.startRecord();
}
停止录制
/**
* 停止录制
*/
public void stopRecord() {
PreviewRecorder.getInstance().stopRecord();
}
合并视频
PreviewRecorder.getInstance().combineVideo(videoPath, mCombineListener);
// 合成监听器
private VideoCombiner.CombineListener mCombineListener = new VideoCombiner.CombineListener() {
@Override
public void onCombineStart() {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
MultiUtils.showToast(activity, "正在合成视频,请稍候");
}
});
}
@Override
public void onCombineProcessing(final int current, final int sum) {
}
@Override
public void onCombineFinished(final boolean success) {
startEdit();
}
};
4.2 摄像头切换
public void switchCamera() {
PreviewRenderer.getInstance().switchCamera();
}
4.3 视频美颜
//添加磨皮
VideoClipper addBeautySkin = new VideoClipper();
addBeautySkin.setInputVideoPath(videoPath);
String outputPath = MultiUtils.getOutPutVideoPath();
BeautySkinAdjust beautySkinAdjust = new BeautySkinAdjust(beautySkinValue);
addBeautySkin.setBeautySkinAdjust(beautySkinAdjust);
addBeautySkin.setOutputVideoPath(outputPath);
try {
addBeautySkin.clipVideo(0, videoDuration * 1000);
} catch (IOException e) {
e.printStackTrace();
}
addBeautySkin.setOnVideoCutFinishListener(new VideoClipper.OnVideoCutFinishListener() {
@Override
public void onFinish() {
}
});
//添加美白
VideoClipper addBeautyWhite = new VideoClipper();
addBeautyWhite.setInputVideoPath(videoPath);
String outputPath = MultiUtils.getOutPutVideoPath();
BeautyWhiteAdjust beautyWhiteAdjust = new BeautyWhiteAdjust(beautyWhiteValue);
addBeautyWhite.setBeautyWhiteAdjust(beautyWhiteAdjust);
addBeautyWhite.setOutputVideoPath(outputPath);
try {
addBeautyWhite.clipVideo(0, videoDuration * 1000);
} catch (IOException e) {
e.printStackTrace();
}
addBeautyWhite.setOnVideoCutFinishListener(new VideoClipper.OnVideoCutFinishListener() {
@Override
public void onFinish() {
}
});
4.4 视频滤镜
VideoClipper addFilter = new VideoClipper();
addFilter.setInputVideoPath(videoPath);
String outputFilterPath = MultiUtils.getOutPutVideoPath();
addFilter.setFilterType(filterType);
addFilter.setOutputVideoPath(outputFilterPath);
try {
addFilter.clipVideo(0, videoDuration * 1000);
} catch (IOException e) {
e.printStackTrace();
}
addFilter.setOnVideoCutFinishListener(new VideoClipper.OnVideoCutFinishListener() {
@Override
public void onFinish() {
}
});
4.5 视频剪辑
支持剪辑视频画面大小和时长。
2.4.1版本之前
/**
* @param context 上下文
* @param srcVideoPath 输入视频路径
* @param destPath 输出视频路径
* @param startPosition 开始裁剪的位置 单位:毫秒
* @param endPosition 结束裁剪的位置 单位:毫秒
* @param width 输出的视频宽度
* @param height 输出的视频高度
* @param x X轴偏移位置
* @param y y轴偏移位置
* @param listener 处理过程回调
*/
ShortVideoHelper.cutVideo(context, srcVideoPath, destPath, startPosition, endPosition, width, height, x, y, new HandleProcessListener() {
@Override
public void onStart() {
}
@Override
public void onFinish() {
}
@Override
public void onFail(int errorCode, String msg) {
}
});
2.4.1版本之后
/**
* @param context 上下文
* @param srcVideoPath 输入视频路径
* @param destPath 输出视频路径
* @param startPosition 开始裁剪的位置 单位:毫秒
* @param endPosition 结束裁剪的位置 单位:毫秒
* @param videoWidth 输入的视频宽度
* @param videoHeight 输入的视频高度
* @param width 输出的视频宽度
* @param height 输出的视频高度
* @param x X轴偏移位置
* @param y y轴偏移位置
* @param listener 处理过程回调
*/
ShortVideoHelper.cutVideo(activity, videoPath, outPutCutPath, startCutTime, endCutTime,videoWidth,videoHeight, cropWidth, cropHeight, x, y, new HandleProcessListener() {
@Override
public void onStart() {
}
@Override
public void onFinish() {
}
@Override
public void onFail(int errorCode, String msg) {
}
});
4.6 视频变速
/**
* @param context 上下文
* @param srcVideoPath 输入视频路径
* @param destPath 输出视频路径
* @param speed 变速倍率 取值0.5f~2.0f
* @param listener 处理过程回调
*/
ShortVideoHelper.changeVideoSpeed(context, srcVideoPath, destPath, speed, new HandleProcessListener() {
@Override
public void onStart() {
}
@Override
public void onFinish() {
}
@Override
public void onFail(int errorCode, String msg) {
}
});
4.7 添加音乐、贴纸和气泡文字
添加音乐时要先通过MusicSet类进行音乐设置。
MusicSet musicSet = null;
if (isAddMusic) {
musicSet = new MusicSet();
int startMusicPos = startCutMusicPosition / 1000;
//设置音乐路径
musicSet.setMusicPath(bacMusicPath);
//设置音乐开始的位置,单位:秒
musicSet.setStartMusicPos(startMusicPos);
//原声大小 0.0~1.0f
musicSet.setOriginVolume(originVolume);
//音乐大小 0.0~1.0f
musicSet.setMusicVolume(musicVolume);
}
添加贴纸和气泡文字时,通过StickerSet进行贴纸和气泡文字设置。
List<StickerSet> stickerSets = new ArrayList<>();
StickerSet stickerSet = new StickerSet();
//设置贴纸和气泡文字开始显示的时间,单位:秒
stickerSet.setStartShowTime(sticker.getStartShowTime());
//设置贴纸和气泡文字结束显示的时间,单位:秒
stickerSet.setEndShowTime(sticker.getEndShowTime());
//设置X轴偏移位置
stickerSet.setxOffSet(xOffSet);
//设置y轴偏移位置
stickerSet.setyOffSet(yOffSet);
//设置贴纸和气泡文字路径
stickerSet.setStickerPath(stickerPath);
stickerSets.add(stickerSet);
只添加音乐时,传入参数musicSet,stickerSets传入参数null。只添加贴纸和气泡文字时,传入参数stickerSets,musicSet传入参数null。同时添加音乐、贴纸和气泡文字时,传入参数stickerSets和musicSet。
/**
* @param context 上下文
* @param srcVideoPath 输入视频路径
* @param destPath 输出视频路径
* @param musicSet 音乐设置
* @param stickerSets 贴纸和气泡设置
* @param listener 处理监听器
*/
ShortVideoHelper.AddMusicAndSticker(context, srcVideoPath, destPath, musicSet, null, new HandleProcessListener() {
@Override
public void onStart() {
}
@Override
public void onFinish() {
}
@Override
public void onFail(int errorCode, String msg) {
}
});
4.8 为视频添加黑边合成新视频
/**
* @param context 上下文
* @param srcVideoPath 输入视频路径
* @param destPath 输出视频路径
* @param videoWidth 输入视频宽度
* @param videoHeight 输入视频高度
* @param destVideoWidth 输出视频宽度
* @param destVideoHeight 输出视频高度
* @param listener 处理监听器
*/
ShortVideoHelper.changeVideoSize(context, srcVideoPath, destPath, videoWidth, videoHeight, 720, 1280, new HandleProcessListener() {
@Override
public void onStart() {
}
@Override
public void onFinish() {
}
@Override
public void onFail(int errorCode, String msg) {
}
});
4.9 图片合成视频
为图片设置展示时长。
//单位:毫秒
image.setImageTime(int time);
为图片设置动画效果。
//animType=0:无动画效果,animType=1:放大 ,animType=2:缩小,animType=3:左滑,animType=4:右滑
image.setImageAnimType(int animType);
设置转场效果。
//transitionType=0:无转场,transitionType=1:重叠,transitionType=2:闪黑,transitionType=3:闪白,transitionType=4:圆形
image.setTransitionType(int transitionType);
设置转场时长。
//单位:毫秒
image.setTransitionTime(int time);
生成预览视频。
/**
* @param context 上下文
* @param selectedImages 图片集合
*/
ImagesVideo imagesVideo = ImagesToVideoHelper.makeImagesVideo(activity, selectedImages);
//播放预览视频
SurfaceImagesVideoRenderer surfaceImagesVideoRenderer = new TextureImagesVideoRender(ivTextureView);
ImagesVideoPlayer imagesVideoPlayer = new ImagesVideoPlayer(activity);
imagesVideoPlayer.setImagesVideoRenderer(surfaceImagesVideoRenderer);
imagesVideoPlayer.setImagesVideoListener(this);
imagesVideoPlayer.setLoop(true);
imagesVideoPlayer.setDataSource(imagesVideo);
imagesVideoPlayer.prepare();
imagesVideoPlayer.setOnPreparedListener(new ImagesVideoPlayer.OnPreparedListener() {
@Override
public void onPreparing(ImagesVideoPlayer imagesPlayer, float progress) {
}
@Override
public void onPrepared(ImagesVideoPlayer imagesPlayer, int prepared, int total) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
isPrepared = true;
imagesVideoPlayer.start();
}
});
}
@Override
public void onError(ImagesVideoPlayer imagesPlayer) {
}
});
控制播放器暂停、播放、滑动。
//播放
imagesVideoPlayer.start();
//暂停
imagesVideoPlayer.pause();
//滑动
imagesVideoPlayer.seekTo(int pos);
播放界面销毁时,销毁播放器。
@Override
protected void onDestroy() {
super.onDestroy();
if (imagesVideoPlayer != null) {
imagesVideoPlayer.destroy();
}
}
将图片合成视频。
ImagesVideoMaker imagesVideoMaker = new ImagesVideoMaker(activity);
/**
* @param width 视频宽度
* @param height 视频高度
* @param frameRate 帧率
* @param bitRate 比特率
* @param iFrameInterval 关键帧间隔
* @param outputPath 输出路径
*/
imagesVideoMaker.setConfig(int width, int height, int frameRate, int bitRate, int iFrameInterval, String outputPath);
ImagesVideo newImagesVideo = ImagesToVideoHelper.makeImagesVideo(activity, selectedImages);
SurfaceImagesVideoRenderer videoRenderer = new SurfaceImagesVideoRenderer(surfaceImagesVideoRenderer);
videoRenderer.setImagesVideo(newImagesVideo);
imagesVideoMaker.setDataSource(videoRenderer);
imagesVideoMaker.startMake(new ImagesVideoMaker.OnMakeListener() {
@Override
public void onMakeFinish(boolean success) {
}
@Override
public void onMakeProgress(int makedDuration, int totalDuration) {
}
});
4.10 视频特效
视频特效预览,完整代码请参考Demo。
/**
* @param startTime 开始时间(单位毫秒)
* @param endTime 结束时间(单位毫秒)
* @param baseEffect 特效
*/
public EffectPeriod addEffect(long startTime, long endTime, BaseEffect baseEffect) {
EffectPeriod period = new EffectPeriod(startTimeMs, endTimeMs, baseEffect);
effectList.putEffect(period);
return period;
}
//添加抖动特效
addEffect(startTime, endTime, new ShakeEffect(context));
//添加闪屏特效
addEffect(startTime, endTime, new FlashEffect(context));
//添加幻觉特效
addEffect(startTime, endTime, new IllusionEffect(context));
//添加毛刺特效
addEffect(startTime, endTime, new BurrEffect(context));
//添加缩放特效
addEffect(startTime, endTime, new ScaleEffect(context));
//预览特效
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
encoderSurface = new EncoderSurface(new Surface(surfaceTexture));
encoderSurface.makeCurrent();
decoderSurface = new DecoderOutputSurface(new BaseEffect(), effectList);
decoderSurface.setOutputResolution(new Resolution(textureViewWidth, textureViewHeight));
decoderSurface.setInputResolution(new Resolution(textureViewWidth, textureViewHeight));
decoderSurface.setupAll();
runOnUiThread(new Runnable() {
@Override
public void run() {
while (decoderSurface.getSurface() == null) {
try {
Thread.sleep(30);
} catch (Exception ex) {
ex.printStackTrace();
}
}
Surface surface = decoderSurface.getSurface();
mediaPlayer.setSurface(surface);
}
});
poll();
}
});
thread.start();
生成特效视频,完整代码请参考Demo。
//添加抖动特效
effectConfigList.add(new EffectConfig(EffectType.SHAKE, effectPeriod.startTime, effectPeriod.endTime));
//添加闪屏特效
effectConfigList.add(new EffectConfig(EffectType.FLASH, effectPeriod.startTime, effectPeriod.endTime));
//添加幻觉特效
effectConfigList.add(new EffectConfig(EffectType.ILLUSION, effectPeriod.startTime, effectPeriod.endTime));
//添加毛刺特效
effectConfigList.add(new EffectConfig(EffectType.BURR, effectPeriod.startTime, effectPeriod.endTime));
//添加缩放特效
effectConfigList.add(new EffectConfig(EffectType.SCALE, effectPeriod.startTime, effectPeriod.endTime));
//生成视频
private void applyEffect() {
EffectList outPutEffectList = new EffectList();
for (EffectConfig effectConfig: effectConfigList) {
outPutEffectList.putEffect(new EffectPeriod(effectConfig.startTime, effectConfig.endTime,
EffectType.createEffect(effectConfig.effectType, null, this)));
}
CustomProgressDialog handleProgressDialog = new CustomProgressDialog(activity);
handleProgressDialog.show();
//videoPath:原视频路径 outPath:输出视频路径
new Mp4Composer(videoPath, outPath)
.frameRate(30)
.effectList(outPutEffectList)
.listener(
new Mp4Composer.Listener() {
@Override
public void onProgress(double progress) {
}
@Override
public void onCompleted() {
runOnUiThread(new Runnable() {
@Override
public void run() {
handleProgressDialog.dismiss();
}
});
}
@Override
public void onCanceled() {
}
@Override
public void onFailed(Exception exception) {
runOnUiThread(new Runnable() {
@Override
public void run() {
handleProgressDialog.dismiss();
}
});
}
}).start();
}