linux ffmpeg截取图片、添加水印和获取视频信息

ffmpeg安装

windows:http://blog.51cto.com/helloway/1642247
linux:https://www.cnblogs.com/freeweb/p/6897907.html

1. ffmpeg截取图片 (b.jpg)

/usr/local/src/ffmpeg-4.1/ffmpeg -i a.mp4 -y -f image2 -ss 00:00:01 -t 0.001 -s 600x400 b.jpg

 2. ffmpeg添加水印(watermark.jpg)

ffmpeg -i test.mp4 -vf "movie=watermark.jpg [watermark]; [in][watermark] overlay=40:00 [out]" result.mp4

参数说明

test.mp4:需要加水印的视频
watermark.jpg:水印图片
40:00: 水印的x轴和y轴坐标
result.mp4:输出带水印的视频

3. 获取视频各项信息

/**
 * 获取视频属性
 * param $url string 视频地址
 */
public function addVideoFile($url)
{
    $command = '/usr/local/src/ffmpeg-4.1/ffmpeg -i '.$url.' 2>&1';
    ob_start();
    passthru($command);
    $info = ob_get_contents();
    ob_end_clean();
    $data = array();
    if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $info, $match)) {
        $data['duration'] = $match[1]; //播放时间
        $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]; //码率(kb)
    }
    if ((preg_match("/Video:(.*)\), (.*)\), (\d+x\d+)(\s|,)(.*)Audio/isU", $info, $match)) || (preg_match("/Video:(.*), (.*), (\d+x\d+)(\s|,)(.*)Metadata/isU", $info, $match))) {
        $data['vcodec'] = strtoupper(trim(explode('(', $match[1])[0])); //视频编码格式
        //$data['vformat'] = $match[2]; //视频格式
        $data['resolution'] = $match[3]; //视频分辨率
        $arr_resolution = explode('x', $match[3]);
        $data['width'] = $arr_resolution[0];
        $data['height'] = $arr_resolution[1];
    }
    if (preg_match("/Audio: (.*?), (\d*) Hz/", $info, $match)) {
        $data['acodec'] = strtoupper(trim(explode('(', $match[1])[0])); //音频编码
        $data['asamplerate'] = $match[2]; //音频采样频率
    }else{
        $data['acodec'] = null; //音频编码
        $data['asamplerate'] = null; //音频采样频率
    }
    if (isset($data['seconds']) && isset($data['start'])) {
        $data['play_time'] = $data['seconds'] + $data['start']; //实际播放时间
    }
    $data['size'] = round(abs(filesize($url))/1024);; //文件大小
    //获取文件的扩展名 
    $type = substr(strrchr($url,'.'), 1);
}

你可能感兴趣的:(linux ffmpeg截取图片、添加水印和获取视频信息)