diff --git a/README.md b/README.md index 1c4681da..86cdb53f 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ import 'package:video_compress/video_compress.dart'; ```dart MediaInfo mediaInfo = await VideoCompress.compressVideo( path, + destPath, quality: VideoQuality.DefaultQuality, deleteOrigin: false, // It's false by default ); @@ -105,7 +106,7 @@ final info = await VideoCompress.getMediaInfo(videopath); ## delete all cache files - Delete all files generated by this will delete all files located at 'video_compress', you shoule ought to know what are you doing. - +- The method is currently deprecated as the compressed video path can be passed to compressVideo method ```dart await VideoCompress.deleteAllCache() ``` @@ -143,7 +144,7 @@ class _Compress extends State { | getByteThumbnail | String `path`[video path], int `quality`(1-100)[thumbnail quality], int `position`[Get a thumbnail from video position] | get thumbnail from video `path` | `Future` | | getFileThumbnail | String `path`[video path], int `quality`(1-100)[thumbnail quality], int `position`[Get a thumbnail from video position] | get thumbnail file from video `path` | `Future` | | getMediaInfo | String `path`[video path] | get media information from video `path` | `Future` | -| compressVideo | String `path`[video path], VideoQuality `quality`[compressed video quality], bool `deleteOrigin`[delete the origin video], int `startTime`[compression video start time], int `duration`[compression video duration from start time], bool `includeAudio`[is include audio in compressed video], int `frameRate`[compressed video frame rate] | compression video at origin video `path` | `Future` | +| compressVideo | String `path`[video path], String `destPath`[compressed video path],VideoQuality `quality`[compressed video quality], bool `deleteOrigin`[delete the origin video], int `startTime`[compression video start time], int `duration`[compression video duration from start time], bool `includeAudio`[is include audio in compressed video], int `frameRate`[compressed video frame rate] | compression video at origin video `path` | `Future` | | cancelCompression | `none` | cancel compressing | `Future` | | deleteAllCache | `none` | Delete all files generated by 'video_compress' will delete all files located at 'video_compress' | `Future` | diff --git a/android/src/main/kotlin/com/example/video_compress/VideoCompressPlugin.kt b/android/src/main/kotlin/com/example/video_compress/VideoCompressPlugin.kt index 7eeadae9..db5b6646 100644 --- a/android/src/main/kotlin/com/example/video_compress/VideoCompressPlugin.kt +++ b/android/src/main/kotlin/com/example/video_compress/VideoCompressPlugin.kt @@ -78,6 +78,7 @@ class VideoCompressPlugin : MethodCallHandler, FlutterPlugin { } "compressVideo" -> { val path = call.argument("path")!! + val destPath = call.argument("destPath")!! val quality = call.argument("quality")!! val deleteOrigin = call.argument("deleteOrigin")!! val startTime = call.argument("startTime") @@ -87,7 +88,6 @@ class VideoCompressPlugin : MethodCallHandler, FlutterPlugin { val tempDir: String = context.getExternalFilesDir("video_compress")!!.absolutePath val out = SimpleDateFormat("yyyy-MM-dd hh-mm-ss").format(Date()) - val destPath: String = tempDir + File.separator + "VID_" + out + path.hashCode() + ".mp4" var videoTrackStrategy: TrackStrategy = DefaultVideoStrategy.atMost(340).build(); val audioTrackStrategy: TrackStrategy diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 3629ef02..9196fd33 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -44,6 +44,7 @@ android { versionCode flutterVersionCode.toInteger() versionName flutterVersionName testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + multiDexEnabled true } buildTypes { diff --git a/example/lib/main.dart b/example/lib/main.dart index d2beae6b..a6ef856a 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,8 +1,11 @@ +import 'dart:io'; + +import 'package:file_selector/file_selector.dart'; import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart'; +import 'package:path/path.dart'; +import 'package:path_provider/path_provider.dart'; import 'package:video_compress/video_compress.dart'; -import 'package:file_selector/file_selector.dart'; -import 'dart:io'; import 'package:video_compress_example/video_thumbnail.dart'; void main() { @@ -50,6 +53,8 @@ class _MyHomePageState extends State { await VideoCompress.setLogLevel(0); final info = await VideoCompress.compressVideo( file.path, + destPath: join((await getApplicationDocumentsDirectory()).path, + "compressed_video_${DateTime.now().millisecondsSinceEpoch}.mp4"), quality: VideoQuality.MediumQuality, deleteOrigin: false, includeAudio: true, diff --git a/example/macos/Flutter/GeneratedPluginRegistrant.swift b/example/macos/Flutter/GeneratedPluginRegistrant.swift index 875bdd33..8f79b0ae 100644 --- a/example/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/example/macos/Flutter/GeneratedPluginRegistrant.swift @@ -6,9 +6,11 @@ import FlutterMacOS import Foundation import file_selector_macos +import path_provider_macos import video_compress func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin")) + PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin")) VideoCompressPlugin.register(with: registry.registrar(forPlugin: "VideoCompressPlugin")) } diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 453ecec7..fdfd5643 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -16,6 +16,8 @@ dependencies: video_player: ^2.4.2 file_selector: ^0.8.4+2 file_selector_macos: ^0.8.2+1 + path_provider: ^2.0.9 + path: ^1.8.0 dev_dependencies: flutter_test: diff --git a/ios/Classes/SwiftVideoCompressPlugin.swift b/ios/Classes/SwiftVideoCompressPlugin.swift index 1e727ff4..0cd0a623 100644 --- a/ios/Classes/SwiftVideoCompressPlugin.swift +++ b/ios/Classes/SwiftVideoCompressPlugin.swift @@ -36,13 +36,14 @@ public class SwiftVideoCompressPlugin: NSObject, FlutterPlugin { getMediaInfo(path, result) case "compressVideo": let path = args!["path"] as! String + let destPath = args!["destPath"] as! String let quality = args!["quality"] as! NSNumber let deleteOrigin = args!["deleteOrigin"] as! Bool let startTime = args!["startTime"] as? Double let duration = args!["duration"] as? Double let includeAudio = args!["includeAudio"] as? Bool let frameRate = args!["frameRate"] as? Int - compressVideo(path, quality, deleteOrigin, startTime, duration, includeAudio, + compressVideo(path, destPath, quality, deleteOrigin, startTime, duration, includeAudio, frameRate, result) case "cancelCompression": cancelCompression(result) @@ -174,18 +175,18 @@ public class SwiftVideoCompressPlugin: NSObject, FlutterPlugin { return composition } - private func compressVideo(_ path: String,_ quality: NSNumber,_ deleteOrigin: Bool,_ startTime: Double?, + private func compressVideo(_ path: String, _ compressionPath: String, _ quality: NSNumber,_ deleteOrigin: Bool,_ startTime: Double?, _ duration: Double?,_ includeAudio: Bool?,_ frameRate: Int?, _ result: @escaping FlutterResult) { let sourceVideoUrl = Utility.getPathUrl(path) let sourceVideoType = "mp4" - + + let compressionUrl = Utility.getPathUrl(compressionPath) + let sourceVideoAsset = avController.getVideoAsset(sourceVideoUrl) let sourceVideoTrack = avController.getTrack(sourceVideoAsset) let uuid = NSUUID() - let compressionUrl = - Utility.getPathUrl("\(Utility.basePath())/\(Utility.getFileName(path))\(uuid.uuidString).\(sourceVideoType)") let timescale = sourceVideoAsset.duration.timescale let minStartTime = Double(startTime ?? 0) diff --git a/lib/src/video_compress/video_compressor.dart b/lib/src/video_compress/video_compressor.dart index 44b3f7c4..1d4bd19d 100644 --- a/lib/src/video_compress/video_compressor.dart +++ b/lib/src/video_compress/video_compressor.dart @@ -2,9 +2,10 @@ import 'dart:async'; import 'dart:convert'; import 'dart:io'; import 'dart:typed_data'; -import 'package:video_compress/src/progress_callback/compress_mixin.dart'; + import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:video_compress/src/progress_callback/compress_mixin.dart'; import 'package:video_compress/video_compress.dart'; abstract class IVideoCompress extends CompressMixin {} @@ -121,6 +122,7 @@ extension Compress on IVideoCompress { /// ``` Future compressVideo( String path, { + required String destPath, VideoQuality quality = VideoQuality.DefaultQuality, bool deleteOrigin = false, int? startTime, @@ -143,6 +145,7 @@ extension Compress on IVideoCompress { setProcessingStatus(true); final jsonStr = await _invoke('compressVideo', { 'path': path, + 'destPath': destPath, 'quality': quality.index, 'deleteOrigin': deleteOrigin, 'startTime': startTime, @@ -168,8 +171,11 @@ extension Compress on IVideoCompress { await _invoke('cancelCompression'); } + /// because path can be provided via compress method, + /// this method only deletes files in cache_dir/video_compress /// delete the cache folder, please do not put other things /// in the folder of this plugin, it will be cleared + @deprecated Future deleteAllCache() async { return await _invoke('deleteAllCache'); }