VideoConUtil.java
4.96 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.sw.laryngoscope.imageToVideo;
import android.graphics.Bitmap;
import android.view.View;
import androidx.annotation.Nullable;
import com.mobile.ffmpeg.util.FFmpegAsyncUtils;
import com.mobile.ffmpeg.util.FFmpegExecuteCallback;
import com.sw.laryngoscope.utils.Logger;
import com.sw.laryngoscope.utils.ShellUtils;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
public class VideoConUtil {
public String TAG = "VideoConUtil";
public void createVideo(String path, String sourcepath, Bitmap bitmap, int width, int height, int rate) {
ImageToVideoUtil2 imageToVideoUtil2 = new ImageToVideoUtil2();
imageToVideoUtil2.init(width, height);
for(int i = 0; i < rate * 10 * 1000 / 1000; i++) {
imageToVideoUtil2.drawframe(bitmap, i);
if ( mVideoCallback != null ) {
mVideoCallback.onVideoProgress( Math.round(i * 100 / ( rate *10.0f )) );
}
}
imageToVideoUtil2.drainEnd();
String listPath = "/sdcard/videolist.txt";
BufferedWriter writer = null;
try {
if ( !(new File(listPath)).exists() ) {
(new File(listPath)).createNewFile();
}
writer = new BufferedWriter(new FileWriter(listPath));
writer.write("file '/sdcard/outputbitmap.mp4'\n");
writer.write("file '"+ sourcepath + "'");
writer.close();
} catch (IOException e1) {
e1.printStackTrace();
}
/*ShellUtils.execCommand("echo " + "file \'/sdcard/outputbitmap.mp4\'\nfile \'"
+ sourcepath + "\'" + " > /sdcard/videolist.txt", true);*/
String[] cmd = new String[9];
cmd[0] ="-f";
cmd[1] ="concat";
cmd[2] ="-safe";
cmd[3] ="0";
cmd[4] = "-i";
cmd[5] = "/sdcard/videolist.txt";
cmd[6] = "-c";
cmd[7] = "copy";
cmd[8] = path;
FFmpegAsyncUtils asyncTask = new FFmpegAsyncUtils();
asyncTask.setCallback(new FFmpegExecuteCallback() {
@Override
public void onFFmpegStart() {}
@Override
public void onFFmpegSucceed(@Nullable String executeOutput) {
Logger.d(TAG, "=====concact====2 ");
if ( mVideoCallback != null ) {
mVideoCallback.onVideoSuc("");
}
}
@Override
public void onFFmpegFailed(@Nullable String executeOutput) {
Logger.d(TAG, "=====concact====3 ");
if ( mVideoCallback != null ) {
mVideoCallback.onVideoFail();
}
}
@Override
public void onFFmpegProgress(@Nullable Integer progress) {
Logger.d(TAG, "=====progress====3 ");
if ( mVideoCallback != null ) {
mVideoCallback.onVideoProgress( 100 );
}
}
@Override
public void onFFmpegCancel() {
Logger.d(TAG, "=====concact====03 ");
if ( mVideoCallback != null ) {
mVideoCallback.onVideoFail();
}
}
});
asyncTask.execute(cmd);
}
public Bitmap saveViewBitmap(View view, int dstWidth, int dstHeight){
view.setDrawingCacheEnabled(true);
Bitmap bitmap = null;
try{
if ( null != view.getDrawingCache() ) {
bitmap = Bitmap.createScaledBitmap(
view.getDrawingCache(), dstWidth, dstHeight, false );
Logger.d("saveViewBitmap ");
}
} catch ( OutOfMemoryError e ) {
e.printStackTrace();
Logger.d("saveViewBitmap " + e.toString());
} finally {
view.setDrawingCacheEnabled(false);
view.destroyDrawingCache();
}
return bitmap;
}
public void saveImage(Bitmap bmp, String path, String name) {
FileOutputStream out = null;
try {
File folder = new File(path);
if ( !folder.exists() ) {
folder.mkdirs();
}
File file = new File(folder, name);
out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
Logger.d("saveImage ");
} catch (Exception e) {
Logger.d("saveImage " + e.toString());
} finally {
try {
if (out != null) {
out.close();
}
} catch (Exception e) {
}
}
}
public VideoCallback mVideoCallback;
public void setVideoCallback(VideoCallback pdfCallback) {
this.mVideoCallback = pdfCallback;
}
public interface VideoCallback {
void onVideoSuc(String path);
void onVideoFail();
void onVideoProgress(int pro);
}
}