2014-10-29 118 views
3

我在android中遇到了一个问题,那就是在从视频库中选择视频后压缩视频,因为当我从视频库中选择视频并将其转换为Base64字符串时,字符串变得太长3.5MB只是string.What可以解决这个问题。它可以降低视频结果。 这是代码片段。从视频库中选择视频后,Android压缩视频

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

“什么可以解决这个问题” - 不要将其转换为Base64。 – CommonsWare 2014-10-29 10:45:50

+0

但我必须将其上传到服务器,并且该API采用base64字符串并在iOS中工作。可以使用其他解决方案吗? – DevUsman 2014-10-29 11:34:41

+0

您是否找到解决方案?我也有同样的问题 – 2014-12-01 09:44:36

回答

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