我想要做的是从上传的视频创建一个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);
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,如何从视频中随机获得60秒并进行转换?
您可以使用此命令切片视频(1):
ffmpeg -sameq -ss [start_seconds] -t [duration_seconds] -i [input_file] [output_file]
Run Code Online (Sandbox Code Playgroud)
您可以使用此命令获取视频持续时间(2):
ffmpeg -i <infile> 2>&1 | grep "Duration" | cut -d ' ' -f 4 | sed s/,//
Run Code Online (Sandbox Code Playgroud)
所以只需使用您喜欢的脚本语言并执行此操作(伪代码):
* 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
Run Code Online (Sandbox Code Playgroud)
在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`;
Run Code Online (Sandbox Code Playgroud)
这个简短的教程描述了一种使用 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"
Run Code Online (Sandbox Code Playgroud)
获取视频的前 60 秒。
正如我在评论中所述,认真研究沃兹沃斯常数将是满足您需求的好主意。