jonataslaw / jonataslaw/VideoCompress

Video Compressor Didn't Work

Open
#281 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Swift
Stars
262
Forks
344
PR merge metrics
No merged PRs in 30d

Description

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 uploadSuccess = Rx(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();
}
}

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.