gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:12:10 25 4
gpt4 key购买 nike

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

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

最佳答案

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

并且在Background Task中对视频进行压缩,效率会更高。

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

关于从图库中选择视频后,Android 压缩视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26628206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com