yum install SDL2-devel
yum install libXext-devel
./configure --disable-x86asm
make && make install
php使用
<?php
class Video
{
function doIt($pre, $after)
{
echo "<pre>";
$arr = $this->getDir($pre);
foreach ($arr as $v) {
$seconds = $this->getContent($v)['seconds'];
$secondsAfterFormat = $this->transitionAudioLen($seconds);
$shell = "ffmpeg -i {$v} -ss 00:00:00 -t $secondsAfterFormat -acodec copy {$after}/" . basename($v) . " -y";
shell_exec($shell);
}
}
function getDir($path)
{
if (!file_exists($path)) {
return [];
}
$files = scandir($path);
$fileItem = [];
foreach ($files as $v) {
$newPath = $path . DIRECTORY_SEPARATOR . $v;
if (is_dir($newPath) && $v != '.' && $v != '..') {
$fileItem = array_merge($fileItem, getDir($newPath));
} else if (is_file($newPath)) {
$fileItem[] = $newPath;
}
}
return $fileItem;
}
function getContent($file)
{
$command = sprintf('ffmpeg -i "%s" 2>&1', $file);
ob_start();
passthru($command);
$info = ob_get_contents();
ob_end_clean();
$data = array();
$match = [];
if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $info, $match)) {
$arr_duration = explode(':', $match[1]);
$data['seconds'] = $arr_duration[0] * 3600 + $arr_duration[1] * 60 + $arr_duration[2];
$data['start'] = $match[2];
$data['bitrate'] = $match[3];
}
$match = [];
if (preg_match("/Video: (.*?), (.*?), (\d+)x(\d+)(.*?), (\d+) kb\/s/", $info, $match)) {
$data['vcodec'] = $match[1];
$data['vformat'] = $match[2];
$data['width'] = $match[3];
$data['height'] = $match[4];
$data['videorate'] = $match[6];
}
$match = [];
if (preg_match("/Audio: (.*?), (\d+) Hz, stereo, fltp, (\d+) kb\/s/", $info, $match)) {
$data['acodec'] = $match[1];
$data['asamplerate'] = $match[2];
$data['audiorate'] = $match[3];
}
if (isset($data['seconds']) && isset($data['start'])) {
$data['play_time'] = $data['seconds'] + $data['start'];
}
$data['size'] = filesize($file) / 1024 / 1024;
return ($data);
}
function transitionAudioLen($seconds)
{
$seconds -= 10;
$hour = floor($seconds / 3600);
$hourFormat = strlen($hour) > 1 ? $hour : (str_repeat('0', 2 - strlen($hour)) . $hour);
$minutes = floor(($seconds - ($hour * 3600)) / 60);
$minuteFormat = strlen($minutes) > 1 ? $minutes : (str_repeat('0', 2 - strlen($minutes)) . $minutes);
$seconds = $seconds - ($hour * 3600) - ($minutes * 60);
$secondFormat = strlen(intval($seconds)) > 1 ? $seconds : (str_repeat('0', 2 - strlen(intval($seconds))) . $seconds);
return "{$hourFormat}:{$minuteFormat}:{$secondFormat}";
}
}
$class = new Video();
$pre = '/home/Go_WorkSpace/old';
$after = '/home/Go_WorkSpace/new';
$class->doIt($pre, $after);