compressImage method
Implementation
Future<XFile> compressImage({int quality = 70, int? minHeight, int? minWidth, int rotate = 0}) async {
final bytes = await readAsBytes();
int height = 0;
int width = 0;
if (minHeight == null || minWidth == null) {
final size = await imageSize();
width = size.width.toInt();
height = size.height.toInt();
} else {
width = minWidth;
height = minHeight;
}
if(kIsWeb){
FFmpeg? ffmpeg;
try {
ffmpeg = createFFmpeg(CreateFFmpegParam(log: true));
ffmpeg.setLogger(_onLogHandler);
ffmpeg.setProgress(_onProgressHandler);
// Check ffmpeg.isLoaded() before ffmpeg.load() if you are reusing the same instance
if (!ffmpeg.isLoaded()) {
await ffmpeg.load();
}
const inputFile = 'input.mp4';
const outputFile = 'output.mp4';
ffmpeg.writeFile(inputFile, bytes);
// Equals to: await ffmpeg.run(['-i', inputFile, '-s', '1920x1080', outputFile]);
await ffmpeg.runCommand('-i $inputFile -s 1920x1080 $outputFile');
final data = ffmpeg.readFile(outputFile);
return XFile.fromData(data);
} finally {
// Do not call exit if you want to reuse same ffmpeg instance
// When you call exit the temporary files are deleted from MEMFS
// If you are working with multiple inputs you can free any of the via: ffmpeg.unlink('my_input.mp4')
ffmpeg?.exit();
}
}else{
final result = await FlutterImageCompress.compressWithList(
bytes,
minHeight: height,
minWidth: width,
quality: quality,
rotate: rotate,
);
return XFile.fromData(result);
}
}