本文转载至:http://blog.csdn.net/mabeijianxi/article/details/63335722()
说明:
本库1.0是在秒拍开源库上做的二次开发,2.0已经重写底层库,从C到Java全面开源,旨在开发简单好用高效的视频录制库。本篇文档只涉及Java层次逻辑,正在业余修炼c语言与JNI相关的东西,如果有幸写第二篇文章,那时将对其做更深入的剖析,如FFmpeg编译、JNI相关代码编写。ps:利用FFmpeg玩转Android视频录制与压缩(二)现已完成
效果图:
功能描述:
利用FFmpeg录制定制化的视频,并可对其定制化的压缩处理。如设置视频尺寸、设置码率、码率模式、帧率、视频质量等级、压缩速度等等,当然这些只是暂时的,后期会继续维护。
项目地址:
https://github.com/mabeijianxi/small-video-record.
原理讲解:
基本过程就是调用系统camera与AudioRecord得到视频和音频的byte回调,然后出入配置好参数FFmpeg,结束后得到目标视频。
1、配置Camera参数:
- 首先我们录制的视频是竖着的,所以需要旋转90°(默认是横屏录制):camera.setDisplayOrientation(90); 然后设置显示控件:camera.setPreviewDisplay(mSurfaceHolder); 帧率设置:这个参数是可传入的,但是每个摄像头所支持的大小是不一样的,所以你传入maxFrameRate我会再校验一遍,如果当前摄像头支持此帧率那么就使用,如果不支持那么就选择个最接近且小于它的,如果你值很小有可能还是找不到,这时就选择最小的一个,具体算法如下:
- List<Integer> rates = mParameters.getSupportedPreviewFrameRates();
- if (rates != null) {
- if (rates.contains(MAX_FRAME_RATE)) {
- mFrameRate = MAX_FRAME_RATE;
- } else {
- boolean findFrame = false;
- Collections.sort(rates);
- for (int i = rates.size() - 1; i >= 0; i–) {
- if (rates.get(i) <= MAX_FRAME_RATE) {
- mFrameRate = rates.get(i);
- findFrame = true;
- break;
- }
- }
- if (!findFrame) {
- mFrameRate = rates.get(0);
- }
- }
- }
- mParameters.setPreviewFrameRate(mFrameRate);
List<Integer> rates = mParameters.getSupportedPreviewFrameRates();
if (rates != null) {
if (rates.contains(MAX_FRAME_RATE)) {
mFrameRate = MAX_FRAME_RATE;
} else {
boolean findFrame = false;
Collections.sort(rates);
for (int i = rates.size() - 1; i >= 0; i--) {
if (rates.get(i) <= MAX_FRAME_RATE) {
mFrameRate = rates.get(i);
findFrame = true;
break;
}
}
if (!findFrame) {
mFrameRate = rates.get(0);
}
}
}
mParameters.setPreviewFrameRate(mFrameRate);
- 摄像头输出尺寸设置:通过系统API mParameters.getSupportedPreviewSizes()可以得到当前摄像头所支持的尺寸,注意这里返回的Size里面其height对应的屏幕短边,width对应的是屏幕长边,也就是说我们也要校验传入的smallVideoWidth是否支持,当然smallVideoHeight不需要校验,因为是小视频,我们到时候说不定还会剪切掉一部分,校验完成即可得到传入的smallVideoWidth所对应的且摄像头所支持的对应高度,把这个宽高设置上即可。常见的smallVideoWidth 有480、720、1080等等。具体如下:
- boolean findWidth = false;
- for (int i = mSupportedPreviewSizes.size() - 1; i >= 0; i–) {
- Size size = mSupportedPreviewSizes.get(i);
- if (size.height == SMALL_VIDEO_WIDTH) {
- mSupportedPreviewWidth = size.width;
- findWidth = true;
- break;
- }
- }
- if (!findWidth) {
- Log.e(getClass().getSimpleName(), ”传入高度不支持或未找到对应宽度,请按照要求重新设置,否则会出现一些严重问题”);
- mSupportedPreviewWidth = 640;
- SMALL_VIDEO_WIDTH = 480;
- SMALL_VIDEO_HEIGHT = 360;
- }
- mParameters.setPreviewSize(mSupportedPreviewWidth, SMALL_VIDEO_WIDTH);
boolean findWidth = false;
for (int i = mSupportedPreviewSizes.size() - 1; i >= 0; i--) {
Size size = mSupportedPreviewSizes.get(i);
if (size.height == SMALL_VIDEO_WIDTH) {
mSupportedPreviewWidth = size.width;
findWidth = true;
break;
}
}
if (!findWidth) {
Log.e(getClass().getSimpleName(), "传入高度不支持或未找到对应宽度,请按照要求重新设置,否则会出现一些严重问题");
mSupportedPreviewWidth = 640;
SMALL_VIDEO_WIDTH = 480;
SMALL_VIDEO_HEIGHT = 360;
}
mParameters.setPreviewSize(mSupportedPreviewWidth, SMALL_VIDEO_WIDTH);
- 设置采样率:常用格式有两种:NV21 / YV12,mParameters.setPreviewFormat(ImageFormat.NV21)
- List<Integer> rates = mParameters.getSupportedPreviewFrameRates();
- if (rates != null) {
- if (rates.contains(MAX_FRAME_RATE)) {
- mFrameRate = MAX_FRAME_RATE;
- } else {
- boolean findFrame = false;
- Collections.sort(rates);
- for (int i = rates.size() - 1; i >= 0; i–) {
- if (rates.get(i) <= MAX_FRAME_RATE) {
- mFrameRate = rates.get(i);
- findFrame = true;
- break;
- }
- }
- if (!findFrame) {
- mFrameRate = rates.get(0);
- }
- }
- }
- mParameters.setPreviewFrameRate(mFrameRate);
List<Integer> rates = mParameters.getSupportedPreviewFrameRates();
if (rates != null) {
if (rates.contains(MAX_FRAME_RATE)) {
mFrameRate = MAX_FRAME_RATE;
} else {
boolean findFrame = false;
Collections.sort(rates);
for (int i = rates.size() - 1; i >= 0; i--) {
if (rates.get(i) <= MAX_FRAME_RATE) {
mFrameRate = rates.get(i);
findFrame = true;
break;
}
}
if (!findFrame) {
mFrameRate = rates.get(0);
}
}
}
mParameters.setPreviewFrameRate(mFrameRate);
- boolean findWidth = false;
- for (int i = mSupportedPreviewSizes.size() - 1; i >= 0; i–) {
- Size size = mSupportedPreviewSizes.get(i);
- if (size.height == SMALL_VIDEO_WIDTH) {
- mSupportedPreviewWidth = size.width;
- findWidth = true;
- break;
- }
- }
- if (!findWidth) {
- Log.e(getClass().getSimpleName(), ”传入高度不支持或未找到对应宽度,请按照要求重新设置,否则会出现一些严重问题”);
- mSupportedPreviewWidth = 640;
- SMALL_VIDEO_WIDTH = 480;
- SMALL_VIDEO_HEIGHT = 360;
- }
- mParameters.setPreviewSize(mSupportedPreviewWidth, SMALL_VIDEO_WIDTH);
boolean findWidth = false; for (int i = mSupportedPreviewSizes.size() - 1; i >= 0; i--) { Size size = mSupportedPreviewSizes.get(i); if (size.height == SMALL_VIDEO_WIDTH) { mSupportedPreviewWidth = size.width; findWidth = true; break; } } if (!findWidth) { Log.e(getClass().getSimpleName(), "传入高度不支持或未找到对应宽度,请按照要求重新设置,否则会出现一些严重问题"); mSupportedPreviewWidth = 640; SMALL_VIDEO_WIDTH = 480; SMALL_VIDEO_HEIGHT = 360; } mParameters.setPreviewSize(mSupportedPreviewWidth, SMALL_VIDEO_WIDTH);
2、接收设备并传入FFmpeg音(音频具体可参考AudioRecorder类)视频数据:
这里首先需要知道几个FFmpeg命令:
- -vf 可以添加滤镜,特别强大,可以旋转缩放剪切等等,我们需要用到旋转和剪切(我一直考虑需不需要用缩放的方式,因为这样可以在预览界面设置高分辨率看着清晰一些)。
transpose,旋转,对应的值有0、1、2、3,0:逆时针旋转90°然后垂直翻转1:顺时针旋转90°,2:逆时针旋转90°,3:顺时针旋转90°然后水平翻转。
剪切,关键字是crop,其有四个参数,分别是宽度、高度、其实剪切位置的X值与Y值,如ffmpeg -i a.mp4 -vf crop=480:360:0:0…;
-vcodec 指定视频编解码器;
-acodec 指定音频编解码器;
vbr 动态码率;
cbr 静态码率;
-crf 视频质量等级0~51,越大质量越差,建议18~28即可,与cbr模式不兼容;
-preset 转码速度,快慢的优劣应该都懂的,可根据自己业务场景设置,具体有:ultrafast、superfast、veryfast、faster、fast、medium、slow、slower、veryslow、placebo;
-i 指定输入;
-x264opts 配置其编解码参数;
maxrate 最大码率;
bitrate 固定码率;
-f 输出格式;
-s 设置帧大小。格式为 ‘wxh’;
-ss 指定开始时间;
-vframes 指定多少帧;
接着皆可在录制前配置我们的录制参数:
- String cmd = String.format("filename = \"%s\"; ", result.mediaPath);
- if (mCameraId == Camera.CameraInfo.CAMERA_FACING_BACK) {
- cmd += String.format("addcmd = %s; ", " -vf \"transpose=1,crop=" + SMALL_VIDEO_WIDTH + ":" + SMALL_VIDEO_HEIGHT + ":0:0\" "
- +getBitrateCrfSize(mediaRecorderConfig,"",true)
- +getBitrateVelocity(mediaRecorderConfig,"",true)
- +getBitrateModeCommand(mediaRecorderConfig,"",true));
- } else {
- cmd += String.format("addcmd = %s; ", " -vf \"transpose=2,crop=" + SMALL_VIDEO_WIDTH + ":" + SMALL_VIDEO_HEIGHT + ":0:0\" "
- +getBitrateCrfSize(mediaRecorderConfig,"",true)
- +getBitrateVelocity(mediaRecorderConfig,"",true)
- +getBitrateModeCommand(mediaRecorderConfig,"",true));
- }
String cmd = String.format("filename = \"%s\"; ", result.mediaPath); if (mCameraId == Camera.CameraInfo.CAMERA_FACING_BACK) { cmd += String.format("addcmd = %s; ", " -vf \"transpose=1,crop=" + SMALL_VIDEO_WIDTH + ":" + SMALL_VIDEO_HEIGHT + ":0:0\" " +getBitrateCrfSize(mediaRecorderConfig,"",true) +getBitrateVelocity(mediaRecorderConfig,"",true) +getBitrateModeCommand(mediaRecorderConfig,"",true)); } else { cmd += String.format("addcmd = %s; ", " -vf \"transpose=2,crop=" + SMALL_VIDEO_WIDTH + ":" + SMALL_VIDEO_HEIGHT + ":0:0\" " +getBitrateCrfSize(mediaRecorderConfig,"",true) +getBitrateVelocity(mediaRecorderConfig,"",true) +getBitrateModeCommand(mediaRecorderConfig,"",true)); }
我们这里设置了旋转滤镜与剪切滤镜,由于我们录制竖屏视频所以旋转90°,然后剪切为我们制定的视频尺寸。当然里面还有三个get函数,分别是视频质量等级、转码速度、码率模式。
- 视频质量等级命令为-crf [size]:
- protected String getBitrateCrfSize(BaseMediaBitrateConfig config, String defualtCmd, boolean nendSymbol) {
- if (TextUtils.isEmpty(defualtCmd)) {
- defualtCmd = ”“;
- }
- String add = ”“;
- if (config != null && config.getMode() == BaseMediaBitrateConfig.MODE.AUTO_VBR && config.getCrfSize() > 0) {
- if (nendSymbol) {
- add = String.format(”-crf \”%d\” ”, config.getCrfSize());
- } else {
- add = String.format(”-crf %d ”, config.getCrfSize());
- }
- } else {
- return defualtCmd;
- }
- return add;
protected String getBitrateCrfSize(BaseMediaBitrateConfig config, String defualtCmd, boolean nendSymbol) { if (TextUtils.isEmpty(defualtCmd)) { defualtCmd = ""; } String add = ""; if (config != null && config.getMode() == BaseMediaBitrateConfig.MODE.AUTO_VBR && config.getCrfSize() > 0) { if (nendSymbol) { add = String.format("-crf \"%d\" ", config.getCrfSize()); } else { add = String.format("-crf %d ", config.getCrfSize()); } } else { return defualtCmd; } return add;
- 转码速度命令为-preset [what]:
- protected String getBitrateVelocity(BaseMediaBitrateConfig config, String defualtCmd, boolean nendSymbol) {
- if (TextUtils.isEmpty(defualtCmd)) {
- defualtCmd = ”“;
- }
- String add = ”“;
- if (config != null && !TextUtils.isEmpty(config.getVelocity())) {
- if (nendSymbol) {
- add = String.format(”-preset \”%s\” ”, config.getVelocity());
- } else {
- add = String.format(”-preset %s ”, config.getVelocity());
- }
- } else {
- return defualtCmd;
- }
- return add;
protected String getBitrateVelocity(BaseMediaBitrateConfig config, String defualtCmd, boolean nendSymbol) { if (TextUtils.isEmpty(defualtCmd)) { defualtCmd = ""; } String add = ""; if (config != null && !TextUtils.isEmpty(config.getVelocity())) { if (nendSymbol) { add = String.format("-preset \"%s\" ", config.getVelocity()); } else { add = String.format("-preset %s ", config.getVelocity()); } } else { return defualtCmd; } return add;
- 码率模式: 码率模式分为vbr与cbr,我在里面加了三个类AutoVBRMode、VBRMode、CBRMode,三者都可传入转码速度。如果不想管那么多那么只需传入无参的AutoVBRMode对象即可,只有AutoVBRMode模式下可以传入视频质量等级值,这个值将最大程度上控制视频质量。VBRMode模式下可以指定最大码率与额定码率。、CBRMode模式下出入一个固定码率即可。
- protected String getBitrateModeCommand(BaseMediaBitrateConfig config, String defualtCmd, boolean needSymbol) {
- String add = ”“;
- if (TextUtils.isEmpty(defualtCmd)) {
- defualtCmd = ”“;
- }
- if (config != null) {
- if (config.getMode() == BaseMediaBitrateConfig.MODE.VBR) {
- if (needSymbol) {
- add = String.format(” -x264opts \”bitrate=%d:vbv-maxrate=%d\” ”, config.getBitrate(), config.getMaxBitrate());
- } else {
- add = String.format(” -x264opts bitrate=%d:vbv-maxrate=%d ”, config.getBitrate(), config.getMaxBitrate());
- }
- return add;
- } else if (mediaRecorderConfig.getMode() == BaseMediaBitrateConfig.MODE.CBR) {
- if (needSymbol) {
- add = String.format(” -x264opts \”bitrate=%d:vbv-bufsize=%d:nal_hrd=cbr\” ”, config.getBitrate(), config.getBufSize());
- } else {
- add = String.format(” -x264opts bitrate=%d:vbv-bufsize=%d:nal_hrd=cbr ”, config.getBitrate(), config.getBufSize());
- }
- return add;
- }
- }
- return defualtCmd;
- }
protected String getBitrateModeCommand(BaseMediaBitrateConfig config, String defualtCmd, boolean needSymbol) { String add = ""; if (TextUtils.isEmpty(defualtCmd)) { defualtCmd = ""; } if (config != null) { if (config.getMode() == BaseMediaBitrateConfig.MODE.VBR) { if (needSymbol) { add = String.format(" -x264opts \"bitrate=%d:vbv-maxrate=%d\" ", config.getBitrate(), config.getMaxBitrate()); } else { add = String.format(" -x264opts bitrate=%d:vbv-maxrate=%d ", config.getBitrate(), config.getMaxBitrate()); } return add; } else if (mediaRecorderConfig.getMode() == BaseMediaBitrateConfig.MODE.CBR) { if (needSymbol) { add = String.format(" -x264opts \"bitrate=%d:vbv-bufsize=%d:nal_hrd=cbr\" ", config.getBitrate(), config.getBufSize()); } else { add = String.format(" -x264opts bitrate=%d:vbv-bufsize=%d:nal_hrd=cbr ", config.getBitrate(), config.getBufSize()); } return add; } } return defualtCmd; }
配置好后即可开始录制,在camera的数据回调里面把数据转入底层。
- @Override
- public void onPreviewFrame(byte[] data, Camera camera) {
- if (mRecording) {
- UtilityAdapter.RenderDataYuv(data);
- }
- super.onPreviewFrame(data, camera);
- }
@Override public void onPreviewFrame(byte[] data, Camera camera) { if (mRecording) { UtilityAdapter.RenderDataYuv(data); } super.onPreviewFrame(data, camera); }
3、多段视频合并
录制过程中我们可以暂停录制,这个可能生成n段短视频,这个我们就需要合并视频了,利用FFmpeg命令也可以轻松实现:
- //合并ts流
- String cmd = String.format("ffmpeg %s -i \"%s\" -vcodec copy -acodec copy -absf aac_adtstoasc -f mp4 -movflags faststart \"%s\" ",
- FFMpegUtils.getLogCommand(),
- mMediaObject.getConcatYUV(),
- mMediaObject.getOutputTempVideoPath());
- boolean mergeFlag = UtilityAdapter.FFmpegRun("", cmd) == 0;
//合并ts流
String cmd = String.format("ffmpeg %s -i \"%s\" -vcodec copy -acodec copy -absf aac_adtstoasc -f mp4 -movflags faststart \"%s\" ",
FFMpegUtils.getLogCommand(),
mMediaObject.getConcatYUV(),
mMediaObject.getOutputTempVideoPath());
boolean mergeFlag = UtilityAdapter.FFmpegRun("", cmd) == 0;
这里视频和音频的编解码器使用原始数据的即可,命令为-vcodec copy -acodec copy这样速度回比较快,-absf表示为匹配的流设置比特流过滤器,当然还有-vbsf,最新的指定方式是-bsf:v
- //合并ts流
- String cmd = String.format("ffmpeg %s -i \"%s\" -vcodec copy -acodec copy -absf aac_adtstoasc -f mp4 -movflags faststart \"%s\" ",
- FFMpegUtils.getLogCommand(),
- mMediaObject.getConcatYUV(),
- mMediaObject.getOutputTempVideoPath());
- boolean mergeFlag = UtilityAdapter.FFmpegRun("", cmd) == 0;
//合并ts流
String cmd = String.format("ffmpeg %s -i \"%s\" -vcodec copy -acodec copy -absf aac_adtstoasc -f mp4 -movflags faststart \"%s\" ",
FFMpegUtils.getLogCommand(),
mMediaObject.getConcatYUV(),
mMediaObject.getOutputTempVideoPath());
boolean mergeFlag = UtilityAdapter.FFmpegRun("", cmd) == 0;
4、进一步转码压缩
如果没有设置 doH264Compress 参数那么将不执行以下逻辑
- String vbr = “ -vbr 4 ”;
- if (compressConfig != null && compressConfig.getMode()==BaseMediaBitrateConfig.MODE.CBR) {
- vbr = ”“;
- }
- String cmd_transcoding = String.format(”ffmpeg -i %s -c:v libx264 %s %s %s -c:a libfdk_aac %s %s”,
- mMediaObject.getOutputTempVideoPath(),
- getBitrateModeCommand(compressConfig,”“,false),
- getBitrateCrfSize(compressConfig, ”-crf 28”, false),
- getBitrateVelocity(compressConfig, ”-preset:v veryfast”, false),
- vbr,
- mMediaObject.getOutputTempTranscodingVideoPath()
- );
- boolean transcodingFlag = UtilityAdapter.FFmpegRun(“”, cmd_transcoding) == 0;
String vbr = " -vbr 4 ";
if (compressConfig != null && compressConfig.getMode()==BaseMediaBitrateConfig.MODE.CBR) {
vbr = "";
}
String cmd_transcoding = String.format("ffmpeg -i %s -c:v libx264 %s %s %s -c:a libfdk_aac %s %s",
mMediaObject.getOutputTempVideoPath(),
getBitrateModeCommand(compressConfig,"",false),
getBitrateCrfSize(compressConfig, "-crf 28", false),
getBitrateVelocity(compressConfig, "-preset:v veryfast", false),
vbr,
mMediaObject.getOutputTempTranscodingVideoPath()
);
boolean transcodingFlag = UtilityAdapter.FFmpegRun("", cmd_transcoding) == 0;
上面我们指定了视频编解码器为libx264,音频编解码器为libfdkaac,然后跟你个性化冲入的doH264Compress 参数进行压缩,结束后我们就得到了压缩好的视频了。
5、截取视频中的一帧作为封面
- public static boolean captureThumbnails(String videoPath, String outputPath, String wh, String ss) {
- if (ss == null)
- ss = "";
- else
- ss = " -ss " + ss;
- String cmd = String.format("ffmpeg -i \"%s\"%s -s %s -vframes 1 \"%s\"", videoPath, ss, wh, outputPath);
- return UtilityAdapter.FFmpegRun("", cmd) == 0;
- }
public static boolean captureThumbnails(String videoPath, String outputPath, String wh, String ss) {
if (ss == null)
ss = "";
else
ss = " -ss " + ss;
String cmd = String.format("ffmpeg -i \"%s\"%s -s %s -vframes 1 \"%s\"", videoPath, ss, wh, outputPath);
return UtilityAdapter.FFmpegRun("", cmd) == 0;
}
- public static boolean captureThumbnails(String videoPath, String outputPath, String wh, String ss) {
- if (ss == null)
- ss = "";
- else
- ss = " -ss " + ss;
- String cmd = String.format("ffmpeg -i \"%s\"%s -s %s -vframes 1 \"%s\"", videoPath, ss, wh, outputPath);
- return UtilityAdapter.FFmpegRun("", cmd) == 0;
- }
public static boolean captureThumbnails(String videoPath, String outputPath, String wh, String ss) {
if (ss == null)
ss = "";
else
ss = " -ss " + ss;
String cmd = String.format("ffmpeg -i \"%s\"%s -s %s -vframes 1 \"%s\"", videoPath, ss, wh, outputPath);
return UtilityAdapter.FFmpegRun("", cmd) == 0;
}
总结
本库的优点是简单便捷,可控性强,后期将继续维护。缺点是FFmpeg优点老,后期会考虑自己编译一份,那时利用FFmpeg玩转Android视频录制与压缩(二)也就出来了,有点开始期待了,有兴趣的同学欢迎到我github上指教https://github.com/mabeijianxi/small-video-record.