FFMPEG - 60秒从任何部分的视频


FFMPEG - 60 seconds from any part of video

我想做的是从上传的视频中创建一个60秒的FLV。但我不想总是看前60秒,如果可能的话,我想看视频的中间部分。但如果不是,我想随机获取一个60秒的视频文件,并创建flv.

我使用以下脚本制作FLV文件

$call="/usr/bin/ffmpeg -i ".$_SESSION['video_to_convert']." -vcodec flv -f flv -r 20 -b ".$quality." -ab 128000 -ar ".$audio."  ".$converted_vids.$name.".flv -y 2> log/".$name.".txt";
$convert = (popen($call." >/dev/null &", "r"));
pclose($convert);

所以我的问题是,我如何从视频随机获得60秒并转换它?

您可以使用以下命令(1)分割一段视频:

ffmpeg -sameq -ss [start_seconds] -t [duration_seconds] -i [input_file] [output_file]

您可以使用以下命令(2)获取视频时长:

ffmpeg -i <infile> 2>&1 | grep "Duration" | cut -d ' ' -f 4 | sed s/,//

使用你最喜欢的脚本语言,这样做(伪代码):

* variable start = (max_duration - 60) / 2
* execute system call command (1) with
    [start_seconds] = variable start   # (starts 30s before video center)
    [duration_seconds] = 60            # (ends 30s after video center)
    [input_file] = original filename of video
    [output_file] = where you want the 60-second clip to be saved

在php中是:

$max_duration = `ffmpeg -i $input_file 2>&1 | grep "Duration" | cut -d ' ' -f 4 | sed s/,//`;
$start = intval(($max_duration - 60) / 2);
`ffmpeg -sameq -ss $start -t 60 -i $input_file $output_file`;

这个简短的教程描述了一种使用FFMPEG剪切视频的方法。基本语法由以下开关组成:

  • -ss [start_seconds]以秒为单位设置起始点
  • -t duration告诉FFMPEG剪辑应该有多长。

所以你的调用看起来像这样:

$call="/usr/bin/ffmpeg -i ".$_SESSION['video_to_convert']." '
-vcodec flv '
-f flv '
-r 20 '
-b ".$quality." ' 
-ab 128000 '
-ar ".$audio." '
-ss 0 '
-t 60 '
".$converted_vids.$name.".flv -y 2> log/".$name.".txt"

获取视频的前60秒。

正如我在评论中所说的,认真研究一下沃兹沃思常数将是一个满足你需求的好主意。