|
26 | 26 |
|
27 | 27 | import android.annotation.TargetApi; |
28 | 28 | import android.graphics.Bitmap; |
| 29 | +import android.graphics.Matrix; |
29 | 30 | import android.media.MediaMetadataRetriever; |
| 31 | +import android.net.Uri; |
30 | 32 | import android.os.Build; |
31 | 33 | import android.util.Base64; |
| 34 | +import android.util.Log; |
32 | 35 |
|
33 | 36 | import com.facebook.react.bridge.Arguments; |
34 | 37 | import com.facebook.react.bridge.Promise; |
| 38 | +import com.facebook.react.bridge.ReactApplicationContext; |
35 | 39 | import com.facebook.react.bridge.WritableArray; |
36 | 40 | import com.facebook.react.bridge.WritableMap; |
37 | 41 | import com.facebook.react.uimanager.events.Event; |
38 | 42 | import com.shahenlibrary.Events.Events; |
| 43 | +import com.shahenlibrary.utils.VideoEdit; |
39 | 44 |
|
40 | 45 | import java.io.ByteArrayOutputStream; |
41 | 46 |
|
| 47 | +import wseemann.media.FFmpegMediaMetadataRetriever; |
| 48 | + |
42 | 49 | public class Trimmer { |
43 | 50 |
|
44 | | - public static void getPreviewImages(String path, Promise promise) { |
45 | | - MediaMetadataRetriever retriever = new MediaMetadataRetriever(); |
46 | | - retriever.setDataSource(path); |
| 51 | + public static void getPreviewImages(String path, Promise promise, ReactApplicationContext ctx) { |
| 52 | + FFmpegMediaMetadataRetriever retriever = new FFmpegMediaMetadataRetriever(); |
| 53 | + if (VideoEdit.shouldUseURI(path)) { |
| 54 | + retriever.setDataSource(ctx, Uri.parse(path)); |
| 55 | + } else { |
| 56 | + retriever.setDataSource(path); |
| 57 | + } |
| 58 | + |
| 59 | + WritableArray images = Arguments.createArray(); |
| 60 | + int duration = Integer.parseInt(retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION)); |
| 61 | + int width = Integer.parseInt(retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)); |
| 62 | + int height = Integer.parseInt(retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)); |
| 63 | + int orientation = Integer.parseInt(retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION)); |
| 64 | + |
| 65 | + float aspectRatio = width / height; |
| 66 | + |
| 67 | + int resizeWidth = 200; |
| 68 | + int resizeHeight = Math.round(resizeWidth / aspectRatio); |
47 | 69 |
|
48 | | - WritableArray images = Arguments.createArray(); |
49 | | - int duration = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)); |
50 | | - int width = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)); |
51 | | - int height = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)); |
| 70 | + float scaleWidth = ((float) resizeWidth) / width; |
| 71 | + float scaleHeight = ((float) resizeHeight) / height; |
52 | 72 |
|
53 | | - int aspectRatio = Math.round(width / height); |
54 | | - int resizeWidth = 200; |
55 | | - int resizeHeight = resizeWidth / aspectRatio; |
| 73 | + Log.d(TrimmerManager.REACT_PACKAGE, "getPreviewImages: \n\tduration: " + duration + |
| 74 | + "\n\twidth: " + width + |
| 75 | + "\n\theight: " + height + |
| 76 | + "\n\torientation: " + orientation + |
| 77 | + "\n\taspectRatio: " + aspectRatio + |
| 78 | + "\n\tresizeWidth: " + resizeWidth + |
| 79 | + "\n\tresizeHeight: " + resizeHeight |
| 80 | + ); |
56 | 81 |
|
57 | | - for (int i = 0; i < duration; i += duration / 10) { |
58 | | - Bitmap currBmp = Bitmap.createScaledBitmap(retriever.getFrameAtTime(i * 1000), resizeWidth, resizeHeight, false); |
59 | | - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
60 | | - currBmp.compress(Bitmap.CompressFormat.PNG, 90, byteArrayOutputStream); |
61 | | - byte[] byteArray = byteArrayOutputStream .toByteArray(); |
62 | | - String encoded = "data:image/png;base64," + Base64.encodeToString(byteArray, Base64.DEFAULT); |
63 | | - images.pushString(encoded); |
64 | | - } |
| 82 | + Matrix mx = new Matrix(); |
65 | 83 |
|
66 | | - WritableMap event = Arguments.createMap(); |
| 84 | + mx.postScale(scaleWidth, scaleHeight); |
| 85 | + mx.postRotate(orientation - 360); |
67 | 86 |
|
68 | | - event.putArray("images", images); |
| 87 | + for (int i = 0; i < duration; i += duration / 10) { |
| 88 | + Bitmap frame = retriever.getFrameAtTime(i * 1000); |
| 89 | + Bitmap currBmp = Bitmap.createScaledBitmap(frame, resizeWidth, resizeHeight, false); |
69 | 90 |
|
70 | | - promise.resolve(event); |
71 | | - retriever.release(); |
| 91 | + Bitmap normalizedBmp = Bitmap.createBitmap(currBmp, 0, 0, resizeWidth, resizeHeight, mx, true); |
| 92 | + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| 93 | + normalizedBmp.compress(Bitmap.CompressFormat.PNG, 90, byteArrayOutputStream); |
| 94 | + byte[] byteArray = byteArrayOutputStream .toByteArray(); |
| 95 | + String encoded = "data:image/png;base64," + Base64.encodeToString(byteArray, Base64.DEFAULT); |
| 96 | + images.pushString(encoded); |
72 | 97 | } |
73 | 98 |
|
74 | | - @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) |
75 | | - public static void getVideoInfo(String path, Promise promise) { |
76 | | - MediaMetadataRetriever mmr = new MediaMetadataRetriever(); |
77 | | - mmr.setDataSource(path); |
| 99 | + WritableMap event = Arguments.createMap(); |
78 | 100 |
|
79 | | - int duration = Integer.parseInt(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)); |
80 | | - int width = Integer.parseInt(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)); |
81 | | - int height = Integer.parseInt(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)); |
82 | | - int orientation = Integer.parseInt(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION)); |
| 101 | + event.putArray("images", images); |
83 | 102 |
|
84 | | - WritableMap event = Arguments.createMap(); |
85 | | - event.putInt(Events.DURATION, duration); |
86 | | - event.putInt(Events.WIDTH, width); |
87 | | - event.putInt(Events.HEIGHT, height); |
88 | | - event.putInt(Events.ORIENTATION, orientation); |
| 103 | + promise.resolve(event); |
| 104 | + retriever.release(); |
| 105 | + } |
89 | 106 |
|
90 | | - promise.resolve(event); |
| 107 | + @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) |
| 108 | + public static void getVideoInfo(String path, Promise promise, ReactApplicationContext ctx) { |
| 109 | + FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever(); |
| 110 | + |
| 111 | + if (VideoEdit.shouldUseURI(path)) { |
| 112 | + mmr.setDataSource(ctx, Uri.parse(path)); |
| 113 | + } else { |
| 114 | + mmr.setDataSource(path); |
91 | 115 | } |
| 116 | + |
| 117 | + int duration = Integer.parseInt(mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION)); |
| 118 | + int width = Integer.parseInt(mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)); |
| 119 | + int height = Integer.parseInt(mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)); |
| 120 | + int orientation = Integer.parseInt(mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION)); |
| 121 | + |
| 122 | + WritableMap event = Arguments.createMap(); |
| 123 | + event.putInt(Events.DURATION, duration); |
| 124 | + event.putInt(Events.WIDTH, width); |
| 125 | + event.putInt(Events.HEIGHT, height); |
| 126 | + event.putInt(Events.ORIENTATION, orientation); |
| 127 | + |
| 128 | + promise.resolve(event); |
| 129 | + |
| 130 | + mmr.release(); |
| 131 | + } |
92 | 132 | } |
0 commit comments