将 Youtube 数据 API V3 视频持续时间格式转换为 Dart 中的秒数

Converting Youtube Data API V3 video duration format to seconds in Dart

在我的例子中,我得到了这种格式的时间:PT2H3M20S 我不知道正则表达式 [使用 dart] 所以我只想知道我们如何从上面的格式计算毫秒..提前感谢

  Future<http.Response> getVideoDuration({var videoUri}) async {
    // print(videoUri);
    final BI_YT_API_KEY = "some_API";
    var lArr = videoUri.split('/');
    var lId = lArr[lArr.length - 1];
    var data = await http.get('https://www.googleapis.com/youtube/v3/videos' +
        "?id=$lId&part=contentDetails&key=$BI_YT_API_KEY");
    if (data.statusCode == 200) {
      var jom = json.decode(data.body);
      print(jom['items'][0]['contentDetails']['duration']);
      var duration = data.body[0];
    }

花了一些时间。但终于完成了。

你可以这样使用它。

int seconds = convertTime("PT1H11S");

此处,seconds 将是转换后的持续时间(以秒为单位)。因此,对于 PT1H11S,答案将是 3611 因为 1 hour == 3600 seconds + 11 seconds.

int convertTime(String duration) {

  RegExp regex = new RegExp(r'(\d+)');
  List<String> a = regex.allMatches(duration).map((e) => e.group(0)!).toList();

  if (duration.indexOf('M') >= 0 &&
      duration.indexOf('H') == -1 &&
      duration.indexOf('S') == -1) {
    a = ["0", a[0], "0"];
  }

  if (duration.indexOf('H') >= 0 && duration.indexOf('M') == -1) {
    a = [a[0], "0", a[1]];
  }
  if (duration.indexOf('H') >= 0 &&
      duration.indexOf('M') == -1 &&
      duration.indexOf('S') == -1) {
    a = [a[0], "0", "0"];
  }

  int seconds = 0;

  if (a.length == 3) {
    seconds = seconds + int.parse(a[0]) * 3600;
    seconds = seconds + int.parse(a[1]) * 60;
    seconds = seconds + int.parse(a[2]);
  }

  if (a.length == 2) {
    seconds = seconds + int.parse(a[0]) * 60;
    seconds = seconds + int.parse(a[1]);
  }

  if (a.length == 1) {
    seconds = seconds + int.parse(a[0]);
  }
  return seconds;
}

注:我不懂flutter,但听说flutter开发者应该会使用Java代码。此答案基于 Java.

tl;博士

有了Java,您只需要:

Duration.parse("PT2H3M20S").toMillis()

java.time.Duration is modelled on ISO-8601 standards and was introduced with Java-8 as part of JSR-310 implementation.

如果您浏览了上述链接,您可能已经注意到 PT2H3M20S 指定了 2 小时 3 分 20 秒的持续时间,您可以将其解析为 Duration 对象,您可以转换成毫秒。

演示:

import java.time.Duration;

public class Main {
    public static void main(String[] args) {
        String strIso8601Duration = "PT2H3M20S";
        Duration duration = Duration.parse(strIso8601Duration);
        long millis = duration.toMillis();
        System.out.println(millis);
    }
}

输出:

7400000

详细了解 modern date-time API* from Trail: Date Time


* 无论出于何种原因,如果您必须坚持Java 6 或Java 7,您可以使用ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and

我还设法使用 dart 获得了以秒为单位的持续时间(如果有人需要的话)

/// For duration  = 2H1M48S
converToSeconds(String duration){

      var hour = "", minute = "", seconds = "";
      var tempList = duration.split('');

      /// HOUR
      if (tempList.contains('H')) {
        var ind = tempList.indexOf('H');
        for (int i = 0; i < ind; i++) {
          hour = hour + tempList[i];
        }
        tempList.removeRange(0, ind + 1);
      }

      /// MINUTES
      if (tempList.contains('M')) {
        var ind = tempList.indexOf('M');
        for (int i = 0; i < ind; i++) {
          minute = minute + tempList[i];
        }
        tempList.removeRange(0, ind + 1);
      }

      /// SECONDS
      if (tempList.contains('S')) {
        var ind = tempList.indexOf('S');
        for (int i = 0; i < ind; i++) {
          seconds = seconds + tempList[i];
        }
        tempList.removeRange(0, ind + 1);
      }

      /// CONVER TO INT
      hour = hour != "" ? hour : '0';
      seconds = seconds != "" ? seconds : '0';
      minute = minute != "" ? minute : '0';

      var ms = ((int.parse(hour) * 3600 + int.parse(minute) * 60) + int.parse(seconds));
}