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

视频裁剪:

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

本作品采用《CC 协议》,转载必须注明作者和本文链接
当神不再是我们的信仰,那么信仰自己吧,努力让自己变好,不辜负自己的信仰!
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
39
粉丝
156
喜欢
700
收藏
333
排名:31
访问:22.0 万
私信
所有博文
社区赞助商