Sharik ansari
Sharik ansari

Reputation: 111

How to get progress percentage of flutter ffmpeg execution

I want to get percentage of ffmpeg execution in flutter

I have some code example but I don't know to do this

ANDROID EXAMPLE:

int start = message.indexOf("time=");
    int end = message.indexOf(" bitrate");
    if (start != -1 && end != -1) {
        String duration = message.substring(start + 5, end);
        if (duration != "") {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
                sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
                dialog.setProgress((int)sdf.parse("1970-01-01 " + duration).getTime());                        
            }catch (ParseException e)
            {
                e.printStackTrace();
            }
        }
}

FLUTTER CODE :

  void statisticsCallback(Statistics statistics) {
print("Statistics: executionId: ${statistics.executionId}, time: ${statistics.time}, size: ${statistics.size}, bitrate: ${statistics.bitrate}, speed: ${statistics.speed}, videoFrameNumber: ${statistics.videoFrameNumber}, videoQuality: ${statistics.videoQuality}, videoFps: ${statistics.videoFps}");
  }

how can I generate progress of execution from statisticsCallback method?

Please help me out

Upvotes: 7

Views: 3195

Answers (3)

Ryan Yang
Ryan Yang

Reputation: 140

Following Documents: https://pub.dev/packages/flutter_ffmpeg

First you have to enable StatisticsCallback, this can be placed inside the initState

  @override
  void initState() {
    super.initState();
    final FlutterFFmpegConfig _flutterFFmpegConfig = new FlutterFFmpegConfig();
    _flutterFFmpegConfig.enableStatisticsCallback(this.statisticsCallback);
  }

Then in your statisticsCallback function, you get statistics.time and totalDurationFile(inMilliseconds) calculate

   void statisticsCallback(Statistics statistics) {
    totalProgress = (statistics.time * 100) ~/ totalFileDuration;
    print("Statistics: executionId: ${statistics.executionId}, time: ${statistics.time}, size: ${statistics.size}, bitrate: ${statistics.bitrate}, speed: ${statistics.speed}, videoFrameNumber: ${statistics.videoFrameNumber}, videoQuality: ${statistics.videoQuality}, videoFps: ${statistics.videoFps}");
  }

Update: https://pub.dev/packages/ffmpeg_kit_flutter

      FFmpegKit.executeAsync(arguments, (session) async {
        final returnCode = await session.getReturnCode();
        if (ReturnCode.isSuccess(returnCode)) {
          /// When sucess
        } else if (ReturnCode.isCancel(returnCode)) {
          /// When cancel
        }
      }, (Log log) {},
      (Statistics statistics) {
        if (statistics == null) {
          return;
        }
        if (statistics.getTime() > 0) {
          totalProgress = (statistics.getTime() * 100) ~/ totalVideoDuration;
        }
      });

Upvotes: 3

Mahmoud Ashour
Mahmoud Ashour

Reputation: 327

There is another approach to solve this issue .

first to get the totalFileDuration using FFprobe instead of initializing VideoPlayer

as follows :

   final FlutterFFprobe flutterFFprobe = FlutterFFprobe();
      MediaInformation mediaInformation =
          await flutterFFprobe.getMediaInformation(audioPath!);
      Map? _mediaProperties = mediaInformation.getMediaProperties();
     final _videoDuration = double.parse(_mediaProperties!["duration"].toString());

Then as the last answer suggests :

 void statisticsCallback(Statistics statistics) {
    totalProgress = (statistics.time * 100) ~/ _videoDuration;
    print("Statistics: executionId: ${statistics.executionId}, time: ${statistics.time}, size: ${statistics.size}, bitrate: ${statistics.bitrate}, speed: ${statistics.speed}, videoFrameNumber: ${statistics.videoFrameNumber}, videoQuality: ${statistics.videoQuality}, videoFps: ${statistics.videoFps}");
  }

Upvotes: 2

Mahesh Jamdade
Mahesh Jamdade

Reputation: 20339

Assuming you have the File Instance of the video you can get the progress of the ongoing compression like this

/// initialize the videoController to get its meta data
final VideoPlayerController _controller = VideoPlayerController.file(video);
await _controller.initialize();

int videoInSeconds =  _controller.value.duration.inSeconds;

/// enable the statistics Callback to get the ongoing compression metadata
_flutterFFmpegConfig.enableStatisticsCallback((x){
double percentage = ((x.time / 1000) / videoInSeconds ) * 100;
debugPrint('progress = ${percentage.toStringAsFixed(0)} %'); // progress = [0 - 100] %
});

Upvotes: 1

Related Questions