How can I get video duration without
https://pub.dev/packages/video_player
package or base this player. Is there any other way?
video player duration
How can I get video duration without
https://pub.dev/packages/video_player
package or base this player. Is there any other way?
video player duration
You can use ffmpeg package for the same, just do:
class ClassName {
final FlutterFFmpeg _flutterFFmpeg = new FlutterFFmpeg();
...
void someFunction() {
_flutterFFmpeg
.getMediaInformation("<file path or uri>")
.then((info) => print(info));
}
}
You can use ffmpeg_kit_flutter.
flutter_ffmpeg is discontinued now.
Here is an example:
import 'dart:io';
import 'package:ffmpeg_kit_flutter/ffprobe_kit.dart';
class VideoFileDurationService {
const VideoFileDurationService._();
static Future<Duration> getDuration(File file) async {
final data = await FFprobeKit.getMediaInformation(file.path);
final media = data.getMediaInformation();
final secondsStr = media?.getDuration();
final milliseconds = _secondsStrToMilliseconds(secondsStr);
if (milliseconds == null) return Duration.zero;
return Duration(milliseconds: milliseconds);
}
static int? _secondsStrToMilliseconds(String? secondsStr) {
if (secondsStr == null) return null;
final seconds = double.tryParse(secondsStr);
if (seconds == null) return null;
final milliseconds = (seconds * 1000).toInt();
return milliseconds;
}
}
Hope this help :)
You can use the video controller :
VideoPlayerController _controller = VideoPlayerController.network('https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4')
..initialize().then((_) {
// Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
});
Duration durationOfVideo = _controller.value.duration;