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

Video Compressor Didn't Work #281

Open
ravindra123-stack opened this issue Oct 10, 2024 · 0 comments
Open

Video Compressor Didn't Work #281

ravindra123-stack opened this issue Oct 10, 2024 · 0 comments

Comments

@ravindra123-stack
Copy link

ravindra123-stack commented Oct 10, 2024

When I compress the VideoSize >=10MB
After compression it almost double the initial size ,you can check in given text.

I/hw-BpHwBinder(14438): onLastStrongRef automatically unlinking death recipients
I/flutter (14438): Original File Size in MB: 84.54
I/flutter (14438): Compressed File Size in MB: 112.64
I/flutter (14438): Compressed video path: /data/user/0/football.interkashi.debug/app_flutter/compressed_video.mp4

CODE -
import 'dart:io';
import 'package:get/get.dart';
import 'package:interkashi/modules/components/extension/get_extension.dart';
import 'package:interkashi/modules/feature/academy/model/training_video_upload_model.dart';
import 'package:interkashi/modules/feature/academy/service/upload_video_service.dart';
import 'package:path_provider/path_provider.dart';
import 'package:video_compress/video_compress.dart'; // Assuming this is the same as your custom implementation

// Use the custom video compressor instance
final IVideoCompress videoCompressor = VideoCompress;

class UploadVideoController extends GetxController {
final UploadVideoService _uploadVideoService;
UploadVideoController(this._uploadVideoService);

Rxn selectedVideoPath = Rxn();
RxBool isUploading = RxBool(false);
RxDouble uploadProgress = RxDouble(0.0);
Rx<bool?> uploadSuccess = Rx<bool?>(null);
final double maxSizeMB = 100;
RxBool flag = true.obs;
late String lessonId;
late String courseId;

@OverRide
void onInit() {
super.onInit();
final arguments = Get.arguments;
lessonId = arguments['lessonId'];
courseId = arguments['courseId'];

// Subscribe to the compression progress stream
videoCompressor.compressProgress$.subscribe((progress) {
  uploadProgress.value = progress / 100;
});

}

Future compressVideo(String path) async {
// Compress the video using the custom IVideoCompress implementation
final info = await videoCompressor.compressVideo(
path,
quality: VideoQuality.Res640x480Quality,
deleteOrigin: false,
includeAudio: false,
frameRate: 24,
);

if (info != null && info.path != null) {
  // Save the compressed video
  Directory svdir = await getApplicationDocumentsDirectory();
  var svPath = '${svdir.path}/compressed_video.mp4';

  File compressedFile = File(info.path!);
  File savedFile = await compressedFile.copy(svPath);

  selectedVideoPath.value = savedFile.path;

  // Calculate and print the file size
  int originalSize = File(path).lengthSync();
  int compressedSize = savedFile.lengthSync();

  double originalSizeMB = originalSize / (1024 * 1024);
  double compressedSizeMB = compressedSize / (1024 * 1024);

  print("Original File Size in MB: ${originalSizeMB.toStringAsFixed(2)}");
  print("Compressed File Size in MB: ${compressedSizeMB.toStringAsFixed(2)}");

  // Check if the compressed file size is under the max size
  if (compressedSizeMB > maxSizeMB) {
    flag.value = false;
    GetExt.snackBar("Compressed file size is still larger than 100MB");
  } else {
    flag.value = true;
  }
} else {
  GetExt.snackBar("Video compression failed or file path not found.");
}

}

Future checkFileSize(String? video) async {
if (video == null) return;

File videoFile = File(video);
int fileSize = videoFile.lengthSync();
double fileSizeMB = fileSize / (1024 * 1024);

if (fileSizeMB > maxSizeMB) {
  flag.value = false;
  GetExt.snackBar("Please upload a file smaller than 100MB");
} else {
  selectedVideoPath.value = video;
  flag.value = true;
}

}

Future uploadVideo() async {
if (selectedVideoPath.value == null || !flag.value) {
return;
}

uploadSuccess.value = null;
isUploading.value = true;
uploadProgress.value = 0.0;

// Compress the video before uploading
await compressVideo(selectedVideoPath.value!);
print("Compressed video path: ${selectedVideoPath.value!}");

// Upload the video to the server
final uploadResponse = await _uploadVideoService.uploadVideo(selectedVideoPath.value!);
print("Upload response body: $uploadResponse");

if (uploadResponse != null) {
  final trainingResponse = await _uploadVideoService.uploadTrainingData(
    TrainingVideoUploadModel(
      lessonId: lessonId,
      courseId: courseId,
      videoUrl: uploadResponse.video.url,
      thumbnailUrl: uploadResponse.thumbnail.url,
    ),
  );

  if (trainingResponse != null) {
    uploadProgress.value = 1.0;
    await Future.delayed(Duration(seconds: 2), () {});
    uploadSuccess.value = true;
  } else {
    uploadSuccess.value = false;
    GetExt.snackBar("Failed to upload training data");
  }
} else {
  uploadSuccess.value = false;
  GetExt.snackBar("Failed to upload video");
}

selectedVideoPath.value = null;
isUploading.value = false;

// Clear compression progress subscription
videoCompressor.dispose();

}

@OverRide
void onClose() {
if (videoCompressor.isCompressing) {
videoCompressor.cancelCompression();
}
videoCompressor.dispose();
super.onClose();
}

Future deleteAllCache() async {
await videoCompressor.deleteAllCache();
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant