react-native-video-trim 3.0.3 → 3.0.5

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.
@@ -72,7 +72,7 @@ public class VideoTrimmerUtil {
72
72
  retriever.setDataSource(inputFile);
73
73
  String rotationStr = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
74
74
  int rotation = rotationStr != null ? Integer.parseInt(rotationStr) : 0;
75
- retriever.release();
75
+
76
76
 
77
77
  extractor = new MediaExtractor();
78
78
  extractor.setDataSource(inputFile);
@@ -89,11 +89,37 @@ public class VideoTrimmerUtil {
89
89
  long endUs = endMs * 1000; // e.g., 9s = 9000000us
90
90
  long trimmedDurationUs = endUs - startUs; // e.g., 4s = 4000000us
91
91
 
92
+ // Determine max buffer size from video format and resolution
93
+ int maxBufferSize = BUFFER_SIZE; // Default 1MB
94
+ String widthStr = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
95
+ String heightStr = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
96
+ int width = widthStr != null ? Integer.parseInt(widthStr) : 0; // Default to unknown
97
+ int height = heightStr != null ? Integer.parseInt(heightStr) : 0;
98
+
99
+ // IMPORTANT: after this line, the retriever is no longer needed
100
+ retriever.release();
101
+
102
+ // Adjust buffer size based on resolution
103
+ if (width > 3840 || height > 2160) { // 8K+
104
+ maxBufferSize = 16 * 1024 * 1024; // 16MB for 8K
105
+ } else if (width > 1920 || height > 1080) { // 4K
106
+ maxBufferSize = 8 * 1024 * 1024; // 8MB for 4K
107
+ } else if (width > 1280 || height > 720) { // 1080p
108
+ maxBufferSize = 4 * 1024 * 1024; // 4MB for 1080p
109
+ } // 720p or lower (or unknown resolution) sticks with BUFFER_SIZE (1MB)
110
+
92
111
  // Add tracks with corrected duration
93
112
  for (int i = 0; i < trackCount; i++) {
94
113
  MediaFormat format = extractor.getTrackFormat(i);
114
+
115
+ // Override with KEY_MAX_INPUT_SIZE if available
116
+ if (format.containsKey(MediaFormat.KEY_MAX_INPUT_SIZE)) {
117
+ int maxInputSize = format.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
118
+ maxBufferSize = Math.max(maxBufferSize, maxInputSize);
119
+ }
120
+
95
121
  String mime = format.getString(MediaFormat.KEY_MIME);
96
- if (mime.startsWith("video/")) {
122
+ if (mime != null && mime.startsWith("video/")) {
97
123
  videoTrackIndex = i;
98
124
  }
99
125
  // Set the duration for each track to the trimmed duration
@@ -109,7 +135,7 @@ public class VideoTrimmerUtil {
109
135
  // Seek to start time
110
136
  extractor.seekTo(startUs, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
111
137
 
112
- ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
138
+ ByteBuffer buffer = ByteBuffer.allocate(maxBufferSize);
113
139
  MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
114
140
  long lastProgressTime = System.currentTimeMillis();
115
141
  boolean videoSampleWritten = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-video-trim",
3
- "version": "3.0.3",
3
+ "version": "3.0.5",
4
4
  "description": "Video trimmer for your React Native app",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/module/index",