VideoUtils.java
4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.chudiangameplay.android.util;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.SystemClock;
import com.bumptech.glide.gifencoder.AnimatedGifEncoder;
import com.chudiangameplay.android.tool.Log;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import wseemann.media.FFmpegMediaMetadataRetriever;
/**
* Created by Administrator on 2019/2/20.
*/
public class VideoUtils {
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
public static File file;
/**
* 获取视频的第一帧图片
*/
public static void getImageForVideo(Context context, String videoPath, OnLoadVideoImageListener listener) {
LoadVideoImageTask task = new LoadVideoImageTask(context, listener);
task.execute(videoPath);
}
public static class LoadVideoImageTask extends AsyncTask<String, Integer, String> {
private OnLoadVideoImageListener listener;
Context context;
List<Bitmap> list = new ArrayList<>();
long maxTime = 1500;
int fps = 150;
// int fps = 1000;
public LoadVideoImageTask(Context context, OnLoadVideoImageListener listener) {
this.context = context;
this.listener = listener;
}
@Override
protected String doInBackground(String... params) {
FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
String path = params[0];
if (path.startsWith("http"))
//获取网络视频第一帧图片
mmr.setDataSource(path, new HashMap());
else
//本地视频
mmr.setDataSource(path);
//截取视频帧
for (long i = 0; i < maxTime; i = i + fps) {
Log.i("test", "截帧时间:" + (i * 1000));
Bitmap bitmap = mmr.getFrameAtTime(i * 1000, FFmpegMediaMetadataRetriever.OPTION_CLOSEST);
String filepath = Environment.getExternalStorageDirectory() + "/themb_video" + i + ".jpg";
BitmapUtil.saveBitmapFile(context, bitmap, filepath, 20);
Bitmap bitmap1 = BitmapUtil.getSmallBitmap(filepath, 180, 320);
list.add(bitmap1);
}
mmr.release();
return createGif("temp", list, fps);
}
@Override
protected void onPostExecute(String path) {
super.onPostExecute(path);
if (listener != null) {
listener.onLoadImage(path, list);
}
}
}
public interface OnLoadVideoImageListener {
void onLoadImage(String gifPath, List<Bitmap> bitmaps);
}
public static String createGif(String filename, List<Bitmap> list, int fps) {
String path = Environment.getExternalStorageDirectory().getPath() + "/video_gif/" + filename + SystemClock.currentThreadTimeMillis() + ".gif";
try {
Log.i("test", "生成GIF:开始");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
AnimatedGifEncoder localAnimatedGifEncoder = new AnimatedGifEncoder();
localAnimatedGifEncoder.start(baos);//start
localAnimatedGifEncoder.setRepeat(0);//设置生成gif的开始播放时间。0为立即开始播放
localAnimatedGifEncoder.setDelay(fps);
if (list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
localAnimatedGifEncoder.addFrame(list.get(i));
}
}
localAnimatedGifEncoder.finish();//finish
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/video_gif");
if (!file.exists()) file.mkdir();
try {
FileOutputStream fos = new FileOutputStream(path);
baos.writeTo(fos);
baos.flush();
fos.flush();
baos.close();
fos.close();
} catch (Exception e) {
}
Log.i("test", "生成GIF:结束");
} catch (Exception e) {
}
return path;
}
}