Advertisement
Guest User

Untitled

a guest
Aug 4th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.92 KB | None | 0 0
  1. public void startTrim(@NonNull String input, @NonNull String dstDir, long startMs, long endMs) throws IOException {
  2.         File src = new File(input);
  3.         final String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
  4.         final String fileName = "MP4_" + timeStamp + ".mp4";
  5.         final String filePath = dstDir + fileName;
  6.  
  7.         File file = new File(filePath);
  8.         file.getParentFile().mkdirs();
  9.         genVideoUsingMp4Parser(src, file, startMs, endMs);
  10.     }
  11.  
  12.     private static void genVideoUsingMp4Parser(@NonNull File src, @NonNull File dst, long startMs, long endMs) throws IOException {
  13.         // NOTE: Switched to using FileDataSourceViaHeapImpl since it does not use memory mapping (VM).
  14.         // Otherwise we get OOM with large movie files.
  15.         Movie movie = MovieCreator.build(new FileDataSourceViaHeapImpl(src.getAbsolutePath()));
  16.  
  17.         List<Track> tracks = movie.getTracks();
  18.         movie.setTracks(new LinkedList<Track>());
  19.         // remove all tracks we will create new tracks from the old
  20.  
  21.         double startTime1 = startMs / 1000;
  22.         double endTime1 = endMs / 1000;
  23.  
  24.         boolean timeCorrected = false;
  25.  
  26.         // Here we try to find a track that has sync samples. Since we can only start decoding
  27.         // at such a sample we SHOULD make sure that the start of the new fragment is exactly
  28.         // such a frame
  29.         for (Track track : tracks) {
  30.             if (track.getSyncSamples() != null && track.getSyncSamples().length > 0) {
  31.                 if (timeCorrected) {
  32.                     // This exception here could be a false positive in case we have multiple tracks
  33.                     // with sync samples at exactly the same positions. E.g. a single movie containing
  34.                     // multiple qualities of the same video (Microsoft Smooth Streaming file)
  35.  
  36.                     throw new RuntimeException("The startTime has already been corrected by another track with SyncSample. Not Supported.");
  37.                 }
  38.                 startTime1 = correctTimeToSyncSample(track, startTime1, false);
  39.                 endTime1 = correctTimeToSyncSample(track, endTime1, true);
  40.                 timeCorrected = true;
  41.             }
  42.         }
  43.  
  44.         for (Track track : tracks) {
  45.             long currentSample = 0;
  46.             double currentTime = 0;
  47.             double lastTime = -1;
  48.             long startSample1 = -1;
  49.             long endSample1 = -1;
  50.  
  51.             for (int i = 0; i < track.getSampleDurations().length; i++) {
  52.                 long delta = track.getSampleDurations()[i];
  53.  
  54.  
  55.                 if (currentTime > lastTime && currentTime <= startTime1) {
  56.                     // current sample is still before the new starttime
  57.                     startSample1 = currentSample;
  58.                 }
  59.                 if (currentTime > lastTime && currentTime <= endTime1) {
  60.                     // current sample is after the new start time and still before the new endtime
  61.                     endSample1 = currentSample;
  62.                 }
  63.                 lastTime = currentTime;
  64.                 currentTime += (double) delta / (double) track.getTrackMetaData().getTimescale();
  65.                 currentSample++;
  66.             }
  67.             movie.addTrack(new AppendTrack(new CroppedTrack(track, startSample1, endSample1)));
  68.         }
  69.  
  70.         dst.getParentFile().mkdirs();
  71.  
  72.         if (!dst.exists()) {
  73.             dst.createNewFile();
  74.         }
  75.  
  76.         Container out = new DefaultMp4Builder().build(movie);
  77.  
  78.         FileOutputStream fos = new FileOutputStream(dst);
  79.         FileChannel fc = fos.getChannel();
  80.         out.writeContainer(fc);
  81.  
  82.         fc.close();
  83.         fos.close();
  84.     }
  85.  
  86.     private static double correctTimeToSyncSample(@NonNull Track track, double cutHere, boolean next) {
  87.         double[] timeOfSyncSamples = new double[track.getSyncSamples().length];
  88.         long currentSample = 0;
  89.         double currentTime = 0;
  90.         for (int i = 0; i < track.getSampleDurations().length; i++) {
  91.             long delta = track.getSampleDurations()[i];
  92.  
  93.             if (Arrays.binarySearch(track.getSyncSamples(), currentSample + 1) >= 0) {
  94.                 // samples always start with 1 but we start with zero therefore +1
  95.                 timeOfSyncSamples[Arrays.binarySearch(track.getSyncSamples(), currentSample + 1)] = currentTime;
  96.             }
  97.             currentTime += (double) delta / (double) track.getTrackMetaData().getTimescale();
  98.             currentSample++;
  99.  
  100.         }
  101.         double previous = 0;
  102.         for (double timeOfSyncSample : timeOfSyncSamples) {
  103.             if (timeOfSyncSample > cutHere) {
  104.                 if (next) {
  105.                     return timeOfSyncSample;
  106.                 } else {
  107.                     return previous;
  108.                 }
  109.             }
  110.             previous = timeOfSyncSample;
  111.         }
  112.         return timeOfSyncSamples[timeOfSyncSamples.length - 1];
  113.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement