如何在Dart中一次处理列表2中的项目

问题描述 投票:0回答:1

我有一个要通过从Dart调用ffmpeg进行压缩的视频目录,但是我想一次处理2个视频。

import 'package:glob/glob.dart';
import 'dart:core';

void main(List<String> args) async {
  final videoFiles = Glob('E:/camera/*.mov');
  var videos = videoFiles.listSync();
  var videosInQueue = 0;

  while (true) {
    if (videos.isEmpty) {
      print('No more videos to process');
      break;
    }

    if (videosInQueue < 2){
      print('\n$videosInQueue videos in the queue. Adding more...');
      var video = videos.removeLast();
      var videoInfo = await getVideoInfo(video);

      if (videoInfo['shouldProcess']) {
        videosInQueue += 1;
        var response = ProcessVideo(videoInfo['path'], videoInfo['outputName']);

        response.then((onValue) {
          videosInQueue -= 1;
        });
      }
    }else{
      continue;
    }
  }
}

Future<int> ProcessVideo(String path, String outputName) async {
  print('Processing $path');
  await Future.delayed(const Duration(seconds: 5));
  return 200;
}

[我尝试使用隔离器,但这只是增加了额外的复杂性,因为我没有使用dart进行实际处理,而只是对ffmpeg进行了系统调用。

getVideoInfo函数返回带有视频信息的地图。如果outputName不存在,则shouldProcess为true。该函数的输出如下所示:

{
'shouldProcess':True,
'path': 'E:/camera/recording0023-10-12-2019.mov'
'outputName': 'E:/camera/done/recording0023-10-12-2019.mov'
}

当我运行此代码时,它会将前两个视频添加到队列中,但卡在while循环中

输出:

0 videos in the queue. Adding more...
Processing E:/camera\recording0023-10-12-2019.mov

1 videos in the queue. Adding more...
Processing E:/camera\recording0024-10-12-2019.mov

我怀疑此代码...:

response.then((onValue) {
          videosInQueue -= 1;
        });

不会被调用,所以videoInQueue永远不会递减。我该如何纠正?

asynchronous dart async-await
1个回答
0
投票

带有信号灯的计划示例:

© www.soinside.com 2019 - 2024. All rights reserved.