react-native-video-trim 0.0.1 → 1.0.1

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.
Files changed (39) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +206 -0
  3. package/android/build.gradle +105 -0
  4. package/android/gradle.properties +5 -0
  5. package/android/src/main/AndroidManifest.xml +2 -0
  6. package/android/src/main/AndroidManifestDeprecated.xml +3 -0
  7. package/android/src/main/java/com/videotrim/VideoTrimModule.java +315 -0
  8. package/android/src/main/java/com/videotrim/VideoTrimPackage.java +28 -0
  9. package/android/src/main/java/com/videotrim/adapters/VideoTrimmerAdapter.java +60 -0
  10. package/android/src/main/java/com/videotrim/interfaces/IVideoTrimmerView.java +5 -0
  11. package/android/src/main/java/com/videotrim/interfaces/VideoTrimListener.java +9 -0
  12. package/android/src/main/java/com/videotrim/utils/StorageUtil.java +277 -0
  13. package/android/src/main/java/com/videotrim/utils/VideoTrimmerUtil.java +109 -0
  14. package/android/src/main/java/com/videotrim/widgets/RangeSeekBarView.java +534 -0
  15. package/android/src/main/java/com/videotrim/widgets/SpacesItemDecoration2.java +33 -0
  16. package/android/src/main/java/com/videotrim/widgets/VideoTrimmerView.java +444 -0
  17. package/android/src/main/java/com/videotrim/widgets/ZVideoView.java +48 -0
  18. package/android/src/main/res/drawable/ic_video_pause_black.png +0 -0
  19. package/android/src/main/res/drawable/ic_video_play_black.png +0 -0
  20. package/android/src/main/res/drawable/ic_video_thumb_handle.png +0 -0
  21. package/android/src/main/res/drawable/icon_seek_bar.png +0 -0
  22. package/android/src/main/res/layout/video_thumb_item_layout.xml +16 -0
  23. package/android/src/main/res/layout/video_trimmer_view.xml +148 -0
  24. package/android/src/main/res/values/colors.xml +17 -0
  25. package/android/src/main/res/values/strings.xml +14 -0
  26. package/ios/VideoTrim-Bridging-Header.h +2 -0
  27. package/ios/VideoTrim.mm +10 -0
  28. package/ios/VideoTrim.swift +170 -0
  29. package/ios/VideoTrim.xcodeproj/project.pbxproj +283 -0
  30. package/ios/VideoTrim.xcodeproj/project.xcworkspace/contents.xcworkspacedata +4 -0
  31. package/lib/commonjs/index.js +32 -0
  32. package/lib/commonjs/index.js.map +1 -0
  33. package/lib/module/index.js +25 -0
  34. package/lib/module/index.js.map +1 -0
  35. package/lib/typescript/index.d.ts +7 -0
  36. package/lib/typescript/index.d.ts.map +1 -0
  37. package/package.json +158 -7
  38. package/react-native-video-trim.podspec +41 -0
  39. package/src/index.tsx +35 -0
@@ -0,0 +1,60 @@
1
+ package com.videotrim.adapters;
2
+
3
+
4
+ import android.content.Context;
5
+ import android.graphics.Bitmap;
6
+ import android.view.LayoutInflater;
7
+ import android.view.View;
8
+ import android.view.ViewGroup;
9
+ import android.widget.ImageView;
10
+ import android.widget.LinearLayout;
11
+
12
+ import androidx.annotation.NonNull;
13
+ import androidx.recyclerview.widget.RecyclerView;
14
+
15
+ import com.videotrim.R;
16
+ import com.videotrim.utils.VideoTrimmerUtil;
17
+
18
+ import java.util.ArrayList;
19
+ import java.util.List;
20
+
21
+ public class VideoTrimmerAdapter extends RecyclerView.Adapter {
22
+ private List<Bitmap> mBitmaps = new ArrayList<>();
23
+ private LayoutInflater mInflater;
24
+ private Context context;
25
+
26
+ public VideoTrimmerAdapter(Context context) {
27
+ this.context = context;
28
+ this.mInflater = LayoutInflater.from(context);
29
+ }
30
+
31
+ @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
32
+ return new TrimmerViewHolder(mInflater.inflate(R.layout.video_thumb_item_layout, parent, false));
33
+ }
34
+
35
+ @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
36
+ ((TrimmerViewHolder) holder).thumbImageView.setImageBitmap(mBitmaps.get(position));
37
+ }
38
+
39
+ @Override public int getItemCount() {
40
+ return mBitmaps.size();
41
+ }
42
+
43
+ public void addBitmaps(Bitmap bitmap) {
44
+ mBitmaps.add(bitmap);
45
+ notifyDataSetChanged();
46
+ }
47
+
48
+ private final class TrimmerViewHolder extends RecyclerView.ViewHolder {
49
+ ImageView thumbImageView;
50
+
51
+ TrimmerViewHolder(View itemView) {
52
+ super(itemView);
53
+ thumbImageView = itemView.findViewById(R.id.thumb);
54
+ LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) thumbImageView.getLayoutParams();
55
+ layoutParams.width = VideoTrimmerUtil.VIDEO_FRAMES_WIDTH / VideoTrimmerUtil.MAX_COUNT_RANGE;
56
+ thumbImageView.setLayoutParams(layoutParams);
57
+ }
58
+ }
59
+ }
60
+
@@ -0,0 +1,5 @@
1
+ package com.videotrim.interfaces;
2
+
3
+ public interface IVideoTrimmerView {
4
+ void onDestroy();
5
+ }
@@ -0,0 +1,9 @@
1
+ package com.videotrim.interfaces;
2
+
3
+ public interface VideoTrimListener {
4
+ void onStartTrim();
5
+ void onTrimmingProgress(int percentage);
6
+ void onFinishTrim(String url);
7
+ void onError();
8
+ void onCancel();
9
+ }
@@ -0,0 +1,277 @@
1
+ package com.videotrim.utils;
2
+
3
+ import android.Manifest;
4
+ import android.annotation.SuppressLint;
5
+ import android.content.ContentValues;
6
+ import android.content.Context;
7
+ import android.media.MediaScannerConnection;
8
+ import android.net.Uri;
9
+ import android.os.Build;
10
+ import android.os.Environment;
11
+ import android.provider.MediaStore;
12
+ import android.text.TextUtils;
13
+ import android.util.Log;
14
+
15
+ import androidx.fragment.app.FragmentActivity;
16
+
17
+ import com.facebook.react.bridge.ReactApplicationContext;
18
+ import com.permissionx.guolindev.PermissionX;
19
+
20
+ import java.io.File;
21
+ import java.io.FileInputStream;
22
+ import java.io.FileOutputStream;
23
+ import java.io.IOException;
24
+ import java.io.InputStream;
25
+ import java.io.OutputStream;
26
+ import java.util.Locale;
27
+
28
+ import iknow.android.utils.BaseUtils;
29
+ import iknow.android.utils.BuildConfig;
30
+
31
+
32
+ @SuppressWarnings({ "ResultOfMethodCallIgnored", "FieldCanBeLocal" })
33
+ public class StorageUtil {
34
+
35
+ private static final String TAG = "StorageUtil";
36
+ private static String APP_DATA_PATH = "/Android/data/" + BuildConfig.APPLICATION_ID;
37
+
38
+ private static String sDataDir;
39
+ private static String sCacheDir;
40
+
41
+ public static String getAppDataDir() {
42
+ if (TextUtils.isEmpty(sDataDir)) {
43
+ try {
44
+ if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
45
+ sDataDir = Environment.getExternalStorageDirectory().getPath() + APP_DATA_PATH;
46
+ if (TextUtils.isEmpty(sDataDir)) {
47
+ sDataDir = BaseUtils.getContext().getFilesDir().getAbsolutePath();
48
+ }
49
+ } else {
50
+ sDataDir = BaseUtils.getContext().getFilesDir().getAbsolutePath();
51
+ }
52
+ } catch (Throwable e) {
53
+ e.printStackTrace();
54
+ sDataDir = BaseUtils.getContext().getFilesDir().getAbsolutePath();
55
+ }
56
+ File file = new File(sDataDir);
57
+ if (!file.exists()) {//判断文件目录是否存在
58
+ file.mkdirs();
59
+ }
60
+ }
61
+ return sDataDir;
62
+ }
63
+
64
+ public static String getCacheDir() {
65
+ if (TextUtils.isEmpty(sCacheDir)) {
66
+ File file = null;
67
+ Context context = BaseUtils.getContext();
68
+ try {
69
+ if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
70
+ file = context.getExternalCacheDir();
71
+ if (file == null || !file.exists()) {
72
+ file = getExternalCacheDirManual(context);
73
+ }
74
+ }
75
+ if (file == null) {
76
+ file = context.getCacheDir();
77
+ if (file == null || !file.exists()) {
78
+ file = getCacheDirManual(context);
79
+ }
80
+ }
81
+ Log.w(TAG, "cache dir = " + file.getAbsolutePath());
82
+ sCacheDir = file.getAbsolutePath();
83
+ } catch (Throwable ignored) {
84
+ }
85
+ }
86
+ return sCacheDir;
87
+ }
88
+
89
+ private static File getExternalCacheDirManual(Context context) {
90
+ File dataDir = new File(new File(Environment.getExternalStorageDirectory(), "Android"), "data");
91
+ File appCacheDir = new File(new File(dataDir, context.getPackageName()), "cache");
92
+ if (!appCacheDir.exists()) {
93
+ if (!appCacheDir.mkdirs()) {//
94
+ Log.w(TAG, "Unable to create external cache directory");
95
+ return null;
96
+ }
97
+ try {
98
+ new File(appCacheDir, ".nomedia").createNewFile();
99
+ } catch (IOException e) {
100
+ Log.i(TAG, "Can't create \".nomedia\" file in application external cache directory");
101
+ }
102
+ }
103
+ return appCacheDir;
104
+ }
105
+
106
+ @SuppressLint("SdCardPath")
107
+ private static File getCacheDirManual(Context context) {
108
+ String cacheDirPath = "/data/data/" + context.getPackageName() + "/cache";
109
+ return new File(cacheDirPath);
110
+ }
111
+
112
+ public static boolean delFiles(String path) {
113
+ File cacheFile = new File(path);
114
+ if (!cacheFile.exists()) {
115
+ return false;
116
+ }
117
+ File[] files = cacheFile.listFiles();
118
+ for (int i = 0; i < files.length; i++) {
119
+ // 是文件则直接删除
120
+ if (files[i].exists() && files[i].isFile()) {
121
+ files[i].delete();
122
+ } else if (files[i].exists() && files[i].isDirectory()) {
123
+ // 递归删除文件
124
+ delFiles(files[i].getAbsolutePath());
125
+ // 删除完目录下面的所有文件后再删除该文件夹
126
+ files[i].delete();
127
+ }
128
+ }
129
+
130
+ return true;
131
+ }
132
+
133
+ public static long sizeOfDirectory(File dir) {
134
+ if (dir.exists()) {
135
+ long result = 0;
136
+ File[] fileList = dir.listFiles();
137
+ for (int i = 0; i < fileList.length; i++) {
138
+ // Recursive call if it's a directory
139
+ if (fileList[i].isDirectory()) {
140
+ result += sizeOfDirectory(fileList[i]);
141
+ } else {
142
+ // Sum the file size in bytes
143
+ result += fileList[i].length();
144
+ }
145
+ }
146
+ return result; // return the file size
147
+ }
148
+ return 0;
149
+ }
150
+
151
+ /**
152
+ * @param length 长度 byte为单位
153
+ * 将文件大小转换为KB,MB格式
154
+ */
155
+ public static String getFileSize(long length) {
156
+ int MB = 1024 * 1024;
157
+ if (length < MB) {
158
+ double resultKB = length * 1.0 / 1024;
159
+ return String.format(Locale.getDefault(), "%.1f", resultKB) + "Kb";
160
+ }
161
+ double resultMB = length * 1.0 / MB;
162
+ return String.format(Locale.getDefault(), "%.1f", resultMB) + "Mb";
163
+ }
164
+
165
+ public static boolean isFileExist(String path) {
166
+ if (TextUtils.isEmpty(path)) return false;
167
+ File file = new File(path);
168
+ return file.exists();
169
+ }
170
+
171
+ /**
172
+ * @param path 路径
173
+ * @return 是否删除成功
174
+ */
175
+ public static boolean deleteFile(String path) {
176
+ if (TextUtils.isEmpty(path)) return true;
177
+ return deleteFile(new File(path));
178
+ }
179
+
180
+ /**
181
+ * @return 是否删除成功
182
+ */
183
+ public static boolean deleteFile(File file) {
184
+ if (file == null || !file.exists()) return true;
185
+
186
+ if (file.isFile()) {
187
+ return file.delete();
188
+ }
189
+
190
+ if (!file.isDirectory()) {
191
+ return false;
192
+ }
193
+
194
+ for (File f : file.listFiles()) {
195
+ if (f.isFile()) {
196
+ f.delete();
197
+ } else if (f.isDirectory()) {
198
+ deleteFile(f);
199
+ }
200
+ }
201
+ return file.delete();
202
+ }
203
+
204
+ public static void saveVideoToGallery(ReactApplicationContext context, String videoFilePath) throws IOException {
205
+ File videoFile = new File(videoFilePath);
206
+
207
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
208
+ // For Android 10 and higher (API >= 29)
209
+ saveVideoUsingMediaStore(context, videoFile);
210
+ } else {
211
+ // For Android 9 and below (API < 29)
212
+ try {
213
+ saveVideoUsingTraditionalStorage(context, videoFile);
214
+ } catch (IOException e) {
215
+ throw new RuntimeException(e);
216
+ }
217
+ }
218
+ }
219
+
220
+ // Save video using MediaStore for API level >= 29
221
+ private static void saveVideoUsingMediaStore(Context context, File videoFile) {
222
+ ContentValues values = new ContentValues();
223
+ values.put(MediaStore.Video.Media.TITLE, "My Video Title");
224
+ values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
225
+ values.put(MediaStore.Video.Media.RELATIVE_PATH, Environment.DIRECTORY_DCIM);
226
+ Uri uri = context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
227
+
228
+ if (uri != null) {
229
+ try {
230
+ OutputStream outputStream = context.getContentResolver().openOutputStream(uri);
231
+ if (outputStream != null) {
232
+ // Copy the video file to the output stream
233
+ // Here, you can use the method you have to copy the file contents
234
+ // For example, you can use FileInputStream to read from videoFile and write to outputStream
235
+
236
+ outputStream.close();
237
+ // Notify the media scanner that a new video has been added to the gallery
238
+ MediaScannerConnection.scanFile(context, new String[]{videoFile.getAbsolutePath()}, new String[]{"video/*"}, null);
239
+ }
240
+ } catch (IOException e) {
241
+ e.printStackTrace();
242
+ }
243
+ }
244
+ }
245
+
246
+ // Save video using traditional storage for API level < 29
247
+ private static void saveVideoUsingTraditionalStorage(Context context, File videoFile) throws IOException {
248
+ File galleryDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
249
+ File destinationFile = new File(galleryDirectory, videoFile.getName());
250
+ copyFile(videoFile, destinationFile);
251
+ MediaScannerConnection.scanFile(context, new String[]{destinationFile.getAbsolutePath()}, new String[]{"video/*"}, null);
252
+ }
253
+
254
+ private static void copyFile(File sourceFile, File destFile) throws IOException {
255
+ InputStream inputStream = null;
256
+ OutputStream outputStream = null;
257
+
258
+ try {
259
+ inputStream = new FileInputStream(sourceFile);
260
+ outputStream = new FileOutputStream(destFile);
261
+
262
+ byte[] buffer = new byte[1024];
263
+ int length;
264
+ while ((length = inputStream.read(buffer)) > 0) {
265
+ outputStream.write(buffer, 0, length);
266
+ }
267
+
268
+ } finally {
269
+ if (inputStream != null) {
270
+ inputStream.close();
271
+ }
272
+ if (outputStream != null) {
273
+ outputStream.close();
274
+ }
275
+ }
276
+ }
277
+ }
@@ -0,0 +1,109 @@
1
+ package com.videotrim.utils;
2
+
3
+ import android.content.Context;
4
+ import android.graphics.Bitmap;
5
+ import android.media.MediaMetadataRetriever;
6
+ import android.net.Uri;
7
+ import android.util.Log;
8
+
9
+ import com.arthenica.ffmpegkit.FFmpegKit;
10
+ import com.arthenica.ffmpegkit.FFmpegSession;
11
+ import com.arthenica.ffmpegkit.FFmpegSessionCompleteCallback;
12
+ import com.arthenica.ffmpegkit.LogCallback;
13
+ import com.arthenica.ffmpegkit.ReturnCode;
14
+ import com.arthenica.ffmpegkit.SessionState;
15
+ import com.arthenica.ffmpegkit.Statistics;
16
+ import com.arthenica.ffmpegkit.StatisticsCallback;
17
+ import com.videotrim.interfaces.VideoTrimListener;
18
+
19
+ import java.text.SimpleDateFormat;
20
+ import java.util.Date;
21
+ import java.util.Locale;
22
+
23
+ import iknow.android.utils.DeviceUtil;
24
+ import iknow.android.utils.UnitConverter;
25
+ import iknow.android.utils.callback.SingleCallback;
26
+ import iknow.android.utils.thread.BackgroundExecutor;
27
+
28
+ public class VideoTrimmerUtil {
29
+
30
+ private static final String TAG = VideoTrimmerUtil.class.getSimpleName();
31
+ public static final long MIN_SHOOT_DURATION = 3000L;// min 3 seconds for trimming
32
+ // public static final int VIDEO_MAX_TIME = 10;// max 10 seconds for trimming
33
+ // public static final long MAX_SHOOT_DURATION = VIDEO_MAX_TIME * 1000L;
34
+ public static long maxShootDuration = 10 * 1000L;
35
+ public static final int MAX_COUNT_RANGE = 10; // how many images in the highlight range of seek bar
36
+ public static final int SCREEN_WIDTH_FULL = DeviceUtil.getDeviceWidth();
37
+ public static final int RECYCLER_VIEW_PADDING = UnitConverter.dpToPx(35);
38
+ public static final int VIDEO_FRAMES_WIDTH = SCREEN_WIDTH_FULL - RECYCLER_VIEW_PADDING * 2;
39
+ // public static final int THUMB_WIDTH = (SCREEN_WIDTH_FULL - RECYCLER_VIEW_PADDING * 2) / VIDEO_MAX_TIME;
40
+ public static int mThumbWidth = 0; // make it automatic
41
+ public static final int THUMB_HEIGHT = UnitConverter.dpToPx(50); // x2 for better resolution
42
+ private static final int THUMB_RESOLUTION_RES = 2; // double thumb resolution for better quality
43
+
44
+ public static void trim(Context context, String inputFile, String outputFile, int videoDuration, long startMs, long endMs, final VideoTrimListener callback) {
45
+ final String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
46
+ final String outputName = "trimmedVideo_" + timeStamp + ".mp4";
47
+ outputFile = outputFile + "/" + outputName;
48
+
49
+ String cmd = "-i " + inputFile + " -ss " + startMs + "ms" + " -to " + endMs + "ms -c copy " + outputFile;
50
+ String[] command = cmd.split(" ");
51
+ final String tempOutFile = outputFile;
52
+
53
+ callback.onStartTrim();
54
+ FFmpegKit.executeAsync(cmd, session -> {
55
+ SessionState state = session.getState();
56
+ ReturnCode returnCode = session.getReturnCode();
57
+
58
+ Log.d(TAG, String.format("FFmpeg process exited with state %s and rc %s.%s", state, returnCode, session.getFailStackTrace()));
59
+
60
+ if (state.equals(SessionState.COMPLETED)) {
61
+ callback.onFinishTrim(tempOutFile);
62
+ } else {
63
+ callback.onError();
64
+ }
65
+ }, log -> {
66
+
67
+ }, statistics -> {
68
+ int timeInMilliseconds = statistics.getTime();
69
+ if (timeInMilliseconds > 0) {
70
+ int completePercentage =
71
+ (timeInMilliseconds * 100) / videoDuration;
72
+ callback.onTrimmingProgress(Math.min(Math.max(completePercentage, 0), 100));
73
+ }
74
+ });
75
+ }
76
+
77
+ public static void shootVideoThumbInBackground(final Context context, final Uri videoUri, final int totalThumbsCount, final long startPosition,
78
+ final long endPosition, final SingleCallback<Bitmap, Integer> callback) {
79
+ BackgroundExecutor.execute(new BackgroundExecutor.Task("", 0L, "") {
80
+ @Override public void execute() {
81
+ try {
82
+ MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
83
+ mediaMetadataRetriever.setDataSource(context, videoUri);
84
+ // Retrieve media data use microsecond
85
+ long interval = (endPosition - startPosition) / (totalThumbsCount - 1);
86
+ for (long i = 0; i < totalThumbsCount; ++i) {
87
+ long frameTime = startPosition + interval * i;
88
+ Bitmap bitmap = mediaMetadataRetriever.getFrameAtTime(frameTime * 1000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
89
+ if(bitmap == null) continue;
90
+ try {
91
+ if (mThumbWidth <= 0) { // only take first item
92
+ int width = THUMB_HEIGHT * bitmap.getWidth() / bitmap.getHeight();
93
+ mThumbWidth = width;
94
+ }
95
+ bitmap = Bitmap.createScaledBitmap(bitmap, mThumbWidth * THUMB_RESOLUTION_RES, THUMB_HEIGHT * THUMB_RESOLUTION_RES, false);
96
+ } catch (final Throwable t) {
97
+ t.printStackTrace();
98
+ }
99
+ callback.onSingleCallback(bitmap, (int) interval);
100
+ }
101
+ mediaMetadataRetriever.release();
102
+ } catch (final Throwable e) {
103
+ Thread.getDefaultUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), e);
104
+ }
105
+ }
106
+ });
107
+ }
108
+ }
109
+