Skip to content

Commit

Permalink
Functional but slow merging (shahen94#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenBrostoff authored and Shahen Hovhannisyan committed Sep 27, 2018
1 parent cf9afd9 commit 9a47db9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# MacOS temporary files
.DS_Store

# Webstorm
.idea

# Logs
logs
*.log
Expand Down
32 changes: 32 additions & 0 deletions android/src/main/java/com/shahenlibrary/Trimmer/Trimmer.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import android.util.Base64;
import android.util.Log;

import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
Expand Down Expand Up @@ -739,6 +740,37 @@ static void reverse(String source, final Promise promise, ReactApplicationContex
executeFfmpegCommand(cmd, tempFile.getPath(), ctx, promise, "Reverse error", null);
}

static void merge(ReadableArray videoFiles, String concatCmd, final Promise promise, ReactApplicationContext ctx) {
final File tempFile = createTempFile("mp4", promise, ctx);

Log.d(LOG_TAG, "Merging in progress.");

ArrayList<String> cmd = new ArrayList<String>();
cmd.add("-y"); // NOTE: OVERWRITE OUTPUT FILE

for (int i = 0; i < videoFiles.size(); i++) {
cmd.add("-i");
cmd.add(videoFiles.getString(i));
}

cmd.add("-filter_complex");
cmd.add(concatCmd);

cmd.add("-map");
cmd.add("[v]");

cmd.add("-preset");
cmd.add("ultrafast");

// NOTE: DO NOT CONVERT AUDIO TO SAVE TIME
cmd.add("-c:a");

cmd.add("copy");
cmd.add(tempFile.getPath());

executeFfmpegCommand(cmd, tempFile.getPath(), ctx, promise, "Merge error", null);
}

static private Void executeFfmpegCommand(@NonNull ArrayList<String> cmd, @NonNull final String pathToProcessingFile, @NonNull Context ctx, @NonNull final Promise promise, @NonNull final String errorMessageTitle, @Nullable final OnCompressVideoListener cb) {
FfmpegCmdAsyncTaskParams ffmpegCmdAsyncTaskParams = new FfmpegCmdAsyncTaskParams(cmd, pathToProcessingFile, ctx, promise, errorMessageTitle, cb);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.support.annotation.NonNull;
import android.util.Log;

import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
Expand Down Expand Up @@ -100,6 +101,12 @@ public void reverse(String path, Promise promise) {
Trimmer.reverse(path, promise, reactContext);
}

@ReactMethod
public void merge(ReadableArray videoFiles, String cmd, Promise promise) {
Log.d(REACT_PACKAGE, "Sending command: " + cmd);
Trimmer.merge(videoFiles, cmd, promise, reactContext);
}

@ReactMethod
private void loadFfmpeg() {
Trimmer.loadFfmpeg(reactContext);
Expand Down
5 changes: 5 additions & 0 deletions lib/ProcessingManager/ProcessingManager.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { NativeModules } from 'react-native';
import type {
sourceType,
arrayType,
trimOptions,
previewMaxSize,
format,
Expand Down Expand Up @@ -74,4 +75,8 @@ export class ProcessingManager {
.then((res) => res.source);
}

static merge(readableFiles: arrayType, cmd: string): Promise<string> {
return TrimmerManager.merge(readableFiles, cmd).then((res) => res.source);
}

}
3 changes: 3 additions & 0 deletions lib/ProcessingManager/types.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ export type cropOptions = {
// quality: ?trimQuality
};

export type arrayType = Array<string>;

declare class RNTrimmerManager {
static trim(source: string, options: trimOptions): Promise<{ source: string }>;
static compress(source: string, options: any): Promise<*>;
static getVideoInfo(source: string): Promise<*>;
static getPreviewImages(source: string): Promise<*>;
static getPreviewImageAtPosition(source: string, second: number): Promise<{ image: string }>;
static crop(source: string, options: cropOptions): Promise<{ source: string }>;
static merge(source: arrayType, cmd: string): Promise<{ source: string }>;
}

0 comments on commit 9a47db9

Please sign in to comment.