php使用ffmpeg命令给视频水印和截取视频第一帧的方法整理
<?php
// 定义ffmpeg路径及命令常量
define('FFMPEG_CMD', '/usr/bin/ffmpeg -i "%s" 2>&1');
/**
* 使用ffmpeg获取视频信息
* @param String $file 视频文件
* @return Array
*/
function getVideoInfo($file)
{
ob_start();
passthru(sprintf(FFMPEG_CMD, $file));
$video_info = ob_get_contents();
ob_end_clean();
// 使用输出缓冲,获取ffmpeg所有输出内容
$ret = array();
// Duration: 00:33:42.64, start: 0.000000, bitrate: 152 kb/s
if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $video_info, $matches)) {
$ret['duration'] = $matches[1]; // 视频长度
$duration = explode(':', $matches[1]);
$ret['seconds'] = $duration[0] * 3600 + $duration[1] * 60 + $duration[2]; // 转为秒数
$ret['start'] = $matches[2]; // 开始时间
$ret['bitrate'] = $matches[3]; // bitrate 码率 单位kb
}
// Stream #0:1: Video: rv20 (RV20 / 0x30325652), yuv420p, 352x288, 117 kb/s, 15 fps, 15 tbr, 1k tbn, 1k tbc
if (preg_match("/Video: (.*?), (.*?), (.*?)[,\s]/", $video_info, $matches)) {
$ret['vcodec'] = $matches[1]; // 编码格式
$ret['vformat'] = $matches[2]; // 视频格式
$ret['resolution'] = $matches[3]; // 分辨率
list($width, $height) = explode('x', $matches[3]);
$ret['width'] = $width;
$ret['height'] = $height;
}
// Stream #0:0: Audio: cook (cook / 0x6B6F6F63), 22050 Hz, stereo, fltp, 32 kb/s
if (preg_match("/Audio: (.*), (\d*) Hz/", $video_info, $matches)) {
$ret['acodec'] = $matches[1]; // 音频编码
$ret['asamplerate'] = $matches[2]; // 音频采样频率
}
if (isset($ret['seconds']) && isset($ret['start'])) {
$ret['play_time'] = $ret['seconds'] + $ret['start']; // 实际播放时间
}
//打印一下视频内容宽高时长等
echo "<pre>";
print_r($ret);
/* Array
(
[duration] => 00:00:06.26
[seconds] => 6.26
[start] => 0.000000
[bitrate] => 636
[vcodec] => h264 (High) (avc1 / 0x31637661)
[vformat] => yuv420p
[resolution] => 464x960
[width] => 464
[height] => 960
[acodec] => aac (LC) (mp4a / 0x6134706D)
[asamplerate] => 48000
[play_time] => 6.26
)*/
$file = file_get_contents($file);
$ret['size'] = strlen($file); // 视频文件大小
$video_info = iconv('gbk', 'utf8', $video_info);
return array($ret, $video_info);
}
$from = "http://file.qipa250.com/Video/20200527HH3831231.mp4";
// 输出视频信息
$video_result = getVideoInfo($from);
echo "<pre>";
print_r($video_result);
$video_info = $video_result[0];
$width = $video_info['width'];
$height = $video_info['height'];
$name = md5(date('YmdHis')) . ".png";
$to = "/data/web/qipa250/img/";
//判断视频的宽高,成比例截取视频图像
//$str = "ffmpeg -i " . $from . " -y -f mjpeg -ss 3 -t 1 -s 740x500 " . $to . $name;
$str = 'ffmpeg -i ' . $from . ' -y -f mjpeg -ss 3 -t 1 -s ' . $width . 'x' . $height . ' ' . $to . $name;
echo $str;
echo "\n";
system($str);
//增加水印
echo "开始生成带水印视频:" . date('Y-m-d H:i:s', time());
echo "\n";
$shuiyin_pic = '/data/web/qipa250/uploadedqrcode/qrcode/weixin/20200611/ctivity_230.jpg';
$name_mp4 = md5(date('YmdHis')) . ".mp4";
$from_mp4 = 'https://release.qipa250.com/Compeo/202000HH1333.mp4';
$str_shuiyin = 'ffmpeg -i ' . $from_mp4 . ' -vf "movie=' . $shuiyin_pic . '[watermark];[in][watermark] overlay=main_w-overlay_w-10:main_h-overlay_h-10[out] " -c:v libx264 -c:a copy -f mp4 ' . ' ' . $to . $name_mp4;
echo $str_shuiyin;
echo "\n";
system($str_shuiyin);
echo "结束生成带水印视频:" . date('Y-m-d H:i:s', time());
?>
参数说明
y 直接确认,无需输入y
t 60 截取前60秒,这是项目的一个特定需求
i 输入原视频
vf 这个其实我也不太清楚,似乎是加滤镜,文字水印也是以滤镜加上去的
drawtext 绘制文本,也就是文字水印,相关参数第一个似乎要写=,其它参数写:
fontfile 字体文件,windows好像会自动定位到C:\Windows\Fonts
text 文字水印内容
x x坐标,可以直接填数字,也可以使用宽度计算如一半宽度w/2
y y坐标,可以直接填数字,也可以使用高度计算如一半高度h/2
fontsize 水印字体大小,直接填数字,其它的没怎么研究
fontcolor 水印颜色,能填常见的white,yellow,rgb没试过,不填似乎没有,不过也可能是黑色
shadowy 不清楚
最后一个参数是新文件名