视频二次裁剪时间计算出原片的时间片段算法

PHP技术
400
0
0
2022-05-23
标签   PHP算法

视频裁剪:

  • 最近遇到了一个如下的需求, 对原片进行裁剪
  • 运营第一次裁剪视频, 会对视频进行删减(掐掉片头片尾之类), (此处忽略视频的总时长, 单位秒)
  • [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;
}

博客原文www.shiguopeng.cn/archives/522