Wednesday, 17 July 2019

Compressed video generates a new path with flutter

flutter_video_compress

Compressed video generates a new path, keep the source video or delete it. provide get video information or get thumbnail of the video file.

Before Android installation

If your program not enabled AndroidX, you will need to add the following code to the last line of the android/build.gradle file.
rootProject.allprojects {
    subprojects {
        project.configurations.all {
            resolutionStrategy.eachDependency { details ->
                if (details.requested.group == 'androidx.core' && !details.requested.name.contains('androidx')) {
                    details.useVersion "1.0.1"
                }
            }
        }
    }
}
Groovy

Before IOS installation

If your program not support swift, you need to add the following code in ios/Podfile.detail
target 'Runner' do
  use_frameworks! # <--- add this
  ...
end

# -----insert code start-----
pre_install do |installer|
  installer.analysis_result.specifications.each do |s|
      if s.name == 'Regift'
        s.swift_version = '4.0'
    # elsif s.name == 'other-Plugin'
    #   s.swift_version = '5.0'
    # else
    #   s.swift_version = '4.0'
      end
  end
end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['ENABLE_BITCODE'] = 'NO'
    end
  end
end
# -----insert code end-----
Ruby

Methods

FUNCTIONPARAMETERDESCRIPTIONRETURN
getThumbnailString [path], int [quality](1-100), int [position]get thumbnail from [path][Future<Uint8List>]
getThumbnailWithFileString [path], int [quality](1-100), int [position]get thumbnail file from [path][Future<File>]
convertVideoToGifString [path], int [startTime](from 0 start), int [endTime], int [duration]converts provided video to a gif[Future<File>]
getMediaInfoString [path]get media information from [path][Future<MediaInfo>]
compressVideoString [path], VideoQuality [quality], bool [deleteOrigin], int [startTime], int [duration], bool [includeAudio], bool [frameRate]compress video at [path][Future<MediaInfo>]
cancelCompression[none]stop compressing the file that is currently being compressed.[Future<void>]
deleteAllCache[none]Delete the cache, please do not put other things in the folder of this plugin, it will be cleared[Future<bool>]

Subscriptions

SUBSCRIPTIONDESCRIPTIONSTREAM
compressProgress$Subscribe the conversion progress steamdouble [progress]

Usage

Installing
add flutter_video_compress as a dependency in your pubspec.yaml file.
dependencies:
  flutter_video_compress: ^0.3.x
YAML
Creating instance.
final _flutterVideoCompress = FlutterVideoCompress();
Dart
Get thumbnail by video file
final uint8list = await _flutterVideoCompress.getThumbnail(
  file.path,
  quality: 50,
);
Dart
Get thumbnail file by video file
final thumbnailFile = await _flutterVideoCompress.getThumbnailWithFile(
  file.path,
  quality: 50,
);
Dart
Converts provided video to a gif.
final file = await _flutterVideoCompress.convertVideoToGif(
  videoFile.path,
  startTime: 0,
  duration: 5,
);
print(file.path);
Dart
Get media information
Currently only supports video
final info = await _flutterVideoCompress.getMediaInfo(file.path);
print(info.toJson());
Dart
Compression Video
Compatible with ios in Android and web after compression
final info = await _flutterVideoCompress.compressVideo(
  file.path,
  deleteOrigin: true,
);
print(info.toJson());
Dart
Check Compressing state
_flutterVideoCompress.isCompressing
Dart
Stop compression
Android will print InterruptedException, but does not affect the use
await _flutterVideoCompress.cancelCompression()
Dart
delete all cache file
Delete all the files generated by this plugin, I think you ought to know what you are doing.
await _flutterVideoCompress.deleteAllCache()
Dart
Subscribe the conversion progress steam
class ... extends State<MyApp> {
  Subscription _subscription;

  @override
  void initState() {
    super.initState();
    _subscription =
        _flutterVideoCompress.compressProgress$.subscribe((progress) {
      print('progress: $progress');
    });
  }

  @override
  void dispose() {
    super.dispose();
    _subscription.unsubscribe();
  }
}
Dart

Notice

If you find that the size of the apk is significantly increased after importing the plugin, it may be due to the following reasons:
  • exclude x86 related files (./assets)
  • This library does not use ffprobe, only usesffmpeg, but the application still has ffprobe, so it needs to be excluded (asssets/arm or assets/x86)
add this config in build.gradle:
  • Do not use ignoreAssetsPattern "!x86" in debug mode, will crash on the simulator
android {
 ...
   
   // Reduce your application size with this configuration
  aaptOptions {
       ignoreAssetsPattern "!x86:!*ffprobe"
  }
  
  buildTypes {
  ...
  
  }
Gradle

GitHub