From 592d198c7234b8dda3de1bbfc14074fc39df20a9 Mon Sep 17 00:00:00 2001 From: lqs Date: Fri, 27 Nov 2020 19:59:36 +0800 Subject: [PATCH 1/2] allow to use non-integer time for trimming --- .../com/example/video_compress/VideoCompressPlugin.kt | 6 +++--- lib/src/video_compress/video_compressor.dart | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) 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 c072b4b3..ca378fc7 100644 --- a/android/src/main/kotlin/com/example/video_compress/VideoCompressPlugin.kt +++ b/android/src/main/kotlin/com/example/video_compress/VideoCompressPlugin.kt @@ -80,8 +80,8 @@ class VideoCompressPlugin : MethodCallHandler, FlutterPlugin { val path = call.argument("path")!! val quality = call.argument("quality")!! val deleteOrigin = call.argument("deleteOrigin")!! - val startTime = call.argument("startTime") - val duration = call.argument("duration") + val startTime = call.argument("startTime") + val duration = call.argument("duration") val includeAudio = call.argument("includeAudio") ?: true val frameRate = if (call.argument("frameRate")==null) 30 else call.argument("frameRate") @@ -129,7 +129,7 @@ class VideoCompressPlugin : MethodCallHandler, FlutterPlugin { val dataSource = if (startTime != null || duration != null){ val source = UriDataSource(context, Uri.parse(path)) - TrimDataSource(source, (1000 * 1000 * (startTime ?: 0)).toLong(), (1000 * 1000 * (duration ?: 0)).toLong()) + TrimDataSource(source, (1000 * 1000 * (startTime ?: 0).toDouble()).toLong(), (1000 * 1000 * (duration ?: 0).toDouble()).toLong()) }else{ UriDataSource(context, Uri.parse(path)) } diff --git a/lib/src/video_compress/video_compressor.dart b/lib/src/video_compress/video_compressor.dart index aed55399..2228a568 100644 --- a/lib/src/video_compress/video_compressor.dart +++ b/lib/src/video_compress/video_compressor.dart @@ -53,7 +53,7 @@ extension Compress on IVideoCompress { Future getByteThumbnail( String path, { int quality = 100, - int position = -1, + num position = 0, }) async { assert(path != null); assert(quality > 1 || quality < 100); @@ -71,7 +71,7 @@ extension Compress on IVideoCompress { Future getFileThumbnail( String path, { int quality = 100, - int position = -1, + num position = 0, }) async { assert(path != null); assert(quality > 1 || quality < 100); @@ -122,8 +122,8 @@ extension Compress on IVideoCompress { String path, { VideoQuality quality = VideoQuality.DefaultQuality, bool deleteOrigin = false, - int startTime, - int duration, + num startTime, + num duration, bool includeAudio, int frameRate = 30, }) async { From 9f493a5ad3d6259630080ca739ecbd288310b057 Mon Sep 17 00:00:00 2001 From: lqs Date: Fri, 25 Dec 2020 15:21:52 +0800 Subject: [PATCH 2/2] fix bugs in iOS --- ios/Classes/SwiftVideoCompressPlugin.swift | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/ios/Classes/SwiftVideoCompressPlugin.swift b/ios/Classes/SwiftVideoCompressPlugin.swift index ad95ed5c..6668a40b 100644 --- a/ios/Classes/SwiftVideoCompressPlugin.swift +++ b/ios/Classes/SwiftVideoCompressPlugin.swift @@ -205,7 +205,7 @@ public class SwiftVideoCompressPlugin: NSObject, FlutterPlugin { exporter.videoComposition = videoComposition } - if !isIncludeAudio { + if isIncludeAudio { exporter.timeRange = timeRange } @@ -214,9 +214,11 @@ public class SwiftVideoCompressPlugin: NSObject, FlutterPlugin { let timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.updateProgress), userInfo: exporter, repeats: true) + stopCommand = false exporter.exportAsynchronously(completionHandler: { + timer.invalidate() + self.exporter = nil if(self.stopCommand) { - timer.invalidate() self.stopCommand = false var json = self.getMediaInfoJson(path) json["isCancel"] = true @@ -224,18 +226,7 @@ public class SwiftVideoCompressPlugin: NSObject, FlutterPlugin { return result(jsonString) } if deleteOrigin { - timer.invalidate() - let fileManager = FileManager.default - do { - if fileManager.fileExists(atPath: path) { - try fileManager.removeItem(atPath: path) - } - self.exporter = nil - self.stopCommand = false - } - catch let error as NSError { - print(error) - } + Utility.deleteFile(path) } var json = self.getMediaInfoJson(compressionUrl.absoluteString) json["isCancel"] = false @@ -245,8 +236,8 @@ public class SwiftVideoCompressPlugin: NSObject, FlutterPlugin { } private func cancelCompression(_ result: FlutterResult) { - exporter?.cancelExport() stopCommand = true + exporter?.cancelExport() result("") }