saveTrimmedVideo method

Future<void> saveTrimmedVideo()

Implementation

Future<void> saveTrimmedVideo() async {
  playerController.value.pause();
  Timer.periodic(const Duration(milliseconds: 1000), (Timer timer) async{
    timer.cancel();
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (context2){
        return ValueListenableBuilder<int>(
          valueListenable: progressPercentage,
          builder: (context, int percentage, child) {
            if(percentage >= 100){
              Navigator.of(context2).pop();
            }
            return AlertDialog(
              title: const Text('Please wait...'),
              content: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  const CircularProgressIndicator(),
                  SizedBox(
                    height: getScreenHeight() * 0.025,
                  ),
                  Text('$percentage%')
                ]
              )
            );
              }
        );
      }
    );
    String inputFilePath = await copyVideoInput();
    String startTime = millisecondsToDuration(startTrimmedDuration.value).toString();
    String endTime =  millisecondsToDuration(endTrimmedDuration.value).toString();
    String outputFilePath = await createOutputFile();
    String currentMessage = '${draggedWidthCrop.value} ${videoWidth.value} ${draggedHeightCrop.value} ${videoHeight.value}';
    FFmpegKit.executeAsync('-y -i "$inputFilePath" -ss $startTime -to $endTime -filter:v "crop=${draggedWidthCrop.value / sizeScale}:${draggedHeightCrop.value / sizeScale}:${draggedLeftCrop.value / sizeScale}:${draggedTopCrop.value / sizeScale}" "$outputFilePath"', (session) async {
      FFmpegKitConfig.enableLogCallback((log) async{
        final message = log.getMessage();
        currentMessage = message;
        debugPrint(message);
      });

      final returnCode = await session.getReturnCode();
      if (ReturnCode.isSuccess(returnCode)) {
        Navigator.pop(context, FinishedVideoData(
          outputFilePath,
          Size(currentWidthCrop.value, currentHeightCrop.value)
        ));
      } else if (ReturnCode.isCancel(returnCode)) {
        Navigator.of(context).pop();
        showDialog(
          context: context,
          builder: (_){
            return AlertDialog(
              title: const Text('Process has been cancelled'),
              content: ElevatedButton(
                onPressed: () => Navigator.of(context).pop(),
                child: const Text('Ok')
              )
            );
          }
        );
      } else {
        Navigator.of(context).pop();
        showDialog(
          context: context,
          builder: (_){
            return AlertDialog(
              title: Text(currentMessage),
              content: ElevatedButton(
                onPressed: () => Navigator.of(context).pop(),
                child: const Text('Ok')
              )
            );
          }
        );
      }
    }, (Log log) {},
    (Statistics statistics) {
      int timeInMilliseconds = statistics.getTime().toInt();
      if (timeInMilliseconds > 0) {
        progressPercentage.value = ((
          (timeInMilliseconds/1000).round() * 1000) /
          (((endTrimmedDuration.value - startTrimmedDuration.value) /1000).round() * 1000)
        * 100).round();
      }
    });
  });
}