我有一个视频文件,想为整个视频生成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
但是它会为每个时间帧生成相同的第一个缩略图。那么解决方案是什么呢?提前感谢您的帮助!
1条答案
按热度按时间ryevplcw1#
我对代码做了一些编辑,它运行正常:
我之前试过,用你的代码样本,也有同样的问题,然后我发现视频上有相同截图的是那些'int ms = totalMillisecs ~/ 10 * I;持续时间值。
因此,首先确保视频在那些持续时间上具有不同的帧,或者只是使MS差异更大。