ffmpeg安装使用

注意:安装ffmpeg时,如果需要对视频转码加log时需要用到x264依赖库,建议初始安装配置上

1.安装nasm

链接地址:nasm/2.13nasm/2.14
自己按照系统提示要求,一般nasm就够了,不过安装报错可选择更高版本
tar -xvf nasm-2.13.03.tar.gz
./configure
make
make install

2.安装x264

下载源码: git clone http://git.videolan.org/git/x264.git
cd x264
./configure --enable-shared (–enable-shared很重要,如果安装ffmpeg出现using libx264 without pkg-config,类似错误,估计是这个参数导致的)
make
make install

3. 安装ffmpeg

wget https://ffmpeg.org/releases/ffmpeg-4.2.2.tar.bz
./configure --prefix=/usr/local/ffmpeg --enable-gpl --enable-libx264
如果这一步出现了错误ERROR: libx264 not found
在./configure之前输入 export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH即可
make (这一步时间比较长,是正常现象)
make install

4.修改文件/etc/ld.so.conf

使用 vi ld.so.conf命令打开文件
在文件后面添加 /usr/local/ffmpeg/lib/ 这个路径和第3步的–prefix参数路径同步,在后面加一个lib即可
执行 ldconfig 命令使其生效

5. 配置环境变量

使用 vim /etc/profile命令打开profile文件,在文件末添加环境变量:
vi profile

export FFMPEG_HOME=/usr/local/ffmpeg
export PATH=$FFMPEG_HOME/bin:$PATH

执行 ffmpeg -version 测试 是否安装成功
如果出现以下提示,说明安装成功
在这里插入图片描述
下面附赠常用的视频3个操作的php代码片段
获取指定秒数的帧画面,获取视频的宽高,时长,比特率,编码格式,为视频添加图片logo水印

<?php
    date_default_timezone_set('PRC');
    define('VIDEODIR', "/www/lnmp/video/");
    $video = $argv[1];//第一个参数为待处理的原视频的绝对路径
    $out_video = str_replace('.', date('_md_h_i_s.'), $video);
    $in_file = VIDEODIR.$video;
    $out_file = VIDEODIR.$out_video;
    $type = $argv[2] ?? 1;
    switch ($type) {
        case 1:
            $rs = get_video_info($in_file);
            print_r($rs);
            break;
        case 2:
            $out_file = str_replace('.mp4', '.jpg', $out_file);
            $rs = get_video_frames_img($in_file,$out_file);
            break;
        case 3:
            $rs = add_video_watermark($in_file,$out_file);
            break;
        default:
            exit("第二个参数不正确\n");
            break;
    }
/**
 * @获取指定秒数的帧画面
 * @DateTime    2020-05-23
 * @return                 [return]
 * @param       [type]     $in_file   [待添加水印的视频绝对路径]
 * @param       [type]     $out_file  [添加水印后新视频视频保存路径]
 * @param       [type]     $in_file_info [待添加水印的视频宽高详情]
 */
function get_video_frames_img($in_file,$out_file,$in_file_info = [],$seconds = 1){
	$width = $in_file_info['width'] ?? 720;
	$height = $in_file_info['height'] ?? 1280;
 	//要执行的 cmd 命令
	$cmd =  "ffmpeg -i {$in_file} -y -f image2 -ss 00:00:01.167 -t 0.001 -s {$width}x{$height} {$out_file}";
    $cmd = iconv('UTF-8','GB2312',$cmd);
    //执行命令
    shell_exec($cmd.' 2>&1');
    // shell_exec($cmd);
}
/**
 * @获取视频的宽高,时长,比特率,编码格式
 * @DateTime    2020-05-23
 * @return                 [return]
 * @param       [type]     $in_file [description]
 */
function get_video_info($in_file){
    ob_start();
    passthru(sprintf('ffmpeg -i "%s" 2>&1', $in_file));
    $info = ob_get_contents();
    ob_end_clean();
    $ret = array();
    // Duration: 01:24:12.73, start: 0.000000, bitrate: 456 kb/s
    if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $info, $match)) {
        $ret['duration'] = $match[1]; // 提取出播放时间
        $da = explode(':', $match[1]); 
        $ret['seconds'] = $da[0] * 3600 + $da[1] * 60 + $da[2]; // 转换为秒
        $ret['start'] = $match[2]; // 开始时间
        $ret['bitrate'] = $match[3]; // bitrate 码率 单位 kb
    }

    // Stream #0.1: Video: rv40, yuv420p, 512x384, 355 kb/s, 12.05 fps, 12 tbr, 1k tbn, 12 tbc
    if (preg_match("/Video: (.*?), (.*?), (.*?), (.*?)[,\s]/", $info, $match)) {
        // print_r($match);
        $ret['vcodec'] = $match[1]; // 编码格式
        $ret['vformat'] = $match[2]; // 视频格式
            // $ret['resolution'] = $match[3]; // 分辨率
            // $a = explode('x', $match[3]);
            // $ret['width'] = $a[0];
            // $ret['height'] = $a[1];
        if(is_numeric(strpos($match[4], 'x'))){
            $ret['resolution'] = $match[4];
        }else{
            $ret['resolution'] = explode(' ', $match[3])[0];
        }
            $a = explode('x', $ret['resolution']);  
            $ret['width'] = $a[0];
            $ret['height'] = $a[1];     
    }
    //Stream #0.0: Audio: cook, 44100 Hz, stereo, s16, 96 kb/s
    if (preg_match("/Audio: (\w*), (\d*) Hz/", $info, $match)) {
        $ret['acodec'] = $match[1];       // 音频编码
        $ret['asamplerate'] = $match[2];  // 音频采样频率
    }

    if (isset($ret['seconds']) && isset($ret['start'])) {
        $ret['play_time'] = $ret['seconds'] + $ret['start']; // 实际播放时间
    }

    $ret['size'] = filesize($in_file); // 文件大小

    return $ret;
}

/**
 * @为视频添加图片logo水印
 * @DateTime    2020-05-23
 * @return                 [return]
 * @param       [type]     $in_file   [待添加水印的视频绝对路径]
 * @param       [type]     $out_file  [添加水印后新视频视频保存路径]
 * @param       [type]     $in_file_info [待添加水印的视频宽高详情]
 */
function add_video_watermark($in_file, $out_file,$in_file_info = []){
	$logo_file = VIDEODIR."log.png";//水印图片的宽高为64x64
	$width = $in_file_info['width'] ?? 720;
	$height = $in_file_info['height'] ?? 1280;
	$left = (int)($width-64)/2;
	$top = (int)($height-64)/2;
    $cmd = $cmd = "/usr/local/ffmpeg/bin/ffmpeg -i {$in_file} -i {$logo_file}  -filter_complex '[0:v]overlay={$left}:{$top}' -c:v libx264 -c:a copy -b:v 2500k {$out_file} -y";
    $cmd = iconv('UTF-8','GB2312',$cmd);
    // overlay={$left}:{$top}' left 水印图片左上角距离
    // -c:v libx264  设置输出格式为x264格式,可能会出现在浏览器上只有声音,没有画面的现象
    // -b:v 2500k 设置输出视频的比特率的大小
    // shell_exec($cmd.' 2>&1');
    shell_exec($cmd);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值