Skip to content

fix(android): resolve H264 video on android #2083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'com.android.library'
def DEFAULT_COMPILE_SDK_VERSION = 30
def DEFAULT_BUILD_TOOLS_VERSION = "29.0.3"
def DEFAULT_TARGET_SDK_VERSION = 30
def DEFAULT_MIN_SDK_VERSION = 16
def DEFAULT_MIN_SDK_VERSION = 21

android {
compileSdkVersion rootProject.hasProperty('compileSdkVersion') ? rootProject.compileSdkVersion : DEFAULT_COMPILE_SDK_VERSION
Expand All @@ -23,4 +23,8 @@ dependencies {
implementation 'com.facebook.react:react-native:+'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
implementation 'com.github.yalantis:ucrop:2.2.6-native'

implementation 'androidx.media3:media3-transformer:1.4.1'
implementation 'androidx.media3:media3-effect:1.4.1'
implementation 'androidx.media3:media3-common:1.4.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
import android.util.Log;
import android.util.Pair;

import androidx.annotation.OptIn;
import androidx.media3.common.MediaItem;
import androidx.media3.common.MimeTypes;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.transformer.Composition;
import androidx.media3.transformer.EditedMediaItem;
import androidx.media3.transformer.ExportException;
import androidx.media3.transformer.ExportResult;
import androidx.media3.transformer.Transformer;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReadableMap;

Expand Down Expand Up @@ -153,9 +163,36 @@ private Pair<Integer, Integer> calculateTargetDimensions(int currentWidth, int c
return Pair.create(width, height);
}

synchronized void compressVideo(final Activity activity, final ReadableMap options, final String originalVideo, final String compressedVideo, final Promise promise) {
// todo: video compression
@OptIn(markerClass = UnstableApi.class)
synchronized void compressVideo(
final Activity activity, final ReadableMap options, final String originalVideo, final String compressedVideo, final Promise promise) {
// failed attempt 1: ffmpeg => slow and licensing issues
promise.resolve(originalVideo);
// promise.resolve(originalVideo);

Transformer.Listener transformerListener =
new Transformer.Listener() {
@Override
public void onCompleted(Composition composition, ExportResult result) {
promise.resolve(compressedVideo);
}

@Override
public void onError(Composition composition, ExportResult result,
ExportException exception) {
promise.reject("unable to convert input to h264");
}
};

MediaItem inputMediaItem = MediaItem.fromUri(originalVideo);
EditedMediaItem editedMediaItem =
new EditedMediaItem.Builder(inputMediaItem).setFrameRate(15).build();
Transformer transformer =
new Transformer.Builder(activity)
.setVideoMimeType(MimeTypes.VIDEO_H264)
.setAudioMimeType(MimeTypes.AUDIO_AAC)
.addListener(transformerListener)
.build();
transformer.start(editedMediaItem, compressedVideo);

}
}