视频压缩用FFmpeg实在是太慢了,26mb的视频要30秒。后来采用了这个方案,需要15秒左右:https://github.com/Tourenathan-G5organisation/SiliCompressor
视频拍摄完成后,开启一个service服务去压缩,压缩任务用list装载,是串行的,一个任务结束后继续下一个任务。
Intent intent = new Intent(this, DownloadService.class);
intent.putExtra(DownloadService.OUTPUT, target);
intent.putExtra(DownloadService.DIR, dir);
intent.putExtra(DownloadService.INPUT, inputDir);
intent.putExtra(DownloadService.HUAN_HAO, zuhuanhao); intent.setAction(DownloadService.ACTION_DOWNLOAD);
startService(intent);
service
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.util.Log;
import com.chinaxinge.backstage.R;
import com.chinaxinge.backstage.surface.common.MasterApplication;
import com.chinaxinge.backstage.surface.shelter.activity.KSListActivity;
import com.chinaxinge.backstage.utility.AlbumNotifyHelper;
import com.iceteck.silicompressorr.SiliCompressor;
import com.squareup.picasso.Picasso;
import com.yumore.common.utility.FileUtils;
import com.yumore.common.utility.ListUtils;
import com.yumore.common.utility.LogUtils;
import java.io.File;
import java.io.IOException;
import androidx.annotation.Nullable;
import io.microshow.rxffmpeg.RxFFmpegInvoke;
import io.microshow.rxffmpeg.RxFFmpegSubscriber;
public class DownloadService extends Service {
public final static String INPUT = "input";
public final static String DIR = "dir";
public final static String OUTPUT = "output";
public final static String HUAN_HAO = "huan_hao";
/**
* 广播
*/
private LocalBroadcastManager mLocalBroadcastManager;
public static final String IMAGE = "iamge_url";
public static final String RECEIVER_ACTION = "com.zhouwei.simpleservice";
private static final String TAG = "DownloadService";
public static final String ACTION_START_SERVICER = "com.zhouwei.startservice";
public static final String ACTION_DOWNLOAD = "com.zhouwei.startdownload";
private Boolean isRunnungTask = false;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "服务启动...");
handleCommand(intent);
return START_STICKY;
}
private void handleCommand(Intent intent) {
if (intent != null) {
String input = intent.getStringExtra(INPUT);
String dir = intent.getStringExtra(DIR);
String output = intent.getStringExtra(OUTPUT);
String huanhao = intent.getStringExtra(HUAN_HAO);
LogUtils.e("onHandleIntent 开启下载任务 \ninput=" + input + "\ndir=" + dir + "\nouput=" + output + "\nhuanhao=" + huanhao);
if (!TextUtils.isEmpty(input)) {
CompressTask compressTask = new CompressTask(input, dir, output, huanhao);
boolean hasSameTask = false;
//防止添加重复任务
for (int i = 0; i < MasterApplication.getmTasks().size(); i++) {
CompressTask mTask = MasterApplication.getmTasks().get(i);
if (mTask.getInput().equals(input)) {
hasSameTask = true;
LogUtils.i("已存在相同任务 input="+input);
}
}
if (!hasSameTask) {
//添加一个任务
MasterApplication.getmTasks().add(compressTask);
if (!isRunnungTask) {
isRunnungTask = true;
new VideoCompressAsyncTask(DownloadService.this, compressTask).execute(input, dir);
}
}
}
}
}
class VideoCompressAsyncTask extends AsyncTask {
Context mContext;
CompressTask task;
public VideoCompressAsyncTask(Context context, CompressTask tt) {
mContext = context;
task = tt;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... paths) {
LogUtils.e("paths[0]源地址=" + paths[0]);
LogUtils.e("paths[1]输出地址=" + paths[1]);
String filePath = null;
try {
int outWidth = 0;
int outHeight = 0;
//默认竖屏
//1080p
outWidth = 1080;
outHeight = 1920;
filePath = SiliCompressor.with(DownloadService.this)
.compressVideo(paths[0], paths[1], outWidth, outHeight, 2300000);
} catch (
Exception e) {
e.printStackTrace();
}
return filePath;
}
@Override
protected void onPostExecute(String compressedFilePath) {
super.onPostExecute(compressedFilePath);
LogUtils.e("压缩成功,视频保存地址:" + compressedFilePath);
String gpname = MasterApplication.getInstance().getCurrentUser().shopname;
String dir = Environment.getExternalStorageDirectory().toString() + File.separator + "xx网" + File.separator + "视频" + File.separator + "" + gpname;
final String destPath = dir + File.separator + "" + task.getHuanhao() + ".mp4";
if (FileUtils.copyFile(compressedFilePath, destPath)) {
LogUtils.i("压缩视频已移动到目录之下:" + destPath);
if (FileUtils.deleteFile(compressedFilePath)) {
LogUtils.i("临时视频已删除:" + compressedFilePath);
}
sendThreadStatus("任务结束", 100);
}
MasterApplication.getmTasks().remove(task);
if (!ListUtils.isEmpty(MasterApplication.getmTasks())) {
CompressTask task1 = MasterApplication.getmTasks().get(0);
//开始下一个压缩任务
new VideoCompressAsyncTask(DownloadService.this, task1).execute(task1.getInput(), task1.getDir());
} else {
//所有任务结束
isRunnungTask = false;
LogUtils.e("所有任务结束");
}
Log.i("Silicompressor", "Path: " + compressedFilePath);
}
}
}
任务列表放在application里面:
public static ArrayList mTasks = new ArrayList();
public static ArrayList getmTasks() {
return mTasks;
}