搜索包含开始和结束持续时间(以秒为单位)的数组(视频)以查找全长视频

时间:2015-11-12 10:29:32

标签: php arrays algorithm

我有一系列包含开始和结束秒数的视频片段。这些视频剪辑具有与背景相同的音频。我想抓住一组剪辑,这些剪辑应该具有适当或尽可能接近的秒数,以便它可以形成完整的视频。我的目标是找到短片并将它们与原始音频合并为背景。它的配音。所以我需要将视频中的间隙数保持最小。例如,我有以下数组:

Array
(
    [0] => Array
        (
            [start_sec] => 0
            [end_sec] => 28
            [clip_id] => 1
        )

    [1] => Array
        (
            [start_sec] => 0
            [end_sec] => 193
            [clip_id] => 4
        )

    [2] => Array
        (
            [start_sec] => 0
            [end_sec] => 18
            [clip_id] => 5
        )

    [3] => Array
        (
            [start_sec] => 0
            [end_sec] => 193
            [clip_id] => 6
        )

    [4] => Array
        (
            [start_sec] => 0
            [end_sec] => 31
            [clip_id] => 10
        )

    [5] => Array
        (
            [start_sec] => 0
            [end_sec] => 13
            [clip_id] => 11
        )

    [6] => Array
        (
            [start_sec] => 0
            [end_sec] => 9
            [clip_id] => 12
        )

    [7] => Array
        (
            [start_sec] => 10
            [end_sec] => 21
            [clip_id] => 0
        )

    [8] => Array
        (
            [start_sec] => 22
            [end_sec] => 171
            [clip_id] => 3
        )

    [9] => Array
        (
            [start_sec] => 29
            [end_sec] => 58
            [clip_id] => 2
        )

    [10] => Array
        (
            [start_sec] => 59
            [end_sec] => 60
            [clip_id] => 0
        )

    [11] => Array
        (
            [start_sec] => 61
            [end_sec] => 72
            [clip_id] => 7
        )

)

在上面的阵列中,我需要抓住近距离组合,你可以看到下面,0-9 10-21然后22 - 171之间没有间隙,所以它很好。

[6] => Array
        (
            [start_sec] => 0
            [end_sec] => 9
            [clip_id] => 12
        )

    [7] => Array
        (
            [start_sec] => 10
            [end_sec] => 21
            [clip_id] => 0
        )

    [8] => Array
        (
            [start_sec] => 22
            [end_sec] => 171
            [clip_id] => 3
        )

或者这个

[0] => Array
        (
            [start_sec] => 0
            [end_sec] => 28
            [clip_id] => 1
        )
 [9] => Array
        (
            [start_sec] => 29
            [end_sec] => 58
            [clip_id] => 2
        )
[10] => Array
        (
            [start_sec] => 59
            [end_sec] => 60
            [clip_id] => 0
        )

    [11] => Array
        (
            [start_sec] => 61
            [end_sec] => 72
            [clip_id] => 7
        )

我知道我们不能总是找到最好的,因为会有很多片段和许多组合,但我需要找到足够好的东西来构建主视频。任何指导或帮助都将受到高度赞赏,因为我已经花了很多时间在这上面:)

2 个答案:

答案 0 :(得分:1)

这是尝试从起点最大化剪辑总长度的东西,只允许剪辑的结束和开始时间之间的间隔为1秒,如您在示例中所示,可以根据需要进行调整(至例如,扩大最大间隙或返回更多组合。此示例假定列表按start_sec排序,并且仅测试从start_sec = 0开始的路径:

$data = Array
(
    0 => Array
        (
            'start_sec' => 0,
            'end_sec' => 28,
            'clip_id' => 1
        ),

    1 => Array
        (
            'start_sec' => 0,
            'end_sec' => 193,
            'clip_id' => 4
        ),

    2 => Array
        (
            'start_sec' => 0,
            'end_sec' => 18,
            'clip_id' => 5
        ),

    3 => Array
        (
            'start_sec' => 0,
            'end_sec' => 193,
            'clip_id' => 6
        ),

    4 => Array
        (
            'start_sec' => 0,
            'end_sec' => 31,
            'clip_id' => 10
        ),

    5 => Array
        (
            'start_sec' => 0,
            'end_sec' => 13,
            'clip_id' => 11
        ),

    6 => Array
        (
            'start_sec' => 0,
            'end_sec' => 9,
            'clip_id' => 12
        ),

    7 => Array
        (
            'start_sec' => 10,
            'end_sec' => 21,
            'clip_id' => 0
        ),

    8 => Array
        (
            'start_sec' => 22,
            'end_sec' => 171,
            'clip_id' => 3
        ),

    9 => Array
        (
            'start_sec' => 29,
            'end_sec' => 58,
            'clip_id' => 2
        ),

    10 => Array
        (
            'start_sec' => 29,
            'end_sec' => 53,
            'clip_id' => 8
        ),

    11 => Array
        (
            'start_sec' => 59,
            'end_sec' => 60,
            'clip_id' => 9
        ),

    12 => Array
        (
            'start_sec' => 61,
            'end_sec' => 72,
            'clip_id' => 7
        )
);
function gap($i,$j,&$arr){
  return $arr[$j]['start_sec'] - $arr[$i]['end_sec'];
}

function length($i,&$arr){
  return $arr[$i]['end_sec'] - $arr[$i]['start_sec'];
}

$n = count($data);

function f($i,$prev,$len,&$arr,$clips){
  global $n;

  if ($i == $n){
    return [$len,$clips];
  }
  if (gap($prev,$i,$arr) != 1){
    return f($i + 1,$prev,$len,$arr,$clips);
  } else {
    list($a,$ac) = f($i + 1,$i,$len + length($i,$arr),$arr,$clips);
    list($b,$bc) = f($i + 1,$prev,$len,$arr,$clips);
    if ($a > $b){
      array_splice($ac,1,0,$i);
      return [$a,$ac];
    } else {
      return [$b,$bc];
    }
  }
}

$i = 0;
while ($data[$i]['start_sec'] == 0){
  echo json_encode(f($i + 1,$i,length($i,$data),$data,[$i++])) . "\n";
}

输出:

[69,[0,9,11,12]]
[193,[1]]
[18,[2]]
[193,[3]]
[31,[4]]
[13,[5]]
[169,[6,7,8]]

答案 1 :(得分:0)

the script below will sort your video clips out in the order you like. It should do what you want. use PHP MultiSort.

 <?PHP
/**
**Sample Array DATA I BUILT TO TEST IT
**/

    $data = array
    (
      array(
            'start_sec' => 0,
            'end_sec' => 28,
            'clip_id' => 1,
            ),
      array(
        'start_sec' => 29,
        'end_sec' => 58,
        'clip_id' => 2,
        ),
        array(
        'start_sec' => 61,
        'end_sec' => 72,
        'clip_id' => 7,
        ),
      array(
        'start_sec' => 59,
        'end_sec' => 60,
        'clip_id' => 0,
        ),
        array(
        'start_sec' =>79,
        'end_sec' => 85,
        'clip_id' => 0,
        ),
        array(
        'start_sec' => 73,
        'end_sec' => 78,
        'clip_id' => 0,
        ),
    );

    // Gets the list
    foreach ($data as $key => $row) {
        $start_sec[$key]  = $row['start_sec'];
        $end_sec[$key] = $row['end_sec'];
    }

    // Sort the data
    array_multisort($start_sec, SORT_ASC, $end_sec, SORT_ASC, $data);


    var_dump($data);