diff --git a/videotrimmer/src/main/java/com/ahmedbadereldin/videotrimmer/Utility.java b/videotrimmer/src/main/java/com/ahmedbadereldin/videotrimmer/Utility.java index 71310bc..624fc92 100644 --- a/videotrimmer/src/main/java/com/ahmedbadereldin/videotrimmer/Utility.java +++ b/videotrimmer/src/main/java/com/ahmedbadereldin/videotrimmer/Utility.java @@ -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; @@ -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"; @@ -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; } } @@ -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();