视频裁剪:
- 最近遇到了一个如下的需要, 对原片进行裁剪
经营第一次裁剪视频, 会对视频进行删减(掐掉片头片尾之类), (此处疏忽视频的总时长, 单位秒)
[10, 15]
[20, 70]
- 以上的两个片段会合并成一个新的视频:
(15-10)+(70-20)=55
经营第二次裁剪视频的时候, 会对第一次裁剪后的视频裁剪(如删除两头局部, 在之前的片头片尾根底上)
- 裁剪的工夫应该用第一次裁剪的工夫根底上计算
- 理论的裁剪工夫应该从原片上计算
说来简单,用例子阐明一下
$first = [ // F1 [10, 15], // F2 [20, 70],];$second = [ // S1 [2, 3], // S2 [4, 9], // S3 [45, 55],];## 理论应该返回一个列表$output = [ // S1 在 F1 失去片段 [12, 13] // S2 在 F1 失去的片段 [14, 15] // S2 在 F2 失去的片段 [20, 24] // S3 在 F2 失去的片段 [60, 70]];
- 通过下面的过程之后,拿到
$output
的后果, 再去原片裁剪即可.
代码如下
$first = [ [10, 15], [20, 70],];$second = [ // 这个是第一段够的 [2, 3], // 第一段不够, 用第二段来补全 [4, 9], // 这个间接跳到第二段 [45, 55],];var_dump(makeSections($first, $second));function makeSections(array $firstCutSections, array $secondCutSections) : array{ // 不论是哪一个为空, 间接返回另外一个即可 if (empty($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 != 0) { // 如果没有可用的时间段, 取出一个来用 if ($usableLength == 0) { $sec = array_shift($usableSections); if (is_null($sec)) { throw new Exception('第二次截取的视频比第一次长'); } $usableLength = $sec[1] - $sec[0]; $start = $sec[0]; continue; } // 如果坐标没对齐, 那么去对齐坐标 if ($remainStart > 0) { // 两种状况 if ($remainStart > $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;}
博客原文https://www.shiguopeng.cn/archives/522