saveTrimmedVideo method

Future<void> saveTrimmedVideo({
  1. required double startValue,
  2. required double endValue,
  3. required dynamic onSave(
    1. String? path
    ),
  4. OutputType outputType = OutputType.video,
  5. int fpsGIF = 10,
  6. int scaleGIF = 480,
  7. int qualityGIF = 50,
  8. String videoFolderName = 'Trimmer',
  9. String? videoFileName,
  10. StorageDirection? storageDir,
})

Implementation

Future<void> saveTrimmedVideo({
  required double startValue,
  required double endValue,
  required Function(String? path) onSave,
  OutputType outputType = OutputType.video,
  int fpsGIF = 10,
  int scaleGIF = 480,
  int qualityGIF = 50,
  String videoFolderName = 'Trimmer',
  String? videoFileName,
  StorageDirection? storageDir,
}) async {
  if (_currentVideoFile == null) {
    throw Exception('No video loaded');
  }

  final path = _currentVideoFile!.path;
  final baseName = basenameWithoutExtension(path);
  final ext = outputType == OutputType.gif ? '.gif' : extension(path);
  final timestamp = DateFormat('yyyyMMdd_HHmmss').format(DateTime.now());
  final safeFileName = videoFileName ??
      '${baseName}_trimmed_$timestamp'.replaceAll(RegExp(r'\s+'), '_');
  final outputDir = await _getStorageDirectory(videoFolderName, storageDir);
  final outputPath = '$outputDir$safeFileName$ext';

  if (outputType == OutputType.gif) {
    final gifPath = await _generateGif(
      videoPath: path,
      fps: fpsGIF,
      width: scaleGIF,
      quality: qualityGIF,
      start: startValue,
      end: endValue,
      outputPath: outputPath,
    );
    onSave(gifPath);
  } else {
    await _videoTrimmer.loadVideo(path);
    final trimmedPath = await _videoTrimmer.trimVideo(
      startTimeMs: startValue.toInt(),
      endTimeMs: endValue.toInt(),
    );
    if (trimmedPath == null) {
      throw Exception('Video trimming failed.');
    }
    final savedPath = await File(trimmedPath).copy(outputPath);
    onSave(savedPath.path);
  }
}