react-native-compressor 1.7.2 → 1.8.0

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 (128) hide show
  1. package/README.md +30 -7
  2. package/android/build.gradle +8 -0
  3. package/android/src/main/java/com/reactnativecompressor/Audio/AudioCompressor.kt +527 -0
  4. package/android/src/main/java/com/reactnativecompressor/Audio/AudioMain.kt +31 -0
  5. package/android/src/main/java/com/reactnativecompressor/CompressorModule.kt +157 -0
  6. package/android/src/main/java/com/reactnativecompressor/CompressorPackage.kt +34 -0
  7. package/android/src/main/java/com/reactnativecompressor/Image/ImageCompressor.kt +199 -0
  8. package/android/src/main/java/com/reactnativecompressor/Image/ImageCompressorOptions.kt +57 -0
  9. package/android/src/main/java/com/reactnativecompressor/Image/ImageMain.kt +31 -0
  10. package/android/src/main/java/com/reactnativecompressor/Image/ImageSize.kt +3 -0
  11. package/android/src/main/java/com/reactnativecompressor/Utils/Downloader.kt +119 -0
  12. package/android/src/main/java/com/reactnativecompressor/Utils/EventEmitterHandler.kt +66 -0
  13. package/android/src/main/java/com/reactnativecompressor/Utils/MediaCache.kt +61 -0
  14. package/android/src/main/java/com/reactnativecompressor/Utils/RealPathUtil.kt +177 -0
  15. package/android/src/main/java/com/reactnativecompressor/Utils/Uploader.kt +117 -0
  16. package/android/src/main/java/com/reactnativecompressor/Utils/Utils.kt +162 -0
  17. package/android/src/main/java/com/reactnativecompressor/Video/AutoVideoCompression.kt +63 -0
  18. package/android/src/main/java/com/reactnativecompressor/Video/VideoCompressorHelper.kt +125 -0
  19. package/android/src/main/java/com/reactnativecompressor/Video/VideoMain.kt +86 -0
  20. package/android/src/newarch/CompressorSpec.kt +5 -0
  21. package/android/src/oldarch/CompressorSpec.kt +32 -0
  22. package/ios/Audio/AudioMain.swift +115 -0
  23. package/ios/Compressor-Bridging-Header.h +1 -2
  24. package/ios/Compressor.h +0 -8
  25. package/ios/Compressor.mm +28 -389
  26. package/ios/CompressorManager.swift +96 -0
  27. package/ios/Image/ImageCompressor.swift +354 -0
  28. package/ios/Image/ImageCompressorOptions.swift +93 -0
  29. package/ios/Image/ImageMain.swift +27 -0
  30. package/ios/Utils/Downloader.swift +157 -0
  31. package/ios/Utils/EventEmitterHandler.swift +54 -0
  32. package/ios/Utils/MediaCache.swift +48 -0
  33. package/ios/Utils/Uploader.swift +99 -0
  34. package/ios/Utils/Utils.swift +110 -0
  35. package/ios/Video/{VideoCompressor.swift → VideoMain.swift} +167 -146
  36. package/lib/commonjs/Image/index.js +1 -1
  37. package/lib/commonjs/Image/index.js.map +1 -1
  38. package/lib/commonjs/Main.js +1 -8
  39. package/lib/commonjs/Main.js.map +1 -1
  40. package/lib/commonjs/Spec/NativeCompressor.js.map +1 -1
  41. package/lib/commonjs/Video/index.js +4 -33
  42. package/lib/commonjs/Video/index.js.map +1 -1
  43. package/lib/commonjs/expo-plugin/compressor.js.map +1 -1
  44. package/lib/commonjs/index.js +11 -6
  45. package/lib/commonjs/index.js.map +1 -1
  46. package/lib/commonjs/utils/Downloader.js +38 -0
  47. package/lib/commonjs/utils/Downloader.js.map +1 -0
  48. package/lib/commonjs/utils/Uploader.js +40 -0
  49. package/lib/commonjs/utils/Uploader.js.map +1 -0
  50. package/lib/commonjs/utils/index.js +35 -0
  51. package/lib/commonjs/utils/index.js.map +1 -1
  52. package/lib/module/Image/index.js +1 -1
  53. package/lib/module/Image/index.js.map +1 -1
  54. package/lib/module/Main.js +1 -7
  55. package/lib/module/Main.js.map +1 -1
  56. package/lib/module/Spec/NativeCompressor.js.map +1 -1
  57. package/lib/module/Video/index.js +5 -33
  58. package/lib/module/Video/index.js.map +1 -1
  59. package/lib/module/expo-plugin/compressor.js.map +1 -1
  60. package/lib/module/index.js +6 -6
  61. package/lib/module/index.js.map +1 -1
  62. package/lib/module/utils/Downloader.js +31 -0
  63. package/lib/module/utils/Downloader.js.map +1 -0
  64. package/lib/module/utils/Uploader.js +33 -0
  65. package/lib/module/utils/Uploader.js.map +1 -0
  66. package/lib/module/utils/index.js +2 -0
  67. package/lib/module/utils/index.js.map +1 -1
  68. package/lib/typescript/Image/index.d.ts +4 -0
  69. package/lib/typescript/Image/index.d.ts.map +1 -1
  70. package/lib/typescript/Main.d.ts +1 -2
  71. package/lib/typescript/Main.d.ts.map +1 -1
  72. package/lib/typescript/Spec/NativeCompressor.d.ts +8 -2
  73. package/lib/typescript/Spec/NativeCompressor.d.ts.map +1 -1
  74. package/lib/typescript/Video/index.d.ts +4 -28
  75. package/lib/typescript/Video/index.d.ts.map +1 -1
  76. package/lib/typescript/index.d.ts +5 -4
  77. package/lib/typescript/index.d.ts.map +1 -1
  78. package/lib/typescript/utils/Downloader.d.ts +2 -0
  79. package/lib/typescript/utils/Downloader.d.ts.map +1 -0
  80. package/lib/typescript/utils/Uploader.d.ts +28 -0
  81. package/lib/typescript/utils/Uploader.d.ts.map +1 -0
  82. package/lib/typescript/utils/index.d.ts +2 -1
  83. package/lib/typescript/utils/index.d.ts.map +1 -1
  84. package/package.json +6 -4
  85. package/src/Image/index.tsx +5 -1
  86. package/src/Main.tsx +1 -15
  87. package/src/Spec/NativeCompressor.ts +14 -2
  88. package/src/Video/index.tsx +11 -84
  89. package/src/expo-plugin/compressor.ts +1 -1
  90. package/src/index.tsx +6 -3
  91. package/src/utils/Downloader.tsx +38 -0
  92. package/src/utils/Uploader.tsx +78 -0
  93. package/src/utils/index.tsx +2 -0
  94. package/android/src/main/java/com/reactnativecompressor/Audio/AudioCompressor.java +0 -605
  95. package/android/src/main/java/com/reactnativecompressor/CompressorModule.java +0 -220
  96. package/android/src/main/java/com/reactnativecompressor/CompressorPackage.java +0 -58
  97. package/android/src/main/java/com/reactnativecompressor/Image/ImageCompressor.java +0 -257
  98. package/android/src/main/java/com/reactnativecompressor/Image/utils/ImageCompressorOptions.java +0 -69
  99. package/android/src/main/java/com/reactnativecompressor/Image/utils/ImageSize.java +0 -13
  100. package/android/src/main/java/com/reactnativecompressor/Utils/FileUplaoder/FileUploadHelper.java +0 -39
  101. package/android/src/main/java/com/reactnativecompressor/Utils/FileUplaoder/FileUploader.java +0 -129
  102. package/android/src/main/java/com/reactnativecompressor/Utils/MediaCache.java +0 -65
  103. package/android/src/main/java/com/reactnativecompressor/Utils/RealPathUtil.java +0 -210
  104. package/android/src/main/java/com/reactnativecompressor/Utils/Utils.java +0 -272
  105. package/android/src/main/java/com/reactnativecompressor/Video/AutoVideoCompression/AutoVideoCompression.java +0 -87
  106. package/android/src/main/java/com/reactnativecompressor/Video/VideoCompressorHelper.java +0 -182
  107. package/android/src/main/java/com/reactnativecompressor/Video/VideoModule.java +0 -112
  108. package/android/src/newarch/CompressorSpec.java +0 -9
  109. package/android/src/newarch/VideoCompressorSpec.java +0 -9
  110. package/android/src/oldarch/CompressorSpec.java +0 -34
  111. package/android/src/oldarch/VideoCompressorSpec.java +0 -26
  112. package/ios/Image/ImageCompressor.h +0 -22
  113. package/ios/Image/ImageCompressor.m +0 -540
  114. package/ios/Image/ImageCompressorOptions.h +0 -28
  115. package/ios/Image/ImageCompressorOptions.m +0 -97
  116. package/ios/Utils/Downloader.h +0 -16
  117. package/ios/Utils/Downloader.m +0 -150
  118. package/ios/Utils/MediaCache.h +0 -11
  119. package/ios/Utils/MediaCache.m +0 -51
  120. package/ios/Video/VideoCompressor.h +0 -5
  121. package/ios/Video/VideoCompressor.m +0 -1
  122. package/lib/commonjs/Spec/NativeVideoCompressor.js +0 -10
  123. package/lib/commonjs/Spec/NativeVideoCompressor.js.map +0 -1
  124. package/lib/module/Spec/NativeVideoCompressor.js +0 -3
  125. package/lib/module/Spec/NativeVideoCompressor.js.map +0 -1
  126. package/lib/typescript/Spec/NativeVideoCompressor.d.ts +0 -13
  127. package/lib/typescript/Spec/NativeVideoCompressor.d.ts.map +0 -1
  128. package/src/Spec/NativeVideoCompressor.ts +0 -14
@@ -0,0 +1,78 @@
1
+ import { NativeEventEmitter, Platform } from 'react-native';
2
+ import type { NativeEventSubscription } from 'react-native';
3
+ import { Compressor } from '../Main';
4
+ const CompressEventEmitter = new NativeEventEmitter(Compressor);
5
+ import { uuidv4 } from '.';
6
+ export declare enum FileSystemUploadType {
7
+ BINARY_CONTENT = 0,
8
+ MULTIPART = 1,
9
+ }
10
+
11
+ export declare enum FileSystemSessionType {
12
+ BACKGROUND = 0,
13
+ FOREGROUND = 1,
14
+ }
15
+
16
+ export declare type HTTPResponse = {
17
+ status: number;
18
+ headers: Record<string, string>;
19
+ body: string;
20
+ };
21
+
22
+ export declare type FileSystemAcceptedUploadHttpMethod =
23
+ | 'POST'
24
+ | 'PUT'
25
+ | 'PATCH';
26
+
27
+ export declare type FileSystemUploadOptions = (
28
+ | {
29
+ uploadType?: FileSystemUploadType.BINARY_CONTENT;
30
+ }
31
+ | {
32
+ uploadType: FileSystemUploadType.MULTIPART;
33
+ fieldName?: string;
34
+ mimeType?: string;
35
+ parameters?: Record<string, string>;
36
+ }
37
+ ) & {
38
+ headers?: Record<string, string>;
39
+ httpMethod?: FileSystemAcceptedUploadHttpMethod;
40
+ sessionType?: FileSystemSessionType;
41
+ };
42
+
43
+ export const backgroundUpload = async (
44
+ url: string,
45
+ fileUrl: string,
46
+ options: FileSystemUploadOptions,
47
+ onProgress?: (writtem: number, total: number) => void
48
+ ): Promise<any> => {
49
+ const uuid = uuidv4();
50
+ let subscription: NativeEventSubscription;
51
+ try {
52
+ if (onProgress) {
53
+ subscription = CompressEventEmitter.addListener(
54
+ 'uploadProgress',
55
+ (event: any) => {
56
+ if (event.uuid === uuid) {
57
+ onProgress(event.data.written, event.data.total);
58
+ }
59
+ }
60
+ );
61
+ }
62
+ if (Platform.OS === 'android' && fileUrl.includes('file://')) {
63
+ fileUrl = fileUrl.replace('file://', '');
64
+ }
65
+ const result = await Compressor.upload(fileUrl, {
66
+ uuid,
67
+ method: options.httpMethod,
68
+ headers: options.headers,
69
+ url,
70
+ });
71
+ return result;
72
+ } finally {
73
+ // @ts-ignore
74
+ if (subscription) {
75
+ subscription.remove();
76
+ }
77
+ }
78
+ };
@@ -186,3 +186,5 @@ export const uuidv4 = () => {
186
186
  return v.toString(16);
187
187
  });
188
188
  };
189
+ export * from './Downloader';
190
+ export * from './Uploader';
@@ -1,605 +0,0 @@
1
- package com.reactnativecompressor.Audio;
2
-
3
- import android.annotation.SuppressLint;
4
- import android.media.MediaCodecList;
5
- import android.media.MediaMetadataRetriever;
6
- import android.util.Log;
7
-
8
- import android.media.MediaCodec;
9
- import android.media.MediaCodecInfo;
10
- import android.media.MediaExtractor;
11
- import android.media.MediaFormat;
12
- import android.media.MediaMuxer;
13
- import android.os.Build;
14
-
15
- import androidx.annotation.RequiresApi;
16
-
17
- import numan.dev.videocompressor.codecinputsurface.CodecInputSurface;
18
- import java.io.File;
19
- import java.io.IOException;
20
- import java.nio.ByteBuffer;
21
-
22
-
23
- public class AudioCompressor {
24
- private static final String TAG = "AudioCompressor";
25
- private static final boolean VERBOSE = true; // lots of logging
26
- public String path;
27
- private String outputPath;
28
- public final static String MIME_TYPE = MediaFormat.MIMETYPE_AUDIO_AAC;
29
- private MediaCodec.BufferInfo mBufferInfo;
30
- private MediaMuxer mMuxer;
31
- private MediaCodec mEncoder;
32
- private MediaCodec mDecoder;
33
- private int mTrackIndex;
34
- private CodecInputSurface mInputSurface;
35
- // bit rate, in bits per second
36
- private int mBitRate = -1;
37
- private static final int MEDIATYPE_NOT_AUDIO_VIDEO = -233;
38
- private final int TIMEOUT_USEC = 2500;
39
-
40
- /***
41
- * trans video and audio by mediacodec
42
- *
43
- * */
44
- @SuppressLint("LongLogTag")
45
- @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
46
- public boolean CompressAudio(final String sourcePath, String destinationPath, int nbitrate) {
47
-
48
- this.path = sourcePath;
49
- this.outputPath = destinationPath;
50
-
51
- if (checkParmsError(sourcePath, destinationPath, nbitrate)) {
52
- return false;
53
- }
54
-
55
- //get origin video info
56
- MediaMetadataRetriever retriever = new MediaMetadataRetriever();
57
- retriever.setDataSource(path);
58
- // String framecount = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_FRAME_COUNT);
59
- long duration = Long.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)) * 1000;
60
-
61
- long startTime = -1;
62
- long endTime = -1;
63
-
64
-
65
-
66
- mBitRate = nbitrate;
67
- // NUM_FRAMES = Integer.getInteger(framecount);
68
- //IFRAME_INTERVAL = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
69
-
70
-
71
- boolean error = false;
72
- long videoStartTime = -1;
73
-
74
- long time = System.currentTimeMillis();
75
-
76
- File cacheFile = new File(destinationPath);
77
- File inputFile = new File(path);
78
- if (!inputFile.canRead()) {
79
-
80
- return false;
81
- }
82
-
83
- MediaExtractor extractor = null;
84
- MediaExtractor mAudioExtractor = null;
85
-
86
- try {
87
- // video MediaExtractor
88
- extractor = new MediaExtractor();
89
- extractor.setDataSource(inputFile.toString());
90
-
91
- // audio MediaExtractor
92
- mAudioExtractor = new MediaExtractor();
93
- mAudioExtractor.setDataSource(inputFile.toString());
94
- try {
95
- mMuxer = new MediaMuxer(outputPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
96
- } catch (IOException ioe) {
97
-
98
-
99
- throw new RuntimeException("MediaMuxer creation failed", ioe);
100
- }
101
-
102
- int muxerAudioTrackIndex = 0;
103
-
104
-
105
- int audioIndex = selectTrack(mAudioExtractor, true);
106
- if (audioIndex >= 0) {
107
- mAudioExtractor.selectTrack(audioIndex);
108
- mAudioExtractor.seekTo(0, MediaExtractor.SEEK_TO_PREVIOUS_SYNC);
109
- MediaFormat trackFormat = mAudioExtractor.getTrackFormat(audioIndex);
110
- // muxerAudioTrackIndex = mMuxer.addTrack(trackFormat);
111
-
112
- // extractor.unselectTrack(muxerAudioTrackIndex);
113
- }
114
-
115
- /**
116
- * mediacodec + surface + opengl
117
- * */
118
- Log.d("CompressAudio", "CompressAudio: ");
119
- int videoIndex = selectTrack(extractor, true);
120
-
121
- if (videoIndex >= 0) {
122
-
123
- long videoTime = -1;
124
- boolean outputDone = false;
125
- boolean inputDone = false;
126
- boolean decoderDone = false;
127
- int swapUV = 0;
128
- int videoTrackIndex = MEDIATYPE_NOT_AUDIO_VIDEO;
129
-
130
-
131
- extractor.selectTrack(videoIndex);
132
- if (startTime > 0) {
133
- extractor.seekTo(startTime, MediaExtractor.SEEK_TO_PREVIOUS_SYNC);
134
- } else {
135
- extractor.seekTo(0, MediaExtractor.SEEK_TO_PREVIOUS_SYNC);
136
- }
137
- MediaFormat inputFormat = extractor.getTrackFormat(videoIndex);
138
-
139
-
140
- /**
141
- ** init mediacodec / encoder and decoder
142
- **/
143
- prepareEncoder(inputFormat);
144
-
145
-
146
- ByteBuffer[] decoderInputBuffers = null;
147
- ByteBuffer[] encoderOutputBuffers = null;
148
-
149
-
150
- decoderInputBuffers = mDecoder.getInputBuffers();
151
- encoderOutputBuffers = mEncoder.getOutputBuffers();
152
-
153
-
154
- while (!outputDone) {
155
- if (!inputDone) {
156
- boolean eof = false;
157
- int index = extractor.getSampleTrackIndex();
158
- if (index == videoIndex) {
159
- int inputBufIndex = mDecoder.dequeueInputBuffer(TIMEOUT_USEC);
160
- if (inputBufIndex >= 0) {
161
- ByteBuffer inputBuf;
162
- if (Build.VERSION.SDK_INT < 21) {
163
- inputBuf = decoderInputBuffers[inputBufIndex];
164
- } else {
165
- inputBuf = mDecoder.getInputBuffer(inputBufIndex);
166
- }
167
- int chunkSize = extractor.readSampleData(inputBuf, 0);
168
- if (chunkSize < 0) {
169
- mDecoder.queueInputBuffer(inputBufIndex, 0, 0, 0L, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
170
- inputDone = true;
171
- } else {
172
- mDecoder.queueInputBuffer(inputBufIndex, 0, chunkSize, extractor.getSampleTime(), 0);
173
- extractor.advance();
174
- }
175
- }
176
- } else if (index == -1) {
177
- eof = true;
178
- }
179
- if (eof) {
180
- int inputBufIndex = mDecoder.dequeueInputBuffer(TIMEOUT_USEC);
181
- if (inputBufIndex >= 0) {
182
- mDecoder.queueInputBuffer(inputBufIndex, 0, 0, 0L, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
183
- inputDone = true;
184
- }
185
- }
186
- }
187
-
188
- boolean decoderOutputAvailable = !decoderDone;
189
- boolean encoderOutputAvailable = true;
190
-
191
- while (decoderOutputAvailable || encoderOutputAvailable) {
192
-
193
- int encoderStatus = mEncoder.dequeueOutputBuffer(mBufferInfo, TIMEOUT_USEC);
194
- if (encoderStatus == MediaCodec.INFO_TRY_AGAIN_LATER) {
195
- encoderOutputAvailable = false;
196
- } else if (encoderStatus == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
197
- if (Build.VERSION.SDK_INT < 21) {
198
- encoderOutputBuffers = mEncoder.getOutputBuffers();
199
- }
200
- } else if (encoderStatus == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
201
- MediaFormat newFormat = mEncoder.getOutputFormat();
202
- if (videoTrackIndex == MEDIATYPE_NOT_AUDIO_VIDEO) {
203
- videoTrackIndex = mMuxer.addTrack(newFormat);
204
- mTrackIndex = videoTrackIndex;
205
- mMuxer.start();
206
- }
207
- } else if (encoderStatus < 0) {
208
- throw new RuntimeException("unexpected result from mEncoder.dequeueOutputBuffer: " + encoderStatus);
209
- } else {
210
- ByteBuffer encodedData;
211
- if (Build.VERSION.SDK_INT < 21) {
212
- encodedData = encoderOutputBuffers[encoderStatus];
213
- } else {
214
- encodedData = mEncoder.getOutputBuffer(encoderStatus);
215
- }
216
- if (encodedData == null) {
217
- throw new RuntimeException("encoderOutputBuffer " + encoderStatus + " was null");
218
- }
219
- if (mBufferInfo.size > 1) {
220
- if ((mBufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) == 0) {
221
- mMuxer.writeSampleData(videoTrackIndex, encodedData, mBufferInfo);
222
- } else if (videoTrackIndex == MEDIATYPE_NOT_AUDIO_VIDEO) {
223
- byte[] csd = new byte[mBufferInfo.size];
224
- encodedData.limit(mBufferInfo.offset + mBufferInfo.size);
225
- encodedData.position(mBufferInfo.offset);
226
- encodedData.get(csd);
227
- ByteBuffer sps = null;
228
- ByteBuffer pps = null;
229
- for (int a = mBufferInfo.size - 1; a >= 0; a--) {
230
- if (a > 3) {
231
- if (csd[a] == 1 && csd[a - 1] == 0 && csd[a - 2] == 0 && csd[a - 3] == 0) {
232
- sps = ByteBuffer.allocate(a - 3);
233
- pps = ByteBuffer.allocate(mBufferInfo.size - (a - 3));
234
- sps.put(csd, 0, a - 3).position(0);
235
- pps.put(csd, a - 3, mBufferInfo.size - (a - 3)).position(0);
236
- break;
237
- }
238
- } else {
239
- break;
240
- }
241
- }
242
-
243
- MediaFormat newFormat = MediaFormat.createAudioFormat(MIME_TYPE, nbitrate, 1);
244
- if (sps != null && pps != null) {
245
- newFormat.setByteBuffer("csd-0", sps);
246
- newFormat.setByteBuffer("csd-1", pps);
247
- }
248
- videoTrackIndex = mMuxer.addTrack(newFormat);
249
- mMuxer.start();
250
- }
251
- }
252
- outputDone = (mBufferInfo.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0;
253
- mEncoder.releaseOutputBuffer(encoderStatus, false);
254
- }
255
- if (encoderStatus != MediaCodec.INFO_TRY_AGAIN_LATER) {
256
- continue;
257
- }
258
-
259
- if (!decoderDone) {
260
- int decoderStatus = mDecoder.dequeueOutputBuffer(mBufferInfo, TIMEOUT_USEC);
261
- if (decoderStatus == MediaCodec.INFO_TRY_AGAIN_LATER) {
262
- decoderOutputAvailable = false;
263
- } else if (decoderStatus == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
264
-
265
- } else if (decoderStatus == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
266
- MediaFormat newFormat = mDecoder.getOutputFormat();
267
- Log.e(TAG, "newFormat = " + newFormat);
268
- } else if (decoderStatus < 0) {
269
- throw new RuntimeException("unexpected result from mDecoder.dequeueOutputBuffer: " + decoderStatus);
270
- } else {
271
- boolean doRender = false;
272
-
273
- doRender = mBufferInfo.size != 0;
274
-
275
- if (endTime > 0 && mBufferInfo.presentationTimeUs >= endTime) {
276
- inputDone = true;
277
- decoderDone = true;
278
- doRender = false;
279
- mBufferInfo.flags |= MediaCodec.BUFFER_FLAG_END_OF_STREAM;
280
- }
281
- if (startTime > 0 && videoTime == -1) {
282
- if (mBufferInfo.presentationTimeUs < startTime) {
283
- doRender = false;
284
- Log.e(TAG, "drop frame startTime = " + startTime + " present time = " + mBufferInfo.presentationTimeUs);
285
- } else {
286
- videoTime = mBufferInfo.presentationTimeUs;
287
- }
288
- }
289
- mDecoder.releaseOutputBuffer(decoderStatus, doRender);
290
- if ((mBufferInfo.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
291
- decoderOutputAvailable = false;
292
- outputDone=true;
293
- Log.e(TAG, "decoder stream end");
294
- }
295
- }
296
- }
297
- }
298
- }
299
- }
300
-
301
- extractor.unselectTrack(videoIndex);
302
-
303
-
304
- writeAudioTrack(mAudioExtractor, mMuxer, mBufferInfo, cacheFile, muxerAudioTrackIndex);
305
-
306
- } catch (Exception e) {
307
- error = true;
308
- Log.e(TAG, e.getMessage());
309
- } finally {
310
- if (extractor != null) {
311
- extractor.release();
312
- extractor = null;
313
- }
314
-
315
-
316
- if (mAudioExtractor != null) {
317
- mAudioExtractor.release();
318
- mAudioExtractor = null;
319
- }
320
- Log.e(TAG, "time = " + (System.currentTimeMillis() - time));
321
- }
322
-
323
-
324
- Log.e(TAG+" Path", path + "");
325
- Log.e(TAG+" Path", cacheFile.getPath() + "");
326
- Log.e(TAG+" Path", inputFile.getPath() + "");
327
-
328
-
329
-
330
- releaseCoder();
331
-
332
- if(error)
333
- return false;
334
- else
335
- return true;
336
- }
337
-
338
-
339
- private boolean checkParmsError(String sourcePath, String destinationPath, int nbitrate) {
340
-
341
-
342
- if (nbitrate <= 0)
343
- return true;
344
- else
345
- return false;
346
-
347
- }
348
-
349
-
350
- @SuppressLint("WrongConstant")
351
- @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
352
- private long simpleReadAndWriteTrack(MediaExtractor extractor, MediaMuxer mediaMuxer, MediaCodec.BufferInfo info, long start, long end, File file, boolean isAudio) throws Exception {
353
- int trackIndex = selectTrack(extractor, isAudio);
354
- if (trackIndex >= 0) {
355
- extractor.selectTrack(trackIndex);
356
- MediaFormat trackFormat = extractor.getTrackFormat(trackIndex);
357
- int muxerTrackIndex = mediaMuxer.addTrack(trackFormat);
358
-
359
- if(!isAudio)
360
- mediaMuxer.start();
361
-
362
- int maxBufferSize = trackFormat.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
363
- boolean inputDone = false;
364
- if (start > 0) {
365
- extractor.seekTo(start, MediaExtractor.SEEK_TO_PREVIOUS_SYNC);
366
- } else {
367
- extractor.seekTo(0, MediaExtractor.SEEK_TO_PREVIOUS_SYNC);
368
- }
369
- ByteBuffer buffer = ByteBuffer.allocateDirect(maxBufferSize);
370
- long startTime = -1;
371
-
372
- while (!inputDone) {
373
-
374
- boolean eof = false;
375
- int index = extractor.getSampleTrackIndex();
376
- if (index == trackIndex) {
377
- info.size = extractor.readSampleData(buffer, 0);
378
-
379
- if (info.size < 0) {
380
- info.size = 0;
381
- eof = true;
382
- } else {
383
- info.presentationTimeUs = extractor.getSampleTime();
384
- if (start > 0 && startTime == -1) {
385
- startTime = info.presentationTimeUs;
386
- }
387
- if (end < 0 || info.presentationTimeUs < end) {
388
- info.offset = 0;
389
- info.flags = extractor.getSampleFlags();
390
- mediaMuxer.writeSampleData(muxerTrackIndex, buffer, info);
391
- extractor.advance();
392
- } else {
393
- eof = true;
394
- }
395
- }
396
- } else if (index == -1) {
397
- eof = true;
398
- }
399
- if (eof) {
400
- inputDone = true;
401
- }
402
- }
403
-
404
- extractor.unselectTrack(trackIndex);
405
- return startTime;
406
- }
407
- return -1;
408
- }
409
-
410
- @SuppressLint("WrongConstant")
411
- @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
412
- private long writeAudioTrack(MediaExtractor extractor, MediaMuxer mediaMuxer, MediaCodec.BufferInfo info, File file, int muxerTrackIndex ) throws Exception {
413
- int trackIndex = selectTrack(extractor, true);
414
- if (trackIndex >= 0) {
415
-
416
- MediaFormat trackFormat = extractor.getTrackFormat(trackIndex);
417
- int maxBufferSize = info.size;
418
- ByteBuffer byteBuffer = ByteBuffer.allocate(maxBufferSize);
419
-
420
- long audioPresentationTimeUs = 0;
421
- MediaCodec.BufferInfo audioBufferInfo = new MediaCodec.BufferInfo();
422
- extractor.selectTrack(mTrackIndex);
423
- /*
424
- * the last audio presentation time.
425
- */
426
- long lastEndAudioTimeUs = 0;
427
- while (true) {
428
- int readAudioSampleSize = extractor.readSampleData(byteBuffer, 0);
429
- if (readAudioSampleSize < 0) {
430
- //if end of the stream, unselect
431
- extractor.unselectTrack(mTrackIndex);
432
- if (audioPresentationTimeUs >= 0) {
433
- //if has reach the end of the video time ,just exit
434
- break;
435
- } else {
436
- //if not the end of the video time, just repeat.
437
- lastEndAudioTimeUs += audioPresentationTimeUs;
438
- extractor.selectTrack(trackIndex);
439
- continue;
440
- }
441
- }
442
-
443
- long audioSampleTime = extractor.getSampleTime();
444
- audioBufferInfo.size = readAudioSampleSize;
445
- audioBufferInfo.presentationTimeUs = audioSampleTime + lastEndAudioTimeUs;
446
- if (audioBufferInfo.presentationTimeUs > 0) {
447
- extractor.unselectTrack(trackIndex);
448
- break;
449
- }
450
- audioPresentationTimeUs = 0;
451
- audioBufferInfo.offset = 0;
452
- audioBufferInfo.flags = extractor.getSampleFlags();
453
- mediaMuxer.writeSampleData(trackIndex, byteBuffer, audioBufferInfo);
454
- extractor.advance();
455
- }
456
- //
457
-
458
-
459
-
460
- // boolean inputDone = false;
461
- // if (start > 0) {
462
- // extractor.seekTo(start, MediaExtractor.SEEK_TO_PREVIOUS_SYNC);
463
- // } else {
464
- // extractor.seekTo(0, MediaExtractor.SEEK_TO_PREVIOUS_SYNC);
465
- // }
466
- // ByteBuffer buffer = ByteBuffer.allocateDirect(maxBufferSize);
467
- // long startTime = -1;
468
- //
469
- // while (!inputDone) {
470
- //
471
- // boolean eof = false;
472
- // int index = extractor.getSampleTrackIndex();
473
- // if (index == trackIndex) {
474
- // info.size = extractor.readSampleData(buffer, 0);
475
- //
476
- // if (info.size < 0) {
477
- // info.size = 0;
478
- // eof = true;
479
- // } else {
480
- // info.presentationTimeUs = extractor.getSampleTime();
481
- // if (start > 0 && startTime == -1) {
482
- // startTime = info.presentationTimeUs;
483
- // }
484
- // if (end < 0 || info.presentationTimeUs < end) {
485
- // info.offset = 0;
486
- // info.flags = extractor.getSampleFlags();
487
- // mediaMuxer.writeSampleData(muxerTrackIndex, buffer, info);
488
- // extractor.advance();
489
- // } else {
490
- // eof = true;
491
- // }
492
- // }
493
- // } else if (index == -1) {
494
- // eof = true;
495
- // }
496
- // if (eof) {
497
- // inputDone = true;
498
- // }
499
- // }
500
- //
501
- // extractor.unselectTrack(trackIndex);
502
- // return startTime;
503
- }
504
- return -1;
505
- }
506
-
507
-
508
- private int selectTrack(MediaExtractor extractor, boolean audio) {
509
- int numTracks = extractor.getTrackCount();
510
- for (int i = 0; i < numTracks; i++) {
511
- MediaFormat format = extractor.getTrackFormat(i);
512
- String mime = format.getString(MediaFormat.KEY_MIME);
513
- if (audio) {
514
- if (mime.startsWith("audio/")) {
515
- return i;
516
- }
517
- } else {
518
- if (mime.startsWith("video/")) {
519
- return i;
520
- }
521
- }
522
- }
523
- return MEDIATYPE_NOT_AUDIO_VIDEO;
524
- }
525
-
526
- private String selectEncoder(String mime) {
527
- for (int index = 0; index < MediaCodecList.getCodecCount(); index++) {
528
- MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(index);
529
-
530
- if (!codecInfo.isEncoder()) {
531
- continue;
532
- }
533
-
534
- for (String type : codecInfo.getSupportedTypes()) {
535
- if (type.equalsIgnoreCase(mime)) {
536
- return MIME_TYPE;
537
- }
538
- }
539
- }
540
- return null;
541
- }
542
-
543
-
544
- /**
545
- * Configures encoder and muxer state, and prepares the input Surface.
546
- */
547
- @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
548
- private void prepareEncoder(MediaFormat format) {
549
- mBufferInfo = new MediaCodec.BufferInfo();
550
-
551
- MediaFormat mAudioFormat = MediaFormat.createAudioFormat(MIME_TYPE, mBitRate, 1);
552
- if (VERBOSE) Log.d(TAG, "format: " + mAudioFormat);
553
- mAudioFormat.setInteger(MediaFormat.KEY_SAMPLE_RATE, format.getInteger(MediaFormat.KEY_SAMPLE_RATE));
554
- mAudioFormat.setInteger(MediaFormat.KEY_BIT_RATE, format.getInteger(MediaFormat.KEY_BIT_RATE));
555
- mAudioFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, format.getInteger(MediaFormat.KEY_CHANNEL_COUNT));
556
- mAudioFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
557
- mAudioFormat.setString(MediaFormat.KEY_MIME, MIME_TYPE);
558
- mAudioFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 10 * 1024);
559
- try {
560
- mEncoder = MediaCodec.createEncoderByType(MIME_TYPE);
561
- mEncoder.configure(mAudioFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
562
- mEncoder.start();
563
- Log.d(TAG, "prepareEncoder...");
564
- } catch (IOException e) {
565
- e.printStackTrace();
566
- }
567
-
568
- try {
569
- mDecoder = MediaCodec.createDecoderByType(format.getString(MediaFormat.KEY_MIME));
570
- } catch (IOException e) {
571
- e.printStackTrace();
572
- }
573
- mDecoder.configure(format, null, null, 0);
574
- mDecoder.start();
575
- // mTrackIndex = -1;
576
- }
577
-
578
- /**
579
- * Releases encoder resources. May be called after partial / failed initialization.
580
- */
581
- @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
582
- private void releaseCoder() {
583
- if (VERBOSE) Log.d(TAG, "releasing encoder objects");
584
- if (mEncoder != null) {
585
- mEncoder.stop();
586
- mEncoder.release();
587
- mEncoder = null;
588
- }
589
- if (mDecoder != null) {
590
- mDecoder.stop();
591
- mDecoder.release();
592
- mDecoder = null;
593
- }
594
- if (mInputSurface != null) {
595
- mInputSurface.release();
596
- mInputSurface = null;
597
- }
598
- if (mMuxer != null) {
599
- mMuxer.stop();
600
- mMuxer.release();
601
- mMuxer = null;
602
- }
603
- }
604
-
605
- }