Skip to content

Ddarkbooked/video_duration_native

Repository files navigation

video_duration_native

Very Good Ventures Very Good Ventures

Developed with 💙 by Very Good Ventures 🦄

coverage style: very good analysis License: MIT

A lightweight native video duration reader for Flutter that works on Android and iOS. No FFmpeg, no byte loading—just native platform APIs for fast, efficient video metadata extraction.

Generated by the Very Good CLI. 🤖

Features

Lightweight - Uses native platform APIs only (no FFmpeg dependencies)
Fast - Reads metadata without loading video data into memory
Cross-platform - Works on both Android and iOS
Content URI support - Android content:// URIs supported
Error handling - Returns Duration.zero on errors
Type-safe - Returns Dart Duration object

Platform Support

Platform Implementation Supported Paths
Android MediaMetadataRetriever File paths, content:// URIs
iOS AVURLAsset File paths, file:// URLs

Installation

Add this to your package's pubspec.yaml file:

dependencies:
  video_duration_native: ^0.1.0

Usage

import 'package:video_duration_native/video_duration_native.dart';

// Get duration from a file path
final duration = await getDuration('/path/to/video.mp4');
print('Video duration: ${duration.inSeconds} seconds');

// Android content URI
final duration2 = await getDuration('content://media/external/video/123');

// Returns Duration.zero on error or invalid path
final invalid = await getDuration('/invalid/path.mp4');
print(invalid); // Duration: 0:00:00.000000

Example with File Picker

import 'package:file_picker/file_picker.dart';
import 'package:video_duration_native/video_duration_native.dart';

Future<void> pickAndGetDuration() async {
  final result = await FilePicker.platform.pickFiles(
    type: FileType.video,
  );

  if (result != null && result.files.isNotEmpty) {
    final path = result.files.first.path;
    if (path != null) {
      final duration = await getDuration(path);
      print('Duration: ${duration.inSeconds} seconds');
      print('Milliseconds: ${duration.inMilliseconds} ms');
    }
  }
}

How It Works

Android (MediaMetadataRetriever)

The Android implementation uses MediaMetadataRetriever to extract video metadata:

  • Supports both file paths and content URIs
  • Lightweight metadata extraction
  • Automatic resource cleanup
  • Returns duration in milliseconds

iOS (AVURLAsset)

The iOS implementation uses AVURLAsset with optimized settings:

  • preloadsEligibleContentKeys = false - Avoids loading tracks into memory
  • prefersPreciseDurationAndTiming = true - Ensures accurate duration (iOS 13+)
  • Supports file paths and URLs
  • Returns duration in milliseconds

Testing

This plugin uses Fluttium for integration tests. For detailed testing information, see TESTING.md.

Quick Start

# Run unit tests
cd video_duration_native
flutter test

# Run integration tests
cd video_duration_native/example
flutter test integration_test/app_test.dart

# Run Fluttium E2E tests
cd video_duration_native/example
fluttium test flows/test_platform_name.yaml
fluttium test flows/test_get_duration.yaml

❗ To run Fluttium tests, you need fluttium_cli installed. See how.

Example App

The example app demonstrates all plugin features:

  • Get platform name
  • Pick video files using native file picker
  • Get video duration with manual path input
  • Display duration in seconds and milliseconds
cd video_duration_native/example
flutter run

Release Management

This project uses release-please for automated version management and changelog generation. The workflow:

  • Monitors commits to the main branch
  • Automatically generates changelog entries based on conventional commits
  • Creates release PRs when changes are ready
  • Publishes releases to GitHub and pub.dev

Conventional Commits:

  • feat: - New features (minor version bump)
  • fix: - Bug fixes (patch version bump)
  • BREAKING CHANGE: - Breaking changes (major version bump)

About

Lightweight native video duration reader for Flutter. No FFmpeg, no byte loading—just native platform APIs.

Resources

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors