Video cropping:

  • I recently encountered a need to crop the original
  • In the first operation of cutting video, the video will be deleted (cutting off the beginning and end of the video), (the total duration of the video is ignored here, in seconds).
    • [10, 15]
    • [20, 70]
  • The above two clips will be combined into a new video:(15-10) + (70-20) = 55
  • When operating the second clip, clip the video after the first clip (such as deleting the middle part on the basis of the previous clip).
    • The clipping time should be calculated on the basis of the first clipping time
    • The actual cutting time should be calculated from the original piece

It’s complicated, but let me give you an example

$first = [ // F1 [10, 15], // F2 [20, 70], ]; $second = [ // S1 [2, 3], // S2 [4, 9], // S3 [45, 55], ]; $output = [// S1 gets fragments from F1 [12, 13] // S2 gets fragments from F1 [14, 15] // S2 gets fragments from F2 [20, 24] // The fragment of S3 obtained in F2 [60, 70]];Copy the code
  • After going through the above process, get$outputThe result, then go to the original piece can be cut.

The following code

$first = [ [10, 15], [20, 70], ]; $second = [1, 2]; $second = [2, 3]; $second = [2, 3]; var_dump(makeSections($first, $second)); function makeSections(array $firstCutSections, array $secondCutSections) : If ($firstCutSections)) {return $secondCutSections; } if (empty($secondCutSections)) { return $firstCutSections; } $newSections = []; foreach ($secondCutSections as $currSection) { $usableSections = $firstCutSections; $start = 0; $usableLength = 0; $remainLength = $currSection[1] - $currSection[0]; $remainStart = $currSection[0]; while ($remainLength ! $SEC = array_shift($usableSections); // If ($usableLength == 0) {$SEC = array_shift($usableSections); If (is_null($SEC)) {throw new Exception(' Second clip is longer than first clip '); } $usableLength = $sec[1] - $sec[0]; $start = $sec[0]; continue; If ($currentStart > $usableLength) {$remainStart -= $usableLength;  $start += $usableLength; $usableLength = 0; } else { $usableLength -= $remainStart; $start += $remainStart; $remainStart = 0; } continue; } $contentLength = 0; if ($remainLength > $usableLength) { $contentLength = $usableLength; $remainLength -= $usableLength; $usableLength = 0; } else { $contentLength = $remainLength; $usableLength -= $remainLength; $remainLength = 0; } var_dump($contentLength); $newSections[] = [$start, $start + $contentLength,]; } } return $newSections; }Copy the code

The original blog www.shiguopeng.cn/archives/52…