Skip to content

Commit

Permalink
Merge pull request #4 from rahulsain/master
Browse files Browse the repository at this point in the history
Fixed Video Trim Crash
  • Loading branch information
Ahmedbadereldin authored Mar 1, 2022
2 parents 4881a21 + 62fd693 commit 62cdb44
Showing 1 changed file with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.ahmedbadereldin.videotrimmer.customVideoViews.OnVideoTrimListener;
import com.coremedia.iso.boxes.Container;
Expand All @@ -23,11 +21,10 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

public class Utility {
public static final String VIDEO_FORMAT = ".mp4";
Expand Down Expand Up @@ -69,8 +66,8 @@ private static void generateVideo(@NonNull File src, @NonNull File dst, long sta

throw new RuntimeException("The startTime has already been corrected by another track with SyncSample. Not Supported.");
}
// startTime1 = correctTimeToSyncSample(track, startTime1, false);
// endTime1 = correctTimeToSyncSample(track, endTime1, true);
startTime1 = correctTimeToSyncSample(track, startTime1, false);
endTime1 = correctTimeToSyncSample(track, endTime1, true);
timeCorrected = true;
}
}
Expand Down Expand Up @@ -117,6 +114,34 @@ private static void generateVideo(@NonNull File src, @NonNull File dst, long sta

}

private static double correctTimeToSyncSample(Track track, double cutHere, boolean next) {
double[] timeOfSyncSamples = new double[track.getSyncSamples().length];
long currentSample = 0;
double currentTime = 0;
for (int i = 0; i < track.getSampleDurations().length; i++) {
long delta = track.getSampleDurations()[i];
if (Arrays.binarySearch(track.getSyncSamples(), currentSample + 1) >= 0) {
// samples always start with 1 but we start with zero therefore +1
timeOfSyncSamples[Arrays.binarySearch(track.getSyncSamples(), currentSample + 1)] = currentTime;
}
currentTime += (double) delta / (double) track.getTrackMetaData().getTimescale();
currentSample++;
}
double previous = 0;
for (double timeOfSyncSample : timeOfSyncSamples) {
if (timeOfSyncSample > cutHere) {
if (next) {
return timeOfSyncSample;
} else {
return previous;
}
}
previous = timeOfSyncSample;
}
return timeOfSyncSamples[timeOfSyncSamples.length - 1];
}


private static File create(Activity activity, String dst) {
File file = new File(dst);
file.getParentFile().mkdirs();
Expand Down

0 comments on commit 62cdb44

Please sign in to comment.