-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathRNAudioTranscoder.java
More file actions
102 lines (88 loc) · 3.31 KB
/
RNAudioTranscoder.java
File metadata and controls
102 lines (88 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.rnaudiotranscoder;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import com.github.hiteshsondhi88.libffmpeg.ExecuteBinaryResponseHandler;
import com.github.hiteshsondhi88.libffmpeg.FFmpeg;
import com.github.hiteshsondhi88.libffmpeg.LoadBinaryResponseHandler;
import com.github.hiteshsondhi88.libffmpeg.exceptions.FFmpegCommandAlreadyRunningException;
import com.github.hiteshsondhi88.libffmpeg.exceptions.FFmpegNotSupportedException;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.modules.core.DeviceEventManagerModule;
public final class RNAudioTranscoder extends ReactContextBaseJavaModule {
private final FFmpeg ffmpeg;
public final String COMMAND_FORMAT = "-i %s -codec:a libmp3lame -qscale:a 2 %s";
public final String TAG = "RNAudioTranscoder";
public RNAudioTranscoder (final ReactApplicationContext context) {
super(context);
ffmpeg = FFmpeg.getInstance(context);
}
@Override
public final String getName() {
return "RNAudioTranscoder";
}
@ReactMethod
public final void transcode(final ReadableMap options, final Promise promise) {
final Optional<String> paramErrors = this.checkRequiredOptions(options);
if (paramErrors.exists) {
promise.reject(paramErrors.value);
} else {
try {
ffmpeg.loadBinary(new LoadBinaryResponseHandler() {
@Override
public void onFailure() {
Log.e(TAG, "Failed to load ffmpeg");
promise.reject("Failed to load ffmpeg binary");
}
@Override
public void onSuccess() {
final String input = options.getString("input");
final String output = options.getString("output");
final String[] command = createFFmpegCommand(input, output);
try {
ffmpeg.execute(command, new ExecuteBinaryResponseHandler() {
@Override
public void onFailure(String s) {
promise.reject(s);
}
@Override
public void onSuccess(String s) {
promise.resolve(makeMessagePayload(s));
}
});
} catch (FFmpegCommandAlreadyRunningException e) {
promise.reject(e.getMessage());
}
}
});
} catch (FFmpegNotSupportedException e) {
Log.e(TAG, "FFMPEG NOT SUPPORTED");
promise.reject(e.getMessage());
}
}
}
private final ReadableMap makeMessagePayload(final String message) {
final WritableMap payload = Arguments.createMap();
payload.putString("message", message);
return payload;
}
private final String[] createFFmpegCommand(final String input, final String output) {
String[] commandParts = COMMAND_FORMAT.split(" ");
commandParts[1] = input;
commandParts[6] = output;
return commandParts;
}
private final Optional<String> checkRequiredOptions(final ReadableMap options) {
if (!options.hasKey("input")) return Optional.of("Missing required parameter 'input'");
if (!options.hasKey("output")) return Optional.of("Missing required parameter 'output'");
return Optional.empty();
}
}