duanlei4759
2017-05-03 01:12 阅读 419
已采纳

连接多个视频php ffmpeg

I have multiple files that are already been encoded (they have the same format and size) I would like to concatenate on a single video (that already exist and has to be overwritten).

Following the official FAQ Documentation I should use demuxer

FFmpeg has a concat demuxer which you can use when you want to avoid a re-encode and your format doesn’t support file level concatenation.

The problem is that I should use a .txt file with a list of files using this command line

ffmpeg -f concat -safe 0 -i mylist.txt -c copy output

where mylist.txt should be:

file '/path/to/file1'
file '/path/to/file2'
file '/path/to/file3'

How can I do with PHP?


Also tried with concat protocol

I tried using also the concat protocol that re-encode videos with these lines of code:

$cli = FFMPEG.' -y -i \'concat:';

foreach ($data as $key => $media) {
  $tmpFilename = $media['id'];
  $tmpPath = $storePath.'/tmp/'.$tmpFilename.'.mp4';

  if ($key != ($dataLenght - 1)) {
    $cli .= $tmpPath.'|';
  } else {
    $cli .= $tmpPath.'\'';
  }
}

$cli .= ' -c copy '.$export;
exec($cli);

that generate this command line:

/usr/local/bin/ffmpeg -i 'concat:/USER/storage/app/public/video/sessions/590916f0d122b/tmp/1493768472144.mp4|/USER/storage/app/public/video/sessions/590916f0d122b/tmp/1493767926114.mp4|/USER/storage/app/public/video/sessions/590916f0d122b/tmp/1493771107551.mp4|/USER/storage/app/public/video/sessions/590916f0d122b/tmp/1493771114598.mp4' -c:v libx264 /USER/storage/app/public/video/sessions/590916f0d122b/tmp_video_session.mp4

but I got this error:

[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7fc8aa800000] Found duplicated MOOV Atom. Skipped it

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dpba63888 dpba63888 2017-05-03 01:33

    The only real trick here is making a temporary file with tempnam:

    //Generate a unique temporary file name, preferably in "/tmp"
    $temporaryFileName = tempnam('/tmp/');
    
    $cli = FFMPEG." -f concat -safe 0 -i $temporaryFileName -c copy $export";
    
    $temporaryFile = fopen($temporaryFileName, 'w');
    
    foreach ($data as $key => $media) {
      $tmpFilename = $media['id'];
      $tmpPath = "file $storePath/tmp/$tmpFilename.mp4
    ";
    
      //Write "$tmpPath" to our temporary file
      fwrite($temporaryFile, $tmpPath);
    }
    
    //Close our file resource
    fclose($temporaryFile);
    
    exec($cli);
    
    //Clean up the temporary text file
    unlink($temporaryFileName);
    

    Keep in mind I have not run this code, but the idea is here.

    点赞 评论 复制链接分享

相关推荐