Android从图库中选择视频后压缩视频

时间:2014-10-29 10:39:05

标签: android video video-processing

我在android中面临一个问题,就是在从图库中选择视频后压缩视频,因为当我从图库中选择一个视频并将其转换为Base64字符串时,字符串变得太长,就像3.5mb的字符串一样。什么可以是解决这个问题的方法。可以降低视频效果。 这是代码段。

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

1 个答案:

答案 0 :(得分:1)

你可以使用FFmepg。我通过实现JDK压缩视频来实现。您需要下载库文件并运行它。有关详细信息,请参阅this

在后台任务中压缩视频,效率更高。

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