如何获取flutter ffmpeg执行的进度百分比

Posted

技术标签:

【中文标题】如何获取flutter ffmpeg执行的进度百分比【英文标题】:How to get progress percentage of flutter ffmpeg execution 【发布时间】:2020-10-26 09:08:17 【问题描述】:

我想在颤振中获得 ffmpeg 执行的百分比

我有一些代码示例,但我不知道该怎么做

安卓示例:

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();
            
        

颤振代码:

  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");
  

如何从 statisticsCallback 方法生成执行进度?

请帮帮我

【问题讨论】:

【参考方案1】:

还有另一种方法可以解决这个问题。

首先使用FFprobe而不是初始化VideoPlayer来获取totalFileDuration

如下:

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

那么正如最后一个答案所暗示的那样:

 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");
  

【讨论】:

【参考方案2】:

以下文件:https://pub.dev/packages/flutter_ffmpeg

首先你必须启用StatisticsCallback,这个可以放在initState里面

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

然后在你的 statisticsCallback 函数中,你得到 statistics.timetotalDurationFile(inMilliseconds) 计算

   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");
  

更新: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;
        
      );

【讨论】:

【参考方案3】:

假设您有视频的文件实例 您可以像这样获得正在进行的压缩的进度

/// 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] %
);

【讨论】:

我也需要 Flutter FFmpeg 方面的帮助,你能看看我的question吗?

以上是关于如何获取flutter ffmpeg执行的进度百分比的主要内容,如果未能解决你的问题,请参考以下文章

使用 ffmpeg 获取帧数

在 Flutter 循环进度指示器中显示下载百分比

Flutter - 如何在 FFMPEG 命令中获取使用数据(输入和输出)

如何为无法获取进度百分比的耗时操作增加“伪进度条”?

我可以获得一些关于如何通过在 C# 中使用 webhook API 从 Forge 获取文件翻译进度百分比的帮助/示例吗?

如何使百分比指示器以编程方式在颤动中改变颜色?