react-native-video-processing 1.7.1 → 1.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.travis.yml +1 -1
- package/CODE_OF_CONDUCT.md +46 -0
- package/ISSUE_TEMPLATE.md +21 -0
- package/README.md +94 -39
- package/_config.yml +1 -0
- package/android/build.gradle +26 -10
- package/android/src/main/java/com/shahenlibrary/Events/Events.java +2 -0
- package/android/src/main/java/com/shahenlibrary/RNVideoProcessingPackage.java +1 -2
- package/android/src/main/java/com/shahenlibrary/Trimmer/Trimmer.java +705 -112
- package/android/src/main/java/com/shahenlibrary/Trimmer/TrimmerManager.java +50 -7
- package/android/src/main/java/com/shahenlibrary/VideoPlayer/VideoPlayerView.java +63 -5
- package/android/src/main/java/com/shahenlibrary/VideoPlayer/VideoPlayerViewManager.java +17 -12
- package/android/src/main/java/com/shahenlibrary/interfaces/OnCompressVideoListener.java +8 -0
- package/android/src/main/java/com/shahenlibrary/utils/VideoEdit.java +34 -7
- package/android/src/main/jniLibs/arm64-v8a/libffmpeg.so +0 -0
- package/android/src/main/jniLibs/armeabi-v7a/libffmpeg.so +0 -0
- package/android/src/main/jniLibs/x86/libffmpeg.so +0 -0
- package/android/src/main/jniLibs/x86_64/libffmpeg.so +0 -0
- package/ios/RNVideoProcessing/RCTSwiftBridgeModule.h +29 -0
- package/ios/RNVideoProcessing/RNTrimmerView/ICGVideoTrimmer/ICGVideoTrimmerView.h +5 -0
- package/ios/RNVideoProcessing/RNTrimmerView/ICGVideoTrimmer/ICGVideoTrimmerView.m +48 -13
- package/ios/RNVideoProcessing/RNTrimmerView/RNTrimmerView.h +1 -0
- package/ios/RNVideoProcessing/RNTrimmerView/RNTrimmerView.swift +107 -36
- package/ios/RNVideoProcessing/RNTrimmerView/RNTrimmerViewBridge.m +4 -1
- package/ios/RNVideoProcessing/RNTrimmerView/RNTrimmerViewManager.swift +4 -1
- package/ios/RNVideoProcessing/RNVideoPlayer.swift +89 -45
- package/ios/RNVideoProcessing/RNVideoProcessingBridge.m +3 -1
- package/ios/RNVideoProcessing/RNVideoProcessingManager.swift +16 -10
- package/ios/RNVideoProcessing/RNVideoTrimmer/AVUtilities.swift +81 -0
- package/ios/RNVideoProcessing/RNVideoTrimmer/RNVideoTrimmer.swift +630 -207
- package/ios/RNVideoProcessing/RNVideoTrimmer/RNVideoTrimmerBridge.m +8 -3
- package/ios/RNVideoProcessing/RNVideoTrimmer/SDAVAssetExportSession.m +32 -32
- package/ios/RNVideoProcessing/VideoProcessingGPUFilters.swift +0 -1
- package/ios/RNVideoProcessing.xcodeproj/project.pbxproj +10 -3
- package/lib/ProcessingManager/ProcessingManager.android.js +81 -17
- package/lib/ProcessingManager/ProcessingManager.ios.js +72 -10
- package/lib/ProcessingManager/index.js +1 -1
- package/lib/ProcessingManager/test/ProcessingManager.test.js +24 -0
- package/lib/ProcessingManager/types.android.js +26 -0
- package/lib/ProcessingManager/types.ios.js +21 -3
- package/lib/Trimmer/Trimmer.android.js +4 -6
- package/lib/Trimmer/Trimmer.ios.js +15 -3
- package/lib/Trimmer/test/Trimmer.test.js +6 -3
- package/lib/Trimmer/test/__snapshots__/Trimmer.test.js.snap +4 -1
- package/lib/VideoPlayer/VideoPlayer.android.js +52 -6
- package/lib/VideoPlayer/VideoPlayer.ios.js +13 -10
- package/lib/VideoPlayer/test/__snapshots__/VideoPlayer.test.js.snap +4 -1
- package/lib/utils/index.js +1 -1
- package/lib/utils/test/trimmer.test.js +55 -0
- package/lib/utils/test/utils.test.js +1 -1
- package/lib/utils/trimmer.js +12 -0
- package/package.json +37 -26
- package/.npmignore +0 -60
- package/init/index.js +0 -7
- package/ios/GPUImage/.npmignore +0 -25
- package/yarn.lock +0 -6183
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
package com.shahenlibrary.Trimmer;
|
|
26
26
|
|
|
27
27
|
import android.annotation.TargetApi;
|
|
28
|
+
import android.content.Context;
|
|
28
29
|
import android.graphics.Bitmap;
|
|
29
30
|
import android.graphics.Matrix;
|
|
30
31
|
import android.media.MediaMetadataRetriever;
|
|
@@ -33,15 +34,16 @@ import android.os.Build;
|
|
|
33
34
|
import android.util.Base64;
|
|
34
35
|
import android.util.Log;
|
|
35
36
|
|
|
37
|
+
import com.facebook.react.bridge.ReadableArray;
|
|
36
38
|
import com.facebook.react.bridge.Arguments;
|
|
37
39
|
import com.facebook.react.bridge.Promise;
|
|
38
40
|
import com.facebook.react.bridge.ReactApplicationContext;
|
|
39
41
|
import com.facebook.react.bridge.ReadableMap;
|
|
40
42
|
import com.facebook.react.bridge.WritableArray;
|
|
41
43
|
import com.facebook.react.bridge.WritableMap;
|
|
42
|
-
import com.facebook.react.uimanager.
|
|
44
|
+
import com.facebook.react.uimanager.ThemedReactContext;
|
|
43
45
|
import com.shahenlibrary.Events.Events;
|
|
44
|
-
import com.shahenlibrary.
|
|
46
|
+
import com.shahenlibrary.interfaces.OnCompressVideoListener;
|
|
45
47
|
import com.shahenlibrary.interfaces.OnTrimVideoListener;
|
|
46
48
|
import com.shahenlibrary.utils.VideoEdit;
|
|
47
49
|
|
|
@@ -51,167 +53,758 @@ import java.io.IOException;
|
|
|
51
53
|
|
|
52
54
|
import wseemann.media.FFmpegMediaMetadataRetriever;
|
|
53
55
|
|
|
56
|
+
import java.util.UUID;
|
|
57
|
+
import java.io.FileOutputStream;
|
|
58
|
+
import java.util.Arrays;
|
|
59
|
+
import java.util.ArrayList;
|
|
60
|
+
|
|
61
|
+
import java.io.BufferedReader;
|
|
62
|
+
import java.io.InputStreamReader;
|
|
63
|
+
import java.io.InputStream;
|
|
64
|
+
import java.io.BufferedInputStream;
|
|
65
|
+
import java.io.FileInputStream;
|
|
66
|
+
import android.os.AsyncTask;
|
|
67
|
+
import androidx.annotation.NonNull;
|
|
68
|
+
import androidx.annotation.Nullable;
|
|
69
|
+
|
|
70
|
+
import java.security.NoSuchAlgorithmException;
|
|
71
|
+
import java.security.MessageDigest;
|
|
72
|
+
import java.util.Formatter;
|
|
73
|
+
|
|
74
|
+
|
|
54
75
|
public class Trimmer {
|
|
55
76
|
|
|
56
77
|
private static final String LOG_TAG = "RNTrimmerManager";
|
|
78
|
+
private static final String FFMPEG_FILE_NAME = "libffmpeg.so";
|
|
79
|
+
|
|
80
|
+
private static final int DEFAULT_BUFFER_SIZE = 4096;
|
|
81
|
+
private static final int END_OF_FILE = -1;
|
|
82
|
+
|
|
83
|
+
private static class FfmpegCmdAsyncTaskParams {
|
|
84
|
+
ArrayList<String> cmd;
|
|
85
|
+
final String pathToProcessingFile;
|
|
86
|
+
Context ctx;
|
|
87
|
+
final Promise promise;
|
|
88
|
+
final String errorMessageTitle;
|
|
89
|
+
final OnCompressVideoListener cb;
|
|
90
|
+
|
|
91
|
+
FfmpegCmdAsyncTaskParams(ArrayList<String> cmd, final String pathToProcessingFile, Context ctx, final Promise promise, final String errorMessageTitle, final OnCompressVideoListener cb) {
|
|
92
|
+
this.cmd = cmd;
|
|
93
|
+
this.pathToProcessingFile = pathToProcessingFile;
|
|
94
|
+
this.ctx = ctx;
|
|
95
|
+
this.promise = promise;
|
|
96
|
+
this.errorMessageTitle = errorMessageTitle;
|
|
97
|
+
this.cb = cb;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private static class FfmpegCmdAsyncTask extends AsyncTask<FfmpegCmdAsyncTaskParams, Void, Void> {
|
|
102
|
+
|
|
103
|
+
@Override
|
|
104
|
+
protected Void doInBackground(FfmpegCmdAsyncTaskParams... params) {
|
|
105
|
+
ArrayList<String> cmd = params[0].cmd;
|
|
106
|
+
final String pathToProcessingFile = params[0].pathToProcessingFile;
|
|
107
|
+
Context ctx = params[0].ctx;
|
|
108
|
+
final Promise promise = params[0].promise;
|
|
109
|
+
final String errorMessageTitle = params[0].errorMessageTitle;
|
|
110
|
+
final OnCompressVideoListener cb = params[0].cb;
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
String errorMessageFromCmd = null;
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
// NOTE: 3. EXECUTE "ffmpeg" COMMAND
|
|
117
|
+
String ffmpegInDir = getFfmpegAbsolutePath(ctx);
|
|
118
|
+
cmd.add(0, ffmpegInDir);
|
|
119
|
+
Process p = new ProcessBuilder(cmd).start();
|
|
120
|
+
|
|
121
|
+
BufferedReader input = getOutputFromProcess(p);
|
|
122
|
+
String line = null;
|
|
123
|
+
|
|
124
|
+
StringBuilder sInput = new StringBuilder();
|
|
125
|
+
|
|
126
|
+
while((line=input.readLine()) != null) {
|
|
127
|
+
Log.d(LOG_TAG, "processing ffmpeg");
|
|
128
|
+
System.out.println(sInput);
|
|
129
|
+
sInput.append(line);
|
|
130
|
+
}
|
|
131
|
+
input.close();
|
|
132
|
+
|
|
133
|
+
int errorCode = p.waitFor();
|
|
134
|
+
Log.d(LOG_TAG, "ffmpeg processing completed");
|
|
135
|
+
|
|
136
|
+
if ( errorCode != 0 ) {
|
|
137
|
+
BufferedReader error = getErrorFromProcess(p);
|
|
138
|
+
StringBuilder sError = new StringBuilder();
|
|
139
|
+
|
|
140
|
+
Log.d(LOG_TAG, "ffmpeg error code: " + errorCode);
|
|
141
|
+
while((line=error.readLine()) != null) {
|
|
142
|
+
System.out.println(sError);
|
|
143
|
+
sError.append(line);
|
|
144
|
+
}
|
|
145
|
+
error.close();
|
|
146
|
+
|
|
147
|
+
errorMessageFromCmd = sError.toString();
|
|
148
|
+
}
|
|
149
|
+
} catch (Exception e) {
|
|
150
|
+
errorMessageFromCmd = e.toString();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if ( errorMessageFromCmd != null ) {
|
|
154
|
+
String errorMessage = errorMessageTitle + ": failed. " + errorMessageFromCmd;
|
|
155
|
+
if (cb != null) {
|
|
156
|
+
cb.onError(errorMessage);
|
|
157
|
+
} else if (promise != null) {
|
|
158
|
+
promise.reject(errorMessage);
|
|
159
|
+
}
|
|
160
|
+
} else {
|
|
161
|
+
String filePath = "file://" + pathToProcessingFile;
|
|
162
|
+
if (cb != null) {
|
|
163
|
+
cb.onSuccess(filePath);
|
|
164
|
+
} else if (promise != null) {
|
|
165
|
+
WritableMap event = Arguments.createMap();
|
|
166
|
+
event.putString("source", filePath);
|
|
167
|
+
promise.resolve(event);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
}
|
|
57
175
|
|
|
58
176
|
public static void getPreviewImages(String path, Promise promise, ReactApplicationContext ctx) {
|
|
59
177
|
FFmpegMediaMetadataRetriever retriever = new FFmpegMediaMetadataRetriever();
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
178
|
+
try {
|
|
179
|
+
if (VideoEdit.shouldUseURI(path)) {
|
|
180
|
+
retriever.setDataSource(ctx, Uri.parse(path));
|
|
181
|
+
} else {
|
|
182
|
+
retriever.setDataSource(path);
|
|
183
|
+
}
|
|
65
184
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
185
|
+
WritableArray images = Arguments.createArray();
|
|
186
|
+
int duration = Integer.parseInt(retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION));
|
|
187
|
+
int width = Integer.parseInt(retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
|
|
188
|
+
int height = Integer.parseInt(retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
|
|
189
|
+
int orientation = Integer.parseInt(retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION));
|
|
71
190
|
|
|
72
|
-
|
|
191
|
+
float aspectRatio = (float)width / (float)height;
|
|
73
192
|
|
|
74
|
-
|
|
75
|
-
|
|
193
|
+
int resizeWidth = 200;
|
|
194
|
+
int resizeHeight = Math.round(resizeWidth / aspectRatio);
|
|
76
195
|
|
|
77
|
-
|
|
78
|
-
|
|
196
|
+
float scaleWidth = ((float) resizeWidth) / width;
|
|
197
|
+
float scaleHeight = ((float) resizeHeight) / height;
|
|
79
198
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
199
|
+
Log.d(TrimmerManager.REACT_PACKAGE, "getPreviewImages: \n\tduration: " + duration +
|
|
200
|
+
"\n\twidth: " + width +
|
|
201
|
+
"\n\theight: " + height +
|
|
202
|
+
"\n\torientation: " + orientation +
|
|
203
|
+
"\n\taspectRatio: " + aspectRatio +
|
|
204
|
+
"\n\tresizeWidth: " + resizeWidth +
|
|
205
|
+
"\n\tresizeHeight: " + resizeHeight
|
|
206
|
+
);
|
|
88
207
|
|
|
89
|
-
|
|
208
|
+
Matrix mx = new Matrix();
|
|
90
209
|
|
|
91
|
-
|
|
92
|
-
|
|
210
|
+
mx.postScale(scaleWidth, scaleHeight);
|
|
211
|
+
mx.postRotate(orientation - 360);
|
|
93
212
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
Bitmap currBmp = Bitmap.createScaledBitmap(frame, resizeWidth, resizeHeight, false);
|
|
213
|
+
for (int i = 0; i < duration; i += duration / 10) {
|
|
214
|
+
Bitmap frame = retriever.getFrameAtTime(i * 1000);
|
|
97
215
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
String encoded = "data:image/png;base64," + Base64.encodeToString(byteArray, Base64.DEFAULT);
|
|
103
|
-
images.pushString(encoded);
|
|
104
|
-
}
|
|
216
|
+
if (frame == null) {
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
Bitmap currBmp = Bitmap.createScaledBitmap(frame, resizeWidth, resizeHeight, false);
|
|
105
220
|
|
|
106
|
-
|
|
221
|
+
Bitmap normalizedBmp = Bitmap.createBitmap(currBmp, 0, 0, resizeWidth, resizeHeight, mx, true);
|
|
222
|
+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
223
|
+
normalizedBmp.compress(Bitmap.CompressFormat.PNG, 90, byteArrayOutputStream);
|
|
224
|
+
byte[] byteArray = byteArrayOutputStream.toByteArray();
|
|
225
|
+
String encoded = "data:image/png;base64," + Base64.encodeToString(byteArray, Base64.DEFAULT);
|
|
226
|
+
images.pushString(encoded);
|
|
227
|
+
}
|
|
107
228
|
|
|
108
|
-
|
|
229
|
+
WritableMap event = Arguments.createMap();
|
|
109
230
|
|
|
110
|
-
|
|
111
|
-
|
|
231
|
+
event.putArray("images", images);
|
|
232
|
+
|
|
233
|
+
promise.resolve(event);
|
|
234
|
+
} finally {
|
|
235
|
+
retriever.release();
|
|
236
|
+
}
|
|
112
237
|
}
|
|
113
238
|
|
|
114
239
|
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
|
|
115
240
|
public static void getVideoInfo(String path, Promise promise, ReactApplicationContext ctx) {
|
|
116
241
|
FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
|
|
242
|
+
try {
|
|
243
|
+
if (VideoEdit.shouldUseURI(path)) {
|
|
244
|
+
mmr.setDataSource(ctx, Uri.parse(path));
|
|
245
|
+
} else {
|
|
246
|
+
mmr.setDataSource(path);
|
|
247
|
+
}
|
|
117
248
|
|
|
118
|
-
|
|
119
|
-
mmr.
|
|
120
|
-
|
|
121
|
-
mmr.
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
249
|
+
int duration = Integer.parseInt(mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION));
|
|
250
|
+
int width = Integer.parseInt(mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
|
|
251
|
+
int height = Integer.parseInt(mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
|
|
252
|
+
int orientation = Integer.parseInt(mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION));
|
|
253
|
+
// METADATA_KEY_FRAMERATE returns a float or int or might not exist
|
|
254
|
+
Integer frameRate = VideoEdit.getIntFromString(mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_FRAMERATE));
|
|
255
|
+
// METADATA_KEY_VARIANT_BITRATE returns a int or might not exist
|
|
256
|
+
Integer bitrate = VideoEdit.getIntFromString(mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VARIANT_BITRATE));
|
|
257
|
+
if (orientation == 90 || orientation == 270) {
|
|
258
|
+
width = width + height;
|
|
259
|
+
height = width - height;
|
|
260
|
+
width = width - height;
|
|
261
|
+
}
|
|
128
262
|
|
|
129
|
-
|
|
130
|
-
|
|
263
|
+
WritableMap event = Arguments.createMap();
|
|
264
|
+
WritableMap size = Arguments.createMap();
|
|
131
265
|
|
|
132
|
-
|
|
133
|
-
|
|
266
|
+
size.putInt(Events.WIDTH, width);
|
|
267
|
+
size.putInt(Events.HEIGHT, height);
|
|
134
268
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
269
|
+
event.putMap(Events.SIZE, size);
|
|
270
|
+
event.putInt(Events.DURATION, duration / 1000);
|
|
271
|
+
event.putInt(Events.ORIENTATION, orientation);
|
|
272
|
+
if (frameRate != null) {
|
|
273
|
+
event.putInt(Events.FRAMERATE, frameRate);
|
|
274
|
+
} else {
|
|
275
|
+
event.putNull(Events.FRAMERATE);
|
|
276
|
+
}
|
|
277
|
+
if (bitrate != null) {
|
|
278
|
+
event.putInt(Events.BITRATE, bitrate);
|
|
279
|
+
} else {
|
|
280
|
+
event.putNull(Events.BITRATE);
|
|
281
|
+
}
|
|
138
282
|
|
|
139
|
-
|
|
283
|
+
promise.resolve(event);
|
|
284
|
+
} finally {
|
|
285
|
+
mmr.release();
|
|
286
|
+
}
|
|
287
|
+
}
|
|
140
288
|
|
|
141
|
-
|
|
289
|
+
static void trim(ReadableMap options, final Promise promise, ReactApplicationContext ctx) {
|
|
290
|
+
String source = options.getString("source");
|
|
291
|
+
String startTime = options.getString("startTime");
|
|
292
|
+
String endTime = options.getString("endTime");
|
|
293
|
+
|
|
294
|
+
final File tempFile = createTempFile("mp4", promise, ctx);
|
|
295
|
+
|
|
296
|
+
ArrayList<String> cmd = new ArrayList<String>();
|
|
297
|
+
cmd.add("-y"); // NOTE: OVERWRITE OUTPUT FILE
|
|
298
|
+
|
|
299
|
+
// NOTE: INPUT FILE
|
|
300
|
+
cmd.add("-i");
|
|
301
|
+
cmd.add(source);
|
|
302
|
+
|
|
303
|
+
// NOTE: PLACE ARGUMENTS FOR FFMPEG IN THIS ORDER:
|
|
304
|
+
// 1. "-i" (INPUT FILE)
|
|
305
|
+
// 2. "-ss" (START TIME)
|
|
306
|
+
// 3. "-to" (END TIME) or "-t" (TRIM TIME)
|
|
307
|
+
// OTHERWISE WE WILL LOSE ACCURACY AND WILL GET WRONG CLIPPED VIDEO
|
|
308
|
+
|
|
309
|
+
cmd.add("-ss");
|
|
310
|
+
cmd.add(startTime);
|
|
311
|
+
|
|
312
|
+
cmd.add("-to");
|
|
313
|
+
cmd.add(endTime);
|
|
314
|
+
|
|
315
|
+
cmd.add("-preset");
|
|
316
|
+
cmd.add("ultrafast");
|
|
317
|
+
// NOTE: DO NOT CONVERT AUDIO TO SAVE TIME
|
|
318
|
+
cmd.add("-c:v");
|
|
319
|
+
cmd.add("copy");
|
|
320
|
+
cmd.add("-c:a");
|
|
321
|
+
cmd.add("copy");
|
|
322
|
+
// NOTE: FLAG TO CONVER "AAC" AUDIO CODEC
|
|
323
|
+
// cmd.add("-strict");
|
|
324
|
+
// cmd.add("-2");
|
|
325
|
+
// NOTE: OUTPUT FILE
|
|
326
|
+
cmd.add(tempFile.getPath());
|
|
327
|
+
|
|
328
|
+
executeFfmpegCommand(cmd, tempFile.getPath(), ctx, promise, "Trim error", null);
|
|
142
329
|
}
|
|
143
330
|
|
|
144
|
-
static
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
331
|
+
private static ReadableMap formatWidthAndHeightForFfmpeg(int width, int height, int availableVideoWidth, int availableVideoHeight) {
|
|
332
|
+
// NOTE: WIDTH/HEIGHT FOR FFMpeg NEED TO BE DEVIDED BY 2.
|
|
333
|
+
// OR YOU WILL SEE BLANK WHITE LINES FROM LEFT/RIGHT (FOR CROP), OR CRASH FOR OTHER COMMANDS
|
|
334
|
+
while( width % 2 > 0 && width < availableVideoWidth ) {
|
|
335
|
+
width += 1;
|
|
336
|
+
}
|
|
337
|
+
while( width % 2 > 0 && width > 0 ) {
|
|
338
|
+
width -= 1;
|
|
339
|
+
}
|
|
340
|
+
while( height % 2 > 0 && height < availableVideoHeight ) {
|
|
341
|
+
height += 1;
|
|
342
|
+
}
|
|
343
|
+
while( height % 2 > 0 && height > 0 ) {
|
|
344
|
+
height -= 1;
|
|
345
|
+
}
|
|
148
346
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
event.putString(Events.ERROR_TRIM, message);
|
|
347
|
+
WritableMap sizes = Arguments.createMap();
|
|
348
|
+
sizes.putInt("width", width);
|
|
349
|
+
sizes.putInt("height", height);
|
|
350
|
+
return sizes;
|
|
351
|
+
}
|
|
155
352
|
|
|
156
|
-
|
|
353
|
+
private static ReadableMap getVideoRequiredMetadata(String source, Context ctx) {
|
|
354
|
+
Log.d(LOG_TAG, "getVideoRequiredMetadata: " + source);
|
|
355
|
+
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
|
|
356
|
+
try {
|
|
357
|
+
if (VideoEdit.shouldUseURI(source)) {
|
|
358
|
+
retriever.setDataSource(ctx, Uri.parse(source));
|
|
359
|
+
} else {
|
|
360
|
+
retriever.setDataSource(source);
|
|
157
361
|
}
|
|
158
362
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
363
|
+
int width = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
|
|
364
|
+
int height = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
|
|
365
|
+
int bitrate = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE));
|
|
366
|
+
|
|
367
|
+
Log.d(LOG_TAG, "getVideoRequiredMetadata: " + Integer.toString(width));
|
|
368
|
+
Log.d(LOG_TAG, "getVideoRequiredMetadata: " + Integer.toString(height));
|
|
369
|
+
Log.d(LOG_TAG, "getVideoRequiredMetadata: " + Integer.toString(bitrate));
|
|
370
|
+
|
|
371
|
+
WritableMap videoMetadata = Arguments.createMap();
|
|
372
|
+
videoMetadata.putInt("width", width);
|
|
373
|
+
videoMetadata.putInt("height", height);
|
|
374
|
+
videoMetadata.putInt("bitrate", bitrate);
|
|
375
|
+
return videoMetadata;
|
|
376
|
+
} finally {
|
|
377
|
+
retriever.release();
|
|
378
|
+
}
|
|
379
|
+
}
|
|
163
380
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
381
|
+
public static void compress(String source, ReadableMap options, final Promise promise, final OnCompressVideoListener cb, ThemedReactContext tctx, ReactApplicationContext rctx) {
|
|
382
|
+
Log.d(LOG_TAG, "OPTIONS: " + options.toString());
|
|
383
|
+
|
|
384
|
+
Context ctx = tctx != null ? tctx : rctx;
|
|
385
|
+
|
|
386
|
+
ReadableMap videoMetadata = getVideoRequiredMetadata(source, ctx);
|
|
387
|
+
int videoWidth = videoMetadata.getInt("width");
|
|
388
|
+
int videoHeight = videoMetadata.getInt("height");
|
|
389
|
+
int videoBitrate = videoMetadata.getInt("bitrate");
|
|
390
|
+
|
|
391
|
+
int width = options.hasKey("width") ? (int)( options.getDouble("width") ) : 0;
|
|
392
|
+
int height = options.hasKey("height") ? (int)( options.getDouble("height") ) : 0;
|
|
393
|
+
|
|
394
|
+
if ( width != 0 && height != 0 && videoWidth != 0 && videoHeight != 0 ) {
|
|
395
|
+
ReadableMap sizes = formatWidthAndHeightForFfmpeg(
|
|
396
|
+
width,
|
|
397
|
+
height,
|
|
398
|
+
videoWidth,
|
|
399
|
+
videoHeight
|
|
400
|
+
);
|
|
401
|
+
width = sizes.getInt("width");
|
|
402
|
+
height = sizes.getInt("height");
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
Double minimumBitrate = options.hasKey("minimumBitrate") ? options.getDouble("minimumBitrate") : null;
|
|
406
|
+
Double bitrateMultiplier = options.hasKey("bitrateMultiplier") ? options.getDouble("bitrateMultiplier") : 1.0;
|
|
407
|
+
Boolean removeAudio = options.hasKey("removeAudio") ? options.getBoolean("removeAudio") : false;
|
|
408
|
+
Integer crf = options.hasKey("crf") ? options.getInt("crf") : null;
|
|
409
|
+
Double averageBitrate = videoBitrate / bitrateMultiplier;
|
|
171
410
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
411
|
+
if (minimumBitrate != null) {
|
|
412
|
+
if (averageBitrate < minimumBitrate) {
|
|
413
|
+
averageBitrate = minimumBitrate;
|
|
175
414
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
File mediaFile = new File(mediaSource.replace("file:///", "/"));
|
|
179
|
-
long startTrimFromPos = (long) startMs * 1000;
|
|
180
|
-
long endTrimFromPos = (long) endMs * 1000;
|
|
181
|
-
String[] dPath = mediaSource.split("/");
|
|
182
|
-
StringBuilder builder = new StringBuilder();
|
|
183
|
-
for (int i = 0; i < dPath.length; ++i) {
|
|
184
|
-
if (i == dPath.length - 1) {
|
|
185
|
-
continue;
|
|
415
|
+
if (videoBitrate < minimumBitrate) {
|
|
416
|
+
averageBitrate = videoBitrate * 1.0;
|
|
186
417
|
}
|
|
187
|
-
builder.append(dPath[i]);
|
|
188
|
-
builder.append(File.separator);
|
|
189
418
|
}
|
|
190
|
-
String path = builder.toString().replace("file:///", "/");
|
|
191
419
|
|
|
192
|
-
Log.d(LOG_TAG, "
|
|
420
|
+
Log.d(LOG_TAG, "getVideoRequiredMetadata: averageBitrate - " + Double.toString(averageBitrate));
|
|
421
|
+
|
|
422
|
+
final File tempFile = createTempFile("mp4", promise, ctx);
|
|
423
|
+
|
|
424
|
+
ArrayList<String> cmd = new ArrayList<String>();
|
|
425
|
+
cmd.add("-y");
|
|
426
|
+
cmd.add("-i");
|
|
427
|
+
cmd.add(source);
|
|
428
|
+
cmd.add("-vcodec");
|
|
429
|
+
cmd.add("libx264");
|
|
430
|
+
if(crf != null) {
|
|
431
|
+
cmd.add("-crf");
|
|
432
|
+
cmd.add(Integer.toString(crf));
|
|
433
|
+
}else {
|
|
434
|
+
cmd.add("-b:v");
|
|
435
|
+
cmd.add(Double.toString(averageBitrate / 1000) + "K");
|
|
436
|
+
cmd.add("-bufsize");
|
|
437
|
+
cmd.add(Double.toString(averageBitrate / 2000) + "K");
|
|
438
|
+
}
|
|
439
|
+
if ( width != 0 && height != 0 ) {
|
|
440
|
+
cmd.add("-vf");
|
|
441
|
+
cmd.add("scale=" + Integer.toString(width) + ":" + Integer.toString(height));
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
cmd.add("-preset");
|
|
445
|
+
cmd.add("ultrafast");
|
|
446
|
+
cmd.add("-pix_fmt");
|
|
447
|
+
cmd.add("yuv420p");
|
|
448
|
+
|
|
449
|
+
if (removeAudio) {
|
|
450
|
+
cmd.add("-an");
|
|
451
|
+
}
|
|
452
|
+
cmd.add(tempFile.getPath());
|
|
453
|
+
|
|
454
|
+
executeFfmpegCommand(cmd, tempFile.getPath(), ctx, promise, "compress error", cb);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
static File createTempFile(String extension, final Promise promise, Context ctx) {
|
|
458
|
+
UUID uuid = UUID.randomUUID();
|
|
459
|
+
String imageName = uuid.toString() + "-screenshot";
|
|
460
|
+
|
|
461
|
+
File cacheDir = ctx.getCacheDir();
|
|
462
|
+
File tempFile = null;
|
|
193
463
|
try {
|
|
194
|
-
|
|
195
|
-
} catch
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
464
|
+
tempFile = File.createTempFile(imageName, "." + extension, cacheDir);
|
|
465
|
+
} catch( IOException e ) {
|
|
466
|
+
promise.reject("Failed to create temp file", e.toString());
|
|
467
|
+
return null;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
if (tempFile.exists()) {
|
|
471
|
+
tempFile.delete();
|
|
199
472
|
}
|
|
473
|
+
|
|
474
|
+
return tempFile;
|
|
200
475
|
}
|
|
201
476
|
|
|
202
|
-
static void
|
|
477
|
+
static void getPreviewImageAtPosition(String source, double sec, String format, final Promise promise, ReactApplicationContext ctx) {
|
|
478
|
+
Bitmap bmp = null;
|
|
479
|
+
int orientation = 0;
|
|
203
480
|
FFmpegMediaMetadataRetriever metadataRetriever = new FFmpegMediaMetadataRetriever();
|
|
204
|
-
|
|
481
|
+
try {
|
|
482
|
+
FFmpegMediaMetadataRetriever.IN_PREFERRED_CONFIG = Bitmap.Config.ARGB_8888;
|
|
483
|
+
metadataRetriever.setDataSource(source);
|
|
484
|
+
|
|
485
|
+
bmp = metadataRetriever.getFrameAtTime((long) (sec * 1000000), FFmpegMediaMetadataRetriever.OPTION_CLOSEST);
|
|
486
|
+
if(bmp == null){
|
|
487
|
+
promise.reject("Failed to get preview at requested position.");
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
// NOTE: FIX ROTATED BITMAP
|
|
492
|
+
orientation = Integer.parseInt(metadataRetriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION));
|
|
493
|
+
} finally {
|
|
494
|
+
metadataRetriever.release();
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
if ( orientation != 0 ) {
|
|
498
|
+
Matrix matrix = new Matrix();
|
|
499
|
+
matrix.postRotate(orientation);
|
|
500
|
+
bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
|
|
501
|
+
}
|
|
205
502
|
|
|
206
|
-
Bitmap bmp = metadataRetriever.getFrameAtTime((long) (sec * 1000000));
|
|
207
503
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
208
|
-
bmp.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
|
|
209
|
-
byte[] byteArray = byteArrayOutputStream .toByteArray();
|
|
210
|
-
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
|
|
211
504
|
|
|
212
505
|
WritableMap event = Arguments.createMap();
|
|
213
|
-
|
|
506
|
+
|
|
507
|
+
if ( format == null || (format != null && format.equals("base64")) ) {
|
|
508
|
+
bmp.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
|
|
509
|
+
byte[] byteArray = byteArrayOutputStream .toByteArray();
|
|
510
|
+
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
|
|
511
|
+
|
|
512
|
+
event.putString("image", encoded);
|
|
513
|
+
} else if ( format.equals("JPEG") ) {
|
|
514
|
+
bmp.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
|
|
515
|
+
byte[] byteArray = byteArrayOutputStream.toByteArray();
|
|
516
|
+
|
|
517
|
+
File tempFile = createTempFile("jpeg", promise, ctx);
|
|
518
|
+
|
|
519
|
+
try {
|
|
520
|
+
FileOutputStream fos = new FileOutputStream( tempFile.getPath() );
|
|
521
|
+
|
|
522
|
+
fos.write( byteArray );
|
|
523
|
+
fos.close();
|
|
524
|
+
} catch (java.io.IOException e) {
|
|
525
|
+
promise.reject("Failed to save image", e.toString());
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
WritableMap imageMap = Arguments.createMap();
|
|
530
|
+
imageMap.putString("uri", "file://" + tempFile.getPath());
|
|
531
|
+
|
|
532
|
+
event.putMap("image", imageMap);
|
|
533
|
+
} else {
|
|
534
|
+
promise.reject("Wrong format error", "Wrong 'format'. Expected one of 'base64' or 'JPEG'.");
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
214
537
|
|
|
215
538
|
promise.resolve(event);
|
|
216
539
|
}
|
|
540
|
+
|
|
541
|
+
static void getTrimmerPreviewImages(String source, double startTime, double endTime, int step, String format, final Promise promise, ReactApplicationContext ctx) {
|
|
542
|
+
FFmpegMediaMetadataRetriever retriever = new FFmpegMediaMetadataRetriever();
|
|
543
|
+
try {
|
|
544
|
+
FFmpegMediaMetadataRetriever.IN_PREFERRED_CONFIG = Bitmap.Config.ARGB_8888;
|
|
545
|
+
retriever.setDataSource(source);
|
|
546
|
+
|
|
547
|
+
WritableArray images = Arguments.createArray();
|
|
548
|
+
int duration = Integer.parseInt(retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION));
|
|
549
|
+
int width = Integer.parseInt(retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
|
|
550
|
+
int height = Integer.parseInt(retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
|
|
551
|
+
int orientation = Integer.parseInt(retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION));
|
|
552
|
+
|
|
553
|
+
float aspectRatio = (float)width / (float)height;
|
|
554
|
+
|
|
555
|
+
int resizeHeight = 100;
|
|
556
|
+
int resizeWidth = Math.round(resizeHeight * aspectRatio);
|
|
557
|
+
|
|
558
|
+
float scaleWidth = ((float) resizeWidth) / width;
|
|
559
|
+
float scaleHeight = ((float) resizeHeight) / height;
|
|
560
|
+
|
|
561
|
+
Log.d(TrimmerManager.REACT_PACKAGE, "getPreviewImages: \n\tduration: " + duration +
|
|
562
|
+
"\n\twidth: " + width +
|
|
563
|
+
"\n\theight: " + height +
|
|
564
|
+
"\n\torientation: " + orientation +
|
|
565
|
+
"\n\taspectRatio: " + aspectRatio +
|
|
566
|
+
"\n\tresizeWidth: " + resizeWidth +
|
|
567
|
+
"\n\tresizeHeight: " + resizeHeight
|
|
568
|
+
);
|
|
569
|
+
|
|
570
|
+
Matrix mx = new Matrix();
|
|
571
|
+
|
|
572
|
+
mx.postScale(scaleWidth, scaleHeight);
|
|
573
|
+
mx.postRotate(orientation - 360);
|
|
574
|
+
|
|
575
|
+
for (int i = (int) startTime; i < (int) endTime; i += step ) {
|
|
576
|
+
Bitmap frame = retriever.getScaledFrameAtTime((long) i * 1000000, resizeWidth, resizeHeight);
|
|
577
|
+
|
|
578
|
+
if (frame == null) {
|
|
579
|
+
continue;
|
|
580
|
+
}
|
|
581
|
+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
582
|
+
frame.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
|
|
583
|
+
byte[] byteArray = byteArrayOutputStream.toByteArray();
|
|
584
|
+
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
|
|
585
|
+
images.pushString(encoded);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
WritableMap event = Arguments.createMap();
|
|
590
|
+
|
|
591
|
+
event.putArray("images", images);
|
|
592
|
+
|
|
593
|
+
promise.resolve(event);
|
|
594
|
+
} finally {
|
|
595
|
+
retriever.release();
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
private static BufferedReader getOutputFromProcess(Process p) {
|
|
600
|
+
return new BufferedReader(new InputStreamReader(p.getInputStream()));
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
private static BufferedReader getErrorFromProcess(Process p) {
|
|
604
|
+
return new BufferedReader(new InputStreamReader(p.getErrorStream()));
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
static void crop(String source, ReadableMap options, final Promise promise, ReactApplicationContext ctx) {
|
|
608
|
+
int cropWidth = (int)( options.getDouble("cropWidth") );
|
|
609
|
+
int cropHeight = (int)( options.getDouble("cropHeight") );
|
|
610
|
+
int cropOffsetX = (int)( options.getDouble("cropOffsetX") );
|
|
611
|
+
int cropOffsetY = (int)( options.getDouble("cropOffsetY") );
|
|
612
|
+
|
|
613
|
+
ReadableMap videoSizes = getVideoRequiredMetadata(source, ctx);
|
|
614
|
+
int videoWidth = videoSizes.getInt("width");
|
|
615
|
+
int videoHeight = videoSizes.getInt("height");
|
|
616
|
+
|
|
617
|
+
ReadableMap sizes = formatWidthAndHeightForFfmpeg(
|
|
618
|
+
cropWidth,
|
|
619
|
+
cropHeight,
|
|
620
|
+
// NOTE: MUST CHECK AGAINST "CROPPABLE" WIDTH/HEIGHT. NOT FULL WIDTH/HEIGHT
|
|
621
|
+
videoWidth - cropOffsetX,
|
|
622
|
+
videoHeight - cropOffsetY
|
|
623
|
+
);
|
|
624
|
+
cropWidth = sizes.getInt("width");
|
|
625
|
+
cropHeight = sizes.getInt("height");
|
|
626
|
+
|
|
627
|
+
// TODO: 1) ADD METHOD TO CHECK "IS FFMPEG LOADED".
|
|
628
|
+
// 2) CHECK IT HERE
|
|
629
|
+
// 3) EXPORT THAT METHOD TO "JS"
|
|
630
|
+
|
|
631
|
+
final File tempFile = createTempFile("mp4", promise, ctx);
|
|
632
|
+
|
|
633
|
+
ArrayList<String> cmd = new ArrayList<String>();
|
|
634
|
+
cmd.add("-y"); // NOTE: OVERWRITE OUTPUT FILE
|
|
635
|
+
|
|
636
|
+
// NOTE: INPUT FILE
|
|
637
|
+
cmd.add("-i");
|
|
638
|
+
cmd.add(source);
|
|
639
|
+
|
|
640
|
+
// NOTE: PLACE ARGUMENTS FOR FFMPEG IN THIS ORDER:
|
|
641
|
+
// 1. "-i" (INPUT FILE)
|
|
642
|
+
// 2. "-ss" (START TIME)
|
|
643
|
+
// 3. "-to" (END TIME) or "-t" (TRIM TIME)
|
|
644
|
+
// OTHERWISE WE WILL LOSE ACCURACY AND WILL GET WRONG CLIPPED VIDEO
|
|
645
|
+
|
|
646
|
+
String startTime = options.hasKey("startTime") ? options.getString("startTime") : null;
|
|
647
|
+
if ( startTime != null ) {
|
|
648
|
+
cmd.add("-ss");
|
|
649
|
+
cmd.add(startTime);
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
String endTime = options.hasKey("endTime") ? options.getString("endTime") : null;
|
|
653
|
+
if ( endTime != null ) {
|
|
654
|
+
cmd.add("-to");
|
|
655
|
+
cmd.add(endTime);
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
cmd.add("-vf");
|
|
659
|
+
cmd.add("crop=" + Integer.toString(cropWidth) + ":" + Integer.toString(cropHeight) + ":" + Integer.toString(cropOffsetX) + ":" + Integer.toString(cropOffsetY));
|
|
660
|
+
|
|
661
|
+
cmd.add("-preset");
|
|
662
|
+
cmd.add("ultrafast");
|
|
663
|
+
// NOTE: DO NOT CONVERT AUDIO TO SAVE TIME
|
|
664
|
+
cmd.add("-c:a");
|
|
665
|
+
cmd.add("copy");
|
|
666
|
+
// NOTE: FLAG TO CONVER "AAC" AUDIO CODEC
|
|
667
|
+
cmd.add("-strict");
|
|
668
|
+
cmd.add("-2");
|
|
669
|
+
// NOTE: OUTPUT FILE
|
|
670
|
+
cmd.add(tempFile.getPath());
|
|
671
|
+
|
|
672
|
+
executeFfmpegCommand(cmd, tempFile.getPath(), ctx, promise, "Crop error", null);
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
static void boomerang(String source, final Promise promise, ReactApplicationContext ctx) {
|
|
676
|
+
|
|
677
|
+
final File tempFile = createTempFile("mp4", promise, ctx);
|
|
678
|
+
|
|
679
|
+
ArrayList<String> cmd = new ArrayList<String>();
|
|
680
|
+
cmd.add("-y"); // NOTE: OVERWRITE OUTPUT FILE
|
|
681
|
+
|
|
682
|
+
// NOTE: INPUT FILE
|
|
683
|
+
cmd.add("-i");
|
|
684
|
+
cmd.add(source);
|
|
685
|
+
|
|
686
|
+
// NOTE: DO THE REVERSAL (credit: https://stackoverflow.com/a/42257863/6894670)
|
|
687
|
+
cmd.add("-filter_complex");
|
|
688
|
+
cmd.add("[0:v]reverse,fifo[r];[0:v][r] concat=n=2:v=1 [v]");
|
|
689
|
+
|
|
690
|
+
cmd.add("-map");
|
|
691
|
+
cmd.add("[v]");
|
|
692
|
+
|
|
693
|
+
cmd.add("-preset");
|
|
694
|
+
cmd.add("ultrafast");
|
|
695
|
+
// NOTE: DO NOT CONVERT AUDIO TO SAVE TIME
|
|
696
|
+
cmd.add("-c:a");
|
|
697
|
+
cmd.add("copy");
|
|
698
|
+
// NOTE: FLAG TO CONVER "AAC" AUDIO CODEC
|
|
699
|
+
cmd.add("-strict");
|
|
700
|
+
cmd.add("-2");
|
|
701
|
+
// NOTE: OUTPUT FILE
|
|
702
|
+
cmd.add(tempFile.getPath());
|
|
703
|
+
|
|
704
|
+
executeFfmpegCommand(cmd, tempFile.getPath(), ctx, promise, "Boomerang error", null);
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
static void reverse(String source, final Promise promise, ReactApplicationContext ctx) {
|
|
708
|
+
|
|
709
|
+
final File tempFile = createTempFile("mp4", promise, ctx);
|
|
710
|
+
|
|
711
|
+
ArrayList<String> cmd = new ArrayList<String>();
|
|
712
|
+
cmd.add("-y"); // NOTE: OVERWRITE OUTPUT FILE
|
|
713
|
+
|
|
714
|
+
// NOTE: INPUT FILE
|
|
715
|
+
cmd.add("-i");
|
|
716
|
+
cmd.add(source);
|
|
717
|
+
|
|
718
|
+
// NOTE: DO THE REVERSAL (credit: https://video.stackexchange.com/a/17739)
|
|
719
|
+
cmd.add("-vf");
|
|
720
|
+
cmd.add("reverse");
|
|
721
|
+
|
|
722
|
+
cmd.add("-preset");
|
|
723
|
+
cmd.add("ultrafast");
|
|
724
|
+
// NOTE: DO NOT CONVERT AUDIO TO SAVE TIME
|
|
725
|
+
cmd.add("-c:a");
|
|
726
|
+
cmd.add("copy");
|
|
727
|
+
// NOTE: FLAG TO CONVER "AAC" AUDIO CODEC
|
|
728
|
+
cmd.add("-strict");
|
|
729
|
+
cmd.add("-2");
|
|
730
|
+
// NOTE: OUTPUT FILE
|
|
731
|
+
cmd.add(tempFile.getPath());
|
|
732
|
+
|
|
733
|
+
executeFfmpegCommand(cmd, tempFile.getPath(), ctx, promise, "Reverse error", null);
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
static void merge(ReadableArray videoFiles, String concatCmd, final Promise promise, ReactApplicationContext ctx) {
|
|
737
|
+
final File tempFile = createTempFile("mp4", promise, ctx);
|
|
738
|
+
|
|
739
|
+
Log.d(LOG_TAG, "Merging in progress.");
|
|
740
|
+
|
|
741
|
+
ArrayList<String> cmd = new ArrayList<String>();
|
|
742
|
+
cmd.add("-y"); // NOTE: OVERWRITE OUTPUT FILE
|
|
743
|
+
|
|
744
|
+
for (int i = 0; i < videoFiles.size(); i++) {
|
|
745
|
+
cmd.add("-i");
|
|
746
|
+
cmd.add(videoFiles.getString(i));
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
cmd.add("-filter_complex");
|
|
750
|
+
cmd.add(concatCmd);
|
|
751
|
+
|
|
752
|
+
cmd.add("-map");
|
|
753
|
+
cmd.add("[v]");
|
|
754
|
+
|
|
755
|
+
cmd.add("-preset");
|
|
756
|
+
cmd.add("ultrafast");
|
|
757
|
+
|
|
758
|
+
// NOTE: DO NOT CONVERT AUDIO TO SAVE TIME
|
|
759
|
+
cmd.add("-c:a");
|
|
760
|
+
|
|
761
|
+
cmd.add("copy");
|
|
762
|
+
cmd.add(tempFile.getPath());
|
|
763
|
+
|
|
764
|
+
executeFfmpegCommand(cmd, tempFile.getPath(), ctx, promise, "Merge error", null);
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
static private Void executeFfmpegCommand(@NonNull ArrayList<String> cmd, @NonNull final String pathToProcessingFile, @NonNull Context ctx, @NonNull final Promise promise, @NonNull final String errorMessageTitle, @Nullable final OnCompressVideoListener cb) {
|
|
768
|
+
FfmpegCmdAsyncTaskParams ffmpegCmdAsyncTaskParams = new FfmpegCmdAsyncTaskParams(cmd, pathToProcessingFile, ctx, promise, errorMessageTitle, cb);
|
|
769
|
+
|
|
770
|
+
FfmpegCmdAsyncTask ffmpegCmdAsyncTask = new FfmpegCmdAsyncTask();
|
|
771
|
+
ffmpegCmdAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR ,ffmpegCmdAsyncTaskParams);
|
|
772
|
+
|
|
773
|
+
return null;
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
private static String getFfmpegAbsolutePath(Context ctx) {
|
|
777
|
+
File folder = new File(ctx.getApplicationInfo().nativeLibraryDir);
|
|
778
|
+
return new File(folder, FFMPEG_FILE_NAME).getAbsolutePath();
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
public static String getSha1FromFile(final File file) {
|
|
782
|
+
MessageDigest messageDigest = null;
|
|
783
|
+
try {
|
|
784
|
+
messageDigest = MessageDigest.getInstance("SHA1");
|
|
785
|
+
} catch (NoSuchAlgorithmException e) {
|
|
786
|
+
Log.d(LOG_TAG, "Failed to load SHA1 Algorithm. " + e.toString());
|
|
787
|
+
return "";
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
try {
|
|
791
|
+
try (InputStream is = new BufferedInputStream(new FileInputStream(file))) {
|
|
792
|
+
final byte[] buffer = new byte[1024];
|
|
793
|
+
for (int read = 0; (read = is.read(buffer)) != -1;) {
|
|
794
|
+
messageDigest.update(buffer, 0, read);
|
|
795
|
+
}
|
|
796
|
+
is.close();
|
|
797
|
+
}
|
|
798
|
+
} catch (IOException e) {
|
|
799
|
+
Log.d(LOG_TAG, "Failed to load SHA1 Algorithm. IOException. " + e.toString());
|
|
800
|
+
return "";
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
try (Formatter f = new Formatter()) {
|
|
804
|
+
for (final byte b : messageDigest.digest()) {
|
|
805
|
+
f.format("%02x", b);
|
|
806
|
+
}
|
|
807
|
+
return f.toString();
|
|
808
|
+
}
|
|
809
|
+
}
|
|
217
810
|
}
|