video_compress Flutter Library

repository·master·Indexed 42 days ago

https://github.com/jonataslaw/videocompress

A lightweight Flutter library for compressing videos, removing audio, and manipulating thumbnails using 100% native code. It avoids FFMPEG to maintain efficiency and compatibility across Safari, Chrome, Android, and iOS. Key features include the VideoCompress.compressVideo method for MP4 encoding, thumbnail extraction via getByteThumbnail and getFileThumbnail, and media metadata retrieval using the MediaInfo class.

Tokens
3.7K
Snippets
13
Records
13
Agent score
52%

What's inside video_compress

  1. Install video_compress

    master

    To use video_compress in your Flutter project, follow these steps:

    1. Add the dependency to your pubspec.yaml file:

    2. Run the install command in your terminal:

    3. Import the package in your Dart code.

    dependencies:
      video_compress: ^3.1.0
    $ pub get
    import 'package:video_compress/video_compress.dart';
  2. Customize iOS Launch Screen Assets

    master

    To customize the launch screen for the iOS version of your Flutter project, you can use one of two methods:

    1. Direct File Replacement: Replace the existing image files directly within the example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory with your own assets.
    2. Xcode Interface:
      • Open the iOS workspace using open ios/Runner.xcworkspace.
      • In the Xcode Project Navigator, navigate to Runner/Assets.xcassets.
      • Drag and drop your desired images into the asset catalog to replace the launch images.
    open ios/Runner.xcworkspace
  3. Listen to compression progress

    master

    To track the progress of a compression task, subscribe to the compressProgress$ stream. Ensure you unsubscribe from the subscription when the widget is disposed to prevent memory leaks.

    class _Compress extends State<Compress> {
    
      Subscription _subscription;
    
      @override
      void initState() {
        super.initState();
        _subscription = 
            VideoCompress.compressProgress$.subscribe((progress) {
          debugPrint('progress: $progress');
        });
      }
    
      @override
      void dispose() {
        _subscription.unsubscribe();
        super.dispose();
      }
    }
  4. Compress a video using VideoCompress.compressVideo

    master

    To compress a video, use the VideoCompress.compressVideo method. You must provide the file path of the source video. You can optionally specify the compression quality using the VideoQuality enum and decide whether to delete the original file using the deleteOrigin flag.

    Returns a MediaInfo object containing the path to the compressed video.

    File file = await ImagePicker.pickVideo(source: ImageSource.gallery);
    final info = await VideoCompress.compressVideo(
      file.path,
      quality: VideoQuality.MediumQuality,
      deleteOrigin: false,
    );
    
    // info.path contains the path to the compressed video
    String compressedPath = info.path;
  5. Get a thumbnail from a video path

    master

    The library provides two ways to extract thumbnails from a video:

    Get thumbnail as bytes (getByteThumbnail)

    Returns a Future<Uint8List> containing the thumbnail data.

    • path: Video path.
    • quality: Integer (1-100). Defaults to 100.
    • position: The video position to capture the frame from. Defaults to -1.

    Get thumbnail as a file (getFileThumbnail)

    Returns a Future<File> pointing to the saved thumbnail file.

    • path: Video path.
    • quality: Integer (1-100). Defaults to 100.
    • position: The video position to capture the frame from. Defaults to -1.
    // Get bytes
    final uint8list = await VideoCompress.getByteThumbnail(
      videopath,
      quality: 50, // default(100)
      position: -1 // default(-1)
    );
    
    // Get file
    final thumbnailFile = await VideoCompress.getFileThumbnail(
      videopath,
      quality: 50, // default(100)
      position: -1 // default(-1)
    );
  6. Compress a video

    master

    Use VideoCompress.compressVideo to compress a video file. The library encodes videos into an MP4 container with AAC audio for maximum compatibility across Safari, Chrome, Android, and iOS.

    Parameters:

    • path: The file path of the origin video.
    • quality: A VideoQuality enum value (e.g., VideoQuality.DefaultQuality).
    • deleteOrigin: If true, the original video file will be deleted after compression. Defaults to false.
    • startTime: (Optional) Compression start time.
    • duration: (Optional) Compression duration.
    • includeAudio: (Optional) Whether to include audio in the compressed video.
    • frameRate: (Optional) The compressed video frame rate.

    Returns a Future<MediaInfo> containing details about the compressed video.

    MediaInfo mediaInfo = await VideoCompress.compressVideo(
      path,
      quality: VideoQuality.DefaultQuality, 
      deleteOrigin: false, // It's false by default
    );
  7. Use the MediaInfo class to access video properties

    master

    The MediaInfo class is a data model used to represent metadata and properties of a video file. It can be instantiated manually or reconstructed from a JSON map using MediaInfo.fromJson.

    Key properties include:

    • path: The file path (String).
    • title: The video title.
    • author: The video author.
    • width & height: The video dimensions in pixels.
    • orientation: The video orientation (available on Android API level 17+).
    • filesize: The file size in bytes.
    • duration: The video duration in milliseconds.
    • file: A File object corresponding to the path.
    // Example of manual instantiation
    final info = MediaInfo(
      path: '/path/to/video.mp4',
      width: 1920,
      height: 1080,
      duration: 15000.0,
    );
    
    // Example of creating from JSON
    final json = {
      'path': '/path/to/video.mp4',
      'duration': '15000',
      'width': 1920,
      'height': 1080,
    };
    final infoFromJson = MediaInfo.fromJson(json);
  8. Serialize MediaInfo to JSON

    master

    You can convert a MediaInfo instance into a Map<String, dynamic> using the toJson() method. This is useful for persisting video metadata. Note that the file field in the resulting JSON map is stored as a string representation of the file path.

    final info = MediaInfo(path: '/path/to/video.mp4', title: 'My Video');
    Map<String, dynamic> json = info.toJson();
    print(json['path']); // Outputs: /path/to/video.mp4