当前位置: 首页 > 后端技术 > PHP

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

时间:2023-03-29 21:33:13 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获取片段[12,13]inF1//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));functionmakeSections(array$firstCutSections,array$secondCutSections):array{//不管哪一个如果为空,就返回另一个if(empty($firstCutSections)){return$secondCutSections;}如果(空($secondCutSections)){返回$firstCutSections;}$newSections=[];foreach($secondCutSectionsas$currSection){$usableSections=$firstCutSections;$开始=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)){thrownewException('第二个截取的视频比第一个长');}$usableLength=$sec[1]-$sec[0];$start=$sec[0];继续;}//如果坐标不对齐,则对齐坐标if($remainStart>0){//两种情况if($remainStart>$usableLength){$remainStart-=$usableLength;$start+=$usableLength;$usableLength=0;}else{$usableLength-=$remainStart;$start+=$remainStart;$remainStart=0;}继续;}//应该使用哪个长度$contentLength=0;如果($remainLength>$usableLength){$contentLength=$usableLength;$remainLength-=$usableLength;$usableLength=0;}else{$contentLength=$remainLength;$usableLength-=$remainLength;$剩余长度=0;}var_dump($contentLength);//取出每一端的时间存储$newSections[]=[$start,$start+$contentLength,];}}return$newSections;}博客原创https://www.shiguopeng.cn/archives/522