exportVideo method
- required void onCompleted(
- File file
- void onError()?,
- String? name,
- String? outDir,
- VideoExportFormat format = VideoExportFormat.mp4,
- double scale = 1.0,
- String? customInstruction,
- void onProgress(
- Statistics,
- double
- VideoExportPreset preset = VideoExportPreset.none,
- bool isFiltersEnabled = true,
Export the video using this edition parameters and return a File
.
The onCompleted
param must be set to return the exported File video.
The onError
function provides the Exception and StackTrace that causes the exportation error.
If the name
is null
, then it uses this video filename.
If the outDir
is null
, then it uses TemporaryDirectory
.
The format
of the video to be exported, by default VideoExportFormat.mp4.
You can export as a GIF file by using VideoExportFormat.gif or with
GifExportFormat() which allows you to control the frame rate of the exported GIF file.
The scale
is scale=width*scale:height*scale
and reduce or increase video size.
The customInstruction
param can be set to add custom commands to the FFmpeg eexecution
(i.e. -an
to mute the generated video), some commands require the GPL package
The onProgress
is called while the video is exporting.
This argument is usually used to update the export progress percentage.
This function return Statistics
from FFmpeg session and the double progress value between 0.0 and 1.0.
The preset
is the compress quality
(Only available on GPL package).
A slower preset will provide better compression (compression is quality per filesize).
More info about presets
Set isFiltersEnabled
to false
if you do not want to apply any changes
Implementation
Future<void> exportVideo({
required void Function(File file) onCompleted,
void Function(Object, StackTrace)? onError,
String? name,
String? outDir,
VideoExportFormat format = VideoExportFormat.mp4,
double scale = 1.0,
String? customInstruction,
void Function(Statistics, double)? onProgress,
VideoExportPreset preset = VideoExportPreset.none,
bool isFiltersEnabled = true,
}) async {
final String videoPath = file.path;
final String outputPath = await _getOutputPath(
filePath: videoPath,
name: name,
outputDirectory: outDir,
format: format,
);
final String filter = _getExportFilters(
videoFormat: format,
scale: scale,
isFiltersEnabled: isFiltersEnabled,
);
final String execute =
// ignore: unnecessary_string_escapes
" -i \'$videoPath\' ${customInstruction ?? ""} $filter ${_getPreset(preset)} $_trimCmd -y \"$outputPath\"";
debugPrint('VideoEditor - run export video command : [$execute]');
// PROGRESS CALLBACKS
FFmpegKit.executeAsync(
execute,
(session) async {
final state =
FFmpegKitConfig.sessionStateToString(await session.getState());
final code = await session.getReturnCode();
if (ReturnCode.isSuccess(code)) {
onCompleted(File(outputPath));
} else {
if (onError != null) {
onError(
Exception(
'FFmpeg process exited with state $state and return code $code.\n${await session.getOutput()}'),
StackTrace.current,
);
}
return;
}
},
null,
onProgress != null
? (stats) {
// Progress value of encoded video
double progressValue =
stats.getTime() / trimmedDuration.inMilliseconds;
onProgress(stats, progressValue.clamp(0.0, 1.0));
}
: null,
);
}