ffmpeg 获取视频第一帧和视频时长

获取视频时长:

<?php

$videoUrl = DATA_DIR.'/fileuploads/20260112/9a3a8905d57aadcab1b0e687ed07113d.mp4';

// 方法1:使用完整命令路径
$command = "ffprobe -v quiet -show_entries format=duration -of default=nw=1 " . escapeshellarg($videoUrl) . " 2>&1";

// 方法2:增加调试信息
$command_debug = "ffprobe -i " . escapeshellarg($videoUrl) . " 2>&1";

// 执行并获取详细输出
$output = shell_exec($command);
$debug_output = shell_exec($command_debug);

echo "命令输出: " . var_export($output, true) . "\n";
echo "调试输出: " . $debug_output . "\n";

if ($output) {
    $output_arr = explode('=',$output);
    $duration = floatval($output_arr[1]) * 1000;
    echo "时长(毫秒): " . $duration . "\n";
} else {
    echo "未获取到时长信息\n";
}

获取视屏第一帧:

<?php

$video_path = DATA_DIR. $filepath . $filename;
$image_info = array(
    'filepath' => 'images/'.date('Ymd').'/',
    'filename' => md5(time()).'.jpeg',
    'filetype' => 'jpeg',
    'host' => DATA_URL,
);
// 创建目录
if (!is_dir(DATA_DIR.$image_info['filepath'])){
    hg_mkdir(DATA_DIR.$image_info['filepath']);
}
$cmd = "ffmpeg -i " . $video_path . " -ss 1 -frames:v 1  " . DATA_DIR.$image_info['filepath'].$image_info['filename'];
exec($cmd . ' 2>&1', $arr, $res);
$index_pic = $image_info['host'].$image_info['filepath'].$image_info['filename'];
posted @ 2026-08-26 09:47  猿份已尽  阅读(4)  评论(0)    收藏  举报