Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make compression destination path changeable #187

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down Expand Up @@ -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()
```
Expand Down Expand Up @@ -143,7 +144,7 @@ class _Compress extends State<Compress> {
| 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<Uint8List>` |
| 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<File>` |
| getMediaInfo | String `path`[video path] | get media information from video `path` | `Future<MediaInfo>` |
| 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<MediaInfo>` |
| 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<MediaInfo>` |
| cancelCompression | `none` | cancel compressing | `Future<void>` |
| deleteAllCache | `none` | Delete all files generated by 'video_compress' will delete all files located at 'video_compress' | `Future<bool>` |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class VideoCompressPlugin : MethodCallHandler, FlutterPlugin {
}
"compressVideo" -> {
val path = call.argument<String>("path")!!
val destPath = call.argument<String>("destPath")!!
val quality = call.argument<Int>("quality")!!
val deleteOrigin = call.argument<Boolean>("deleteOrigin")!!
val startTime = call.argument<Int>("startTime")
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ android {
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}

buildTypes {
Expand Down
9 changes: 7 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -50,6 +53,8 @@ class _MyHomePageState extends State<MyHomePage> {
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,
Expand Down
2 changes: 2 additions & 0 deletions example/macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
2 changes: 2 additions & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 6 additions & 5 deletions ios/Classes/SwiftVideoCompressPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion lib/src/video_compress/video_compressor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down Expand Up @@ -121,6 +122,7 @@ extension Compress on IVideoCompress {
/// ```
Future<MediaInfo?> compressVideo(
String path, {
required String destPath,
VideoQuality quality = VideoQuality.DefaultQuality,
bool deleteOrigin = false,
int? startTime,
Expand All @@ -143,6 +145,7 @@ extension Compress on IVideoCompress {
setProcessingStatus(true);
final jsonStr = await _invoke<String>('compressVideo', {
'path': path,
'destPath': destPath,
'quality': quality.index,
'deleteOrigin': deleteOrigin,
'startTime': startTime,
Expand All @@ -168,8 +171,11 @@ extension Compress on IVideoCompress {
await _invoke<void>('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<bool?> deleteAllCache() async {
return await _invoke<bool>('deleteAllCache');
}
Expand Down