视频裁剪:
- 最近遇到了一个如下的需求, 对原片进行裁剪
- 运营第一次裁剪视频, 会对视频进行删减(掐掉片头片尾之类), (此处忽略视频的总时长, 单位秒)
[10, 15]
[20, 70]
- 以上的两个片段会合并成一个新的视频:
(15-10)+(70-20)=55
- 运营第二次裁剪视频的时候, 会对第一次裁剪后的视频裁剪(如删除中间部分, 在之前的片头片尾基础上)
- 裁剪的时间应该用第一次裁剪的时间基础上计算
- 实际的裁剪时间应该从原片上计算
说来复杂,用例子说明一下
| $first = [ |
| |
| [10, 15], |
| |
| [20, 70], |
| ]; |
| |
| $second = [ |
| |
| [2, 3], |
| |
| [4, 9], |
| |
| [45, 55], |
| ]; |
| |
| |
| ## 实际应该返回一个列表 |
| $output = [ |
| |
| [12, 13] |
| |
| [14, 15] |
| |
| [20, 24] |
| |
| [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; |
| } |
博客原文www.shiguopeng.cn/archives/522