Video cropping:
- Recently encountered a demand as follows, to cut the original film
The first time a video is cropped in the operation, the video will be deleted (cut off the beginning and end of the movie), (the total duration of the video is ignored here, in seconds)
[10, 15]
[20, 70]
- The above two clips will be merged into a new video:
(15-10)+(70-20)=55
When operating the second video cropping, the video after the first cropping will be cropped (such as deleting the middle part, based on the previous credits)
- The cutting time should be calculated based on the first cutting time
- The actual cutting time should be calculated from the original film
It's complicated, let's illustrate with an example
$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]
];
- After the above process, get
$output
, and then go to the original film and cut it.
code show as below
$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;
}
Original blog https://www.shiguopeng.cn/archives/522
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。