flutter video_thumbnail包不会为不同的时间帧生成不同的缩略图

e0bqpujr  于 2022-12-05  发布在  Flutter
关注(0)|答案(1)|浏览(134)

我有一个视频文件,想为整个视频生成10个不同的缩略图。我使用video_thumbnail包和VideoThumbnail.fromFile()构造函数。该构造函数有一个名为timeMs的参数,如文档所述:generates the thumbnail from the frame around the specified millisecond。我的代码如下:

List<String> recordingThumbnailsPaths =
      List<String>.generate(10, (int index) => '');

  Future<void> getRecordingThumbnailsPaths() async {
    final int totalMilliSecs =
        (await FlutterVideoInfo().getVideoInfo(videoFile!.path))!
            .duration!
            .toInt();
    // For debug purpose
    print('Total (ms): $totalMilliSecs');

    for (int i = 0; i < 10; i++) {
      int ms = totalMilliSecs ~/ 10 * i;
      recordingThumbnailsPaths[i] = (await VideoThumbnail.thumbnailFile(
          video: videoFile!.path, timeMs: ms))!;
      // For debug purpose
      print('Iteration $i\nCurrent (ms): $ms');
    }
  }

下面是每次迭代的日志输出:

Total (ms): 17480

Iteration 0
Current (ms): 0

Iteration 1
Current (ms): 1748

Iteration 2
Current (ms): 3496

Iteration 3
Current (ms): 5244
...
Iteration 9
Current (ms): 15732

但是它会为每个时间帧生成相同的第一个缩略图。那么解决方案是什么呢?提前感谢您的帮助!

ryevplcw

ryevplcw1#

我对代码做了一些编辑,它运行正常:

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path/path.dart';
import 'package:video_thumbnail/video_thumbnail.dart';

class TestPage extends StatefulWidget {
  const TestPage({
    super.key,
  });

  @override
  State<TestPage> createState() => _TestPageState();
}

class _TestPageState extends State<TestPage> {
  List<String> recordingThumbnailsPaths = [];

  Future<void> getRecordingThumbnailsPaths() async {
    final videoFile = /* Your Video File*/;

    final int totalMilliSecs = 15000;
    for (int i = 0; i < 10; i++) {
      int ms = totalMilliSecs ~/ 100 * i;
      final current = (await VideoThumbnail.thumbnailFile(
        video: videoFile!.path!,
        timeMs: ms,
        thumbnailPath: dirname(videoFile!.path!) + "/$i.png",
      ))!;
      recordingThumbnailsPaths.add(current);
    }
    setState(() {});
    print(recordingThumbnailsPaths);
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {},
      child: Scaffold(
        resizeToAvoidBottomInset: true,
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                onPressed: () async {
                  await getRecordingThumbnailsPaths();
                },
                child: Text("click"),
              ),
              Wrap(
                children: [
                  ...List.generate(
                    recordingThumbnailsPaths.length,
                    (index) => Image.file(
                      File(
                        recordingThumbnailsPaths[index],
                      ),
                      width: 100,
                    ),
                  ),
                ],
              )
            ],
          ),
        ),
      ),
    );
  }
}

我之前试过,用你的代码样本,也有同样的问题,然后我发现视频上有相同截图的是那些'int ms = totalMillisecs ~/ 10 * I;持续时间值。
因此,首先确保视频在那些持续时间上具有不同的帧,或者只是使MS差异更大。

相关问题