compressVideo static method
Implementation
static Future<FFVideoCutModel> compressVideo(String videoPath,
{double? startValue, double? endValue}) async {
FFVideoCutModel cutModel = new FFVideoCutModel(
path: videoPath,
);
File oldFile = File(videoPath);
if (await oldFile.exists() == false) return cutModel;
MediaInformation info = await ffprobe.getMediaInformation(videoPath);
double fileDuration = double.parse(info.getMediaProperties()!['duration']);
int size = int.parse(info.getMediaProperties()!['size']);
cutModel.millSecond = fileDuration;
cutModel.size = size;
StreamInformation streamInfo = info.getStreams()![0];
var width = streamInfo.getAllProperties()['width'] ?? 0;
var height = streamInfo.getAllProperties()['height'] ?? 0;
cutModel.width = width;
cutModel.height = height;
if (size <= 1024 * 1024 * 10) {
// 小于10M,不进行压缩
return cutModel;
}
startValue = startValue ?? 0;
endValue = endValue ?? fileDuration;
var cut = ' -ss $startValue -t ${endValue - startValue}';
var svg = '';
if (width > height) {
if (width > 1080) svg = " -vf scale=1080:-1";
// svg = " -s 720*480";
} else {
if (height > 1080) svg = " -vf scale=-1:1080";
// svg = " -s 480*720";
}
var bit = ' -b:v 500k';
// -strict -2 -qscale 0 -intra //重新编码,会导致文件增大
cutModel.path = await getVideoRootDirectory() + '${const Uuid().v4()}.mp4';
var cmd = '-i $videoPath$cut$svg$bit ${cutModel.path}';
print('cmd === $cmd');
try {
await ffmpeg.execute(cmd);
print('压缩成功');
MediaInformation info_new = await ffprobe.getMediaInformation(cutModel.path!);
var oldSize = info.getMediaProperties()!['size'];
var newSize = info_new.getMediaProperties()!['size'];
var compressRadio = double.parse(newSize) / double.parse(oldSize);
cutModel.size = int.parse(newSize.toString());
cutModel.millSecond = endValue! - startValue!;
cutModel.width = info_new.getStreams()![0].getAllProperties()['width'];
cutModel.height = info_new.getStreams()![0].getAllProperties()['height'];
print('压缩前大小:$oldSize\n压缩后大小:$newSize\n压缩比:$compressRadio');
} catch (e) {
// cutModel.path = videoPath;
print('压缩失败');
}
return cutModel;
}