Android compresses video after selecting video from gallery

I am facing an issue in android that compresses the video after selecting it from the gallery, because when I select a video from the gallery and convert it to Base64 string, the string gets too long as 3.5MB only strings. What could be the solution to this. Its ok to degrade the video result. Here is a code snippet.

        Uri selectedVideoUri = data.getData();
        String selectedPath = getPath(selectedVideoUri,"Video");            
        encodedVideo=convertFileToString(selectedPath);       

     public String convertFileToString(String pathOnSdCard){
    String strFile=null;        
    File file=new File(pathOnSdCard);

    try {

        byte[] data = FileUtils.readFileToByteArray(file);//Convert any file, image or video into byte array
        strFile = Base64.encodeToString(data, Base64.NO_WRAP);//Convert byte array into string
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        File myFile = new File("/sdcard/mysdfile.txt");
        myFile.createNewFile();
        FileOutputStream fOut = new FileOutputStream(myFile);
        OutputStreamWriter myOutWriter = 
                                new OutputStreamWriter(fOut);
        myOutWriter.append(strFile);
        myOutWriter.close();
        fOut.close();
        Toast.makeText(getBaseContext(),
                "Done writing SD 'mysdfile.txt'",
                Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        Toast.makeText(getBaseContext(), e.getMessage(),
                Toast.LENGTH_SHORT).show();
    }

    return strFile;
}        private String getPath(Uri uri,String str) {
    String[] projection = { MediaStore.Video.Media.DATA, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DURATION}; 
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    cursor.moveToFirst(); 
    String filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
    //      int fileSize = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
    //      long duration = Time.MILLISECONDS.toSeconds(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION)));


    //some extra potentially useful data to help with filtering if necessary
    //System.out.println("size: " + fileSize);
    System.out.println("path: " + filePath);
    // System.out.println("duration: " + duration);

    return filePath;
}

      

+3


source to share


1 answer


You can use FFmepg. I have achieved the implementation of the JDK for video compression. You need to download the library file and run it. For more information see this

And compress the video in the Background task which will be more efficient.



 class VideoCompressing extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        dialog = ProgressDialog.show(Sentinel.this, "", "Compressing, Please Wait...", true);
        dialog.setMax(100);
        dialog.show();
    }
    @Override
    protected String doInBackground(String... key) {


        String newText = key[0];

        LoadJNI vk = new LoadJNI();
        try {
            File sdCardDirectory = new File(Environment.getExternalStorageDirectory() + File.separator + "DTP_Video");
            try {

                if (!sdCardDirectory.exists()) {
                    sdCardDirectory.mkdirs();
                }
                dateofpic=currentDateFormat();
                String videoDestiPath = "VID_"+dateofpic+".mp4";


                File image = new File(sdCardDirectory, videoDestiPath);
                afterCompressPath = image.getAbsolutePath();

            }
            catch (Exception e)
            {
                e.printStackTrace();

            }

            String workFolder = getApplicationContext().getFilesDir().getAbsolutePath();

            String[] complexCommand = {"ffmpeg","-y" ,"-i",newText,
                    "-strict","experimental","-s", "460x320","-r","25", "-vcodec", "mpeg4", "-b", "1500k",
                    "-ab","48000", "-ac", "2", "-ar", "22050", afterCompressPath};



            vk.run(complexCommand, workFolder, getApplicationContext());
            Log.i("test", "ffmpeg4android finished successfully");


        }

        catch (Throwable e) {
            Log.e("test", "vk run exception.", e);
        }

        runOnUiThread(new Runnable() {
            public void run() {

            }
        });

        return null;
    }

      

+1


source







All Articles