我很难从 mp4 文件中读取元数据。我想知道视频的时长(以秒为单位)。
这个文件位于assets/下,准确来说路径是assets/test.mp4
int getLength() {
var file = File('./assets/test.mp4');
// get the length
var length;
return length;
}
您可以使用视频播放器插件并使用此代码
https://pub.dev/packages/video_player
VideoPlayerController controller = VideoPlayerController.file('fileNameHere');
await controller.initialize();
print(controller.value.duration);
使用这个插件flutter_ffmpeg:^0.4.2
print("Duration: ${info.getMediaProperties()['duration']}");
final FlutterFFprobe _flutterFFprobe = new FlutterFFprobe();
_flutterFFprobe.getMediaInformation("<file path or uri>").then((info) {
print("Media Information");
print("Path: ${info.getMediaProperties()['filename']}");
print("Format: ${info.getMediaProperties()['format_name']}");
print("Duration: ${info.getMediaProperties()['duration']}");
print("Start time: ${info.getMediaProperties()['start_time']}");
print("Bitrate: ${info.getMediaProperties()['bit_rate']}");
Map<dynamic, dynamic> tags = info.getMediaProperties()['tags'];
if (tags != null) {
tags.forEach((key, value) {
print("Tag: " + key + ":" + value + "\n");
});
}
if (info.getStreams() != null) {
List<StreamInformation> streams = info.getStreams();
if (streams.length > 0) {
for (var stream in streams) {
print("Stream id: ${stream.getAllProperties()['index']}");
print("Stream type: ${stream.getAllProperties()['codec_type']}");
print("Stream codec: ${stream.getAllProperties()['codec_name']}");
print("Stream full codec: ${stream.getAllProperties()['codec_long_name']}");
print("Stream format: ${stream.getAllProperties()['pix_fmt']}");
print("Stream width: ${stream.getAllProperties()['width']}");
print("Stream height: ${stream.getAllProperties()['height']}");
print("Stream bitrate: ${stream.getAllProperties()['bit_rate']}");
print("Stream sample rate: ${stream.getAllProperties()['sample_rate']}");
print("Stream sample format: ${stream.getAllProperties()['sample_fmt']}");
print("Stream channel layout: ${stream.getAllProperties()['channel_layout']}");
print("Stream sar: ${stream.getAllProperties()['sample_aspect_ratio']}");
print("Stream dar: ${stream.getAllProperties()['display_aspect_ratio']}");
print("Stream average frame rate: ${stream.getAllProperties()['avg_frame_rate']}");
print("Stream real frame rate: ${stream.getAllProperties()['r_frame_rate']}");
print("Stream time base: ${stream.getAllProperties()['time_base']}");
print("Stream codec time base: ${stream.getAllProperties()['codec_time_base']}");
Map<dynamic, dynamic> tags = stream.getAllProperties()['tags'];
if (tags != null) {
tags.forEach((key, value) {
print("Stream tag: " + key + ":" + value + "\n");
});
}
}
}
}
});
这是 Anandh Krishnan 答案的最新版本:
我们应该新安装 ffmpeg_kit_flutter 包。 另请注意:
安装ffmpeg_kit_flutter默认启用https包。
您可能需要安装
ffmpeg_kit_flutter_min_gpl
才能使用 x264 和 x265 格式。
我们可以通过以下方式读取以秒为单位的持续时间:
FFprobeKit.getMediaInformation('$videoFilePath').then((info) {
print('Duration: ${info.getMediaInformation()!.getProperty('format')['duration']}');
});