Java android中的视频压缩库?

Java android中的视频压缩库?,java,android,github,video,video-compression,Java,Android,Github,Video,Video Compression,我正在使用SilicCompressor库进行视频压缩。 我的视频大小是15 MB,压缩视频大小将达到500 kb 由于压缩的大小非常小,当点击压缩视频的播放按钮时,显示错误为无法播放视频 如何获得以MB为单位的压缩大小 这是我压缩后的代码 File imageFile = new File(compressedFilePath); float length = imageFile.length() / 1024f; // Size in KB System.ou

我正在使用SilicCompressor库进行视频压缩。 我的视频大小是15 MB,压缩视频大小将达到500 kb 由于压缩的大小非常小,当点击压缩视频的播放按钮时,显示错误为无法播放视频

如何获得以MB为单位的压缩大小

这是我压缩后的代码

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";

还有其他适合视频压缩的库吗?

您可以使用SilicCompressor吗。这是一个简单的库,效果很好。我已经用过了

尝试实现这一点。如果你有任何错误,请告诉我

编辑:

这样您就可以调用异步任务

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);

}
}  
那现在就叫它

新建VideoCompressAsyncTaskgetActivity.executeselectedVideoPath,f.getPath

selectedVideoPath是视频源路径,f.getPath是目标路径

这样试试

你能用硅压缩机吗。这是一个简单的库,效果很好。我已经用过了

尝试实现这一点。如果你有任何错误,请告诉我

编辑:

这样您就可以调用异步任务

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);

}
}  
那现在就叫它

新建VideoCompressAsyncTaskgetActivity.executeselectedVideoPath,f.getPath

selectedVideoPath是视频源路径,f.getPath是目标路径


这样试试

您可以使用LightCompressor库

调用compressVideo类并向其传递视频路径和所需的压缩视频位置

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);
}

您可以使用LightCompressor库

调用compressVideo类并向其传递视频路径和所需的压缩视频位置

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);
}


我使用了相同的库,压缩的视频给出了错误。仍然得到相同的错误,10Mb的文件被压缩为400KB的文件。我想增加压缩文件的大小。还有更好的方法吗?为什么要压缩10 MB的文件?只需检查长度是否大于20MB,然后进行压缩。还要检查目标路径。文件应以视频格式保存。然后你就可以玩了。无论大小是400 kb还是15 mb。是的,我的目标路径文件格式为.mp4格式,但无法播放视频idk。这是什么错误?我使用了相同的库,压缩视频出现错误。仍然出现相同的错误,10Mb文件压缩为400KB文件。我想增加压缩文件的大小。还有更好的方法吗?为什么要压缩10 MB的文件?只需检查长度是否大于20MB,然后进行压缩。还要检查目标路径。文件应以视频格式保存。然后你就可以玩了。无论大小是400 kb还是15 mb。是的,我的目标路径文件格式为.mp4格式,但无法播放视频idk。这是什么错误?您可能有路径问题。试试我的答案。没有必要发问号。此外,询问库会导致您的问题被关闭为非主题,因为我们这里不处理非站点资源。您可能有路径问题。试试我的答案。没有必要发问号。此外,询问库会导致您的问题被关闭为非主题,因为我们这里不处理非站点资源。压缩需要多少时间?对我来说,5分钟录制的视频压缩大约需要3-4分钟。你可以更改压缩质量以减少压缩时间,也可以将视频最大时间指定为2分钟。如果我降低压缩质量,则不会有太多更改。有没有办法在拍摄过程中减小视频的大小?压缩需要多少时间?对我来说,5分钟录制的视频压缩大约需要3-4分钟。你可以更改压缩质量以减少压缩时间,也可以将视频最大时间指定为2分钟。如果我降低压缩质量,则不会有太多更改。在拍摄过程中减少视频大小的任何方法。。