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: (_){
        return ValueListenableBuilder<int>(
          valueListenable: progressPercentage,
          builder: (context, int percentage, child) {
            return AlertDialog(
              title: Text('Please wait...'),
              content: Container(
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    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 = '';
    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 = '';
        debugPrint(message);
      });

      final returnCode = await session.getReturnCode();
      if (ReturnCode.isSuccess(returnCode)) {
        Navigator.of(context).pop();
        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: Text('Process has been cancelled'),
              content: ElevatedButton(
                onPressed: () => Navigator.of(context).pop(),
                child: Text('Ok')
              )
            );
          }
        );
      } else {
        Navigator.of(context).pop();
        showDialog(
          context: context,
          builder: (_){
            return AlertDialog(
              title: Text(currentMessage),
              content: ElevatedButton(
                onPressed: () => Navigator.of(context).pop(),
                child: Text('Ok')
              )
            );
          }
        );
      }

      Timer.periodic(const Duration(milliseconds: 1000), (Timer timer) async{
        timer.cancel();
        progressPercentage.value = 0;
      });
    }, (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();
      }
    });
  });
}