1

I am using Silicompressor library for video compression. My video size is 15 MB and compressed video size is coming to 500 kb since the compressed size is very very small and when clicked on play button of compressed video it shows an error as "Failed to play video."

How do I get a compressed size in MB?

Here is my code after compressing

File imageFile = new File(compressedFilePath);
        float length = imageFile.length() / 1024f; // Size in KB
        System.out.println("length = " + length);
        String value;
        if (length >= 1024)
            value = length / 1024f + " MB";
        else
            value = length + " KB";

Any other alternative library which works well for video compression ?

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
Josss
  • 229
  • 1
  • 4
  • 19
  • You might have path problem. Try my answer. – Saveen Apr 20 '20 at 05:58
  • there's no need to spam question marks. also, asking for libraries will result in your question being closed as off-topic, because we don't deal with off-site resources here. – a_local_nobody Apr 20 '20 at 10:48

2 Answers2

6

you can use LightCompressor library

LightCompressor

call compressVideo class and pass to it the video path and the desired compressed video location

selectedVideo = data.getData();

compressVideo(getMediaPath(QabaelAdd.this,selectedVideo));

fpath=saveVideoFile(QabaelAdd.this,path).getPath(); //the compressed video location 


private static File saveVideoFile(Context context, String filePath) throws IOException {
    if (filePath != null) {
        File videoFile = new File(filePath);
        String videoFileName = "" + System.currentTimeMillis() + '_' + videoFile.getName();
        String folderName = Environment.DIRECTORY_MOVIES;
        if (Build.VERSION.SDK_INT < 30) {
            File downloadsPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            File desFile = new File(downloadsPath, videoFileName);
            if (desFile.exists()) {
                desFile.delete();
            }

            try {
                desFile.createNewFile();
            } catch (IOException var61) {
                var61.printStackTrace();
            }

            return desFile;
        }

        ContentValues var10 = new ContentValues();
        boolean var11 = false;
        boolean var12 = false;
        var10.put("_display_name", videoFileName);
        var10.put("mime_type", "video/mp4");
        var10.put("relative_path", folderName);
        var10.put("is_pending", 1);
        ContentValues values = var10;
        Uri collection = MediaStore.Video.Media.getContentUri("external_primary");
        Uri fileUri = context.getContentResolver().insert(collection, values);
        Void var10000;
        if (fileUri != null) {
            boolean var13 = false;
            Closeable var18 = (Closeable)context.getContentResolver().openFileDescriptor(fileUri, "rw");
            boolean var19 = false;
            boolean var20 = false;
            Throwable var73 = (Throwable)null;

            try {
                ParcelFileDescriptor descriptor = (ParcelFileDescriptor)var18;
                if (descriptor != null) {
                    boolean var24 = false;
                    boolean var25 = false;
                    Closeable var28 = (Closeable)(new FileOutputStream(descriptor.getFileDescriptor()));
                    boolean var29 = false;
                    boolean var30 = false;
                    Throwable var74 = (Throwable)null;

                    try {
                        FileOutputStream out = (FileOutputStream)var28;
                        Closeable var33 = (Closeable)(new FileInputStream(videoFile));
                        boolean var34 = false;
                        boolean var35 = false;
                        Throwable var76 = (Throwable)null;

                        try {
                            FileInputStream inputStream = (FileInputStream)var33;
                            byte[] buf = new byte[4096];

                            while(true) {
                                int sz = inputStream.read(buf);
                                if (sz <= 0) {
                                    Unit var77 = Unit.INSTANCE;
                                    break;
                                }

                                out.write(buf, 0, sz);
                            }
                        } catch (Throwable var62) {
                            var76 = var62;
                            throw var62;
                        } finally {
                            //CloseableKt.closeFinally(var33, var76);
                        }

                        Unit var75 = Unit.INSTANCE;
                    } catch (Throwable var64) {
                        var74 = var64;
                        throw var64;
                    } finally {
                        //CloseableKt.closeFinally(var28, var74);
                    }

                    Unit var72 = Unit.INSTANCE;
                } else {
                    var10000 = null;
                }
            } catch (Throwable var66) {
                var73 = var66;
                throw var66;
            } finally {
                //CloseableKt.closeFinally(var18, var73);
            }

            values.clear();
            values.put("is_pending", 0);
            context.getContentResolver().update(fileUri, values, (String)null, (String[])null);
            return new File(QabaelAdd.getMediaPath(context, fileUri));
        }

        var10000 = (Void)null;
    }

    return null;
}





@NotNull
public static String getMediaPath(@NotNull Context context, @NotNull Uri uri) throws IOException {
    Intrinsics.checkNotNullParameter(context, "context");
    Intrinsics.checkNotNullParameter(uri, "uri");
    ContentResolver resolver = context.getContentResolver();
    String[] projection = new String[]{"_data"};
    Cursor cursor = (Cursor)null;

    String var30;
    try {
        File file;
        String var57;
        try {
            cursor = resolver.query(uri, projection, (String)null, (String[])null, (String)null);
            if (cursor != null) {
                int columnIndex = cursor.getColumnIndexOrThrow("_data");
                cursor.moveToFirst();
                var57 = cursor.getString(columnIndex);
                Intrinsics.checkNotNullExpressionValue(var57, "cursor.getString(columnIndex)");
            } else {
                var57 = "";
            }
            return var57;
        } catch (Exception var53) {

            String filePath = context.getApplicationInfo().dataDir + File.separator + System.currentTimeMillis();
            file = new File(filePath);
            InputStream var10000 = resolver.openInputStream(uri);
            if (var10000 != null) {
                Closeable var13 = (Closeable)var10000;

                InputStream inputStream = (InputStream)var13;
                Closeable var18 = (Closeable)(new FileOutputStream(file));
                FileOutputStream outputStream = (FileOutputStream)var18;
                byte[] buf = new byte[4096];

                while(true) {
                    int var25 = inputStream.read(buf);
                    if (var25 <= 0) {
                        break;
                    }
                    outputStream.write(buf, 0, var25);
                }

            }
        }
        var57 = file.getAbsolutePath();
        Intrinsics.checkNotNullExpressionValue(var57, "file.absolutePath");
        var30 = var57;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return var30;
}




private void compressVideo(String path){
    VideoCompressor.start(path,fpath , new CompressionListener() {
        @Override
        public void onStart() {
            // Compression start
        }

        @Override
        public void onSuccess() {
            // On Compression success
            Uri uploadUri = Uri.fromFile(new File(fpath));
            Log.e("is dir", String.valueOf(new File(fpath).isDirectory()));

            uploadVideoMethod(uploadUri); //upload the video 
        }

        @Override
        public void onFailure(String failureMessage) {
            // On Failure
            Log.e("fail", failureMessage);
            Toast.makeText(QabaelAdd.this, "failed to compress video", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onProgress(float v) {
            // Update UI with progress value
            runOnUiThread(new Runnable() {
                public void run() {
                    progressDialog.setMessage(" جاري تهيئة الفيديو "+String.valueOf(Math.round(v))+"%");
                    Log.e("progress", String.valueOf(v));
                }
            });
        }

        @Override
        public void onCancelled() {
            // On Cancelled
        }
    }, VideoQuality.MEDIUM, false, false);
}
Mohammed Ashraf
  • 110
  • 1
  • 6
0

Can you go with SiliCompressor. It's nice and simple library and give good result. I have used it.

Try to implement this. If you get any error let me know.

https://github.com/Tourenathan-G5organisation/SiliCompressor

Edit:

This way you can call the async task.

class VideoCompressAsyncTask extends AsyncTask<String, String, String> {

Context mContext;

public VideoCompressAsyncTask(Context context) {
    mContext = context;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected String doInBackground(String... paths) {
    String filePath = null;
    try {
        filePath = SiliCompressor.with(mContext).compressVideo(paths[0], paths[1]);

    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    return filePath;

}

@Override
protected void onPostExecute(String compressedFilePath) {
    super.onPostExecute(compressedFilePath);

    File videoFile = new File(compressedFilePath);

}
}  

Then now call it.

new VideoCompressAsyncTask(getActivity()).execute(selectedVideoPath, f.getPath());

selectedVideoPath is the video source path and f.getPath() is the destination path.

Try this way.

Saveen
  • 4,120
  • 14
  • 38
  • 41
  • I have used this same library, the compressed video is giving error.. – Josss Apr 20 '20 at 05:44
  • Still getting same error and 10Mb FILE is compressed to 400KB file. I want to increase the compressed file size. Any better way to do so. – Josss Apr 20 '20 at 06:19
  • why you do compress 10 MB file? Just put check if length is greater than 20MB only then do compression. – Saveen Apr 20 '20 at 06:24
  • Also check your destination path. File should save in video format. Then you can play it. whatever size is 400 kb or 15 mb. – Saveen Apr 20 '20 at 06:26
  • Yes my destination path file format is in .mp4 format but not able to play the video idk what error is this – Josss Apr 20 '20 at 06:29
  • Can you use the same example which provided by SiliCompressor? Then check it. It's simple thing. I'm not sure where you are lacking. You need to debug all things. surely this is the path issue. – Saveen Apr 20 '20 at 06:37