ffmpeg安装及使用

xiao
xiao

ffmpeg安装及使用

准备

    1. ffmpeg: https://www.ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2
    2. x264:git clone git://git.videolan.org/x264.git
    3. yasm: http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
    4. nasm: 
    5. freetype: http://www.linuxfromscratch.org/blfs/view/svn/general/freetype2.html
    6. fribidi: http://www.linuxfromscratch.org/blfs/view/svn/general/fribidi.html
    7. fontconfig: http://www.linuxfromscratch.org/blfs/view/svn/general/fontconfig.html
    8. harfbuzz: https://www.freedesktop.org/software/harfbuzz/release/harfbuzz-1.2.7.tar.bz2
    
    cd /opt/src  , cd /usr/local
    分别在 src、local 创建以上四个文件夹
    mkdir ffmpeg  x264  yasm  nasm

一、 Linux 下安装

  • yasm 安装

      cd /opt/src/yasm
      wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
      tar -jxvf  yasm-1.3.0.tar.gz
      cd yasm-1.3.0 
      ./configure  --prefix=/usr/local/yasm
      make
      make install
      
      #配置环境变量
      vim /etc/profile
      
      export YASM=/usr/local/yasm
      export PATH=${YASM}/bin:${PATH}
      
      source /etc/profile
      
      #查看版本 测试是否安装成功
      yasm --version 
    
  • nasm 安装
    #查看 nasm 是否安装
    #nasm --version
    
    #下载 最新版 nasm.bz2
    cd /opt/src/nasm 
    tar -jxvf 文件名
    cd nasm-2.13
    ./configure --prefix=/usr/local/nasm
    make 
    make install
    
    #配置环境变量
    export NASM=/usr/local/nasm
    export PATH=${NASM}/bin:${PATH}
    
    source /etc/profile
    
  • x264 安装
    # 下载 x264
    cd /opt/src/x264
    tar -jxvf 文件名
    cd 文件名
    ./configure  --prefix=/usr/local/x264  --enable-shared
    make
    make install
    
    # 配置环境变量

    export PKG_CONFIG_PATH=/usr/local/x264/lib/pkgconfig
    export PATH=${PKG_CONFIG_PATH}/bin:${PATH}
    
    source /etc/profile
  • harfbuzz 安装

     tar xvf *
     cd harfbuzz
     ./configure --prefix=/usr/local/harfbuzz
     make 
     make install
  • freetype 安装

     wget  url 
     tar xvf freetype
     cd freetype
     ./configure --prefix=/usr/local/freetype --disable-static
     make 
     make install
     
  • fribidi 安装

     wget url
     tar xvf fribidi 
     cd fribidi
     ./configure --prefix=/usr/local/fribidi
     make 
     make install 
     
  • fontconfig 安装

     wget url
     tar xvf fontconfig
     cd fontconfig
     ./configure --prefix=/usr/local/fontconfig
     make 
     make install
     
  • PKG_CONFIG_PATH 配置

     vim /etc/profile
     
     export PKG_CONFIG_PATH=/usr/local/x264/lib/pkgconfig:/usr/local/freetype/lib/pkgconfig:/usr/local/harfbuzz/lib/pkgconfig:/usr/local/fribidi/lib/pkgconfig:/usr/local/libass/lib/pkgconfig
     export PATH=${PKG_CONFIG_PATH}/bin:${PATH}
     
     #生效
     source /etc/profile
    
  • ld.so.conf 配置

     vim /etc/ld.so.conf
     
     #添加一下内容
     /usr/local/ffmpeg/lib
     /usr/local/x264/lib
     /usr/local/freetype/lib
     /usr/local/firbidi/lib
     /usr/local/harfbuzz/lib
     /usr/local/libass/lib
     
     #生效
     ldconfig
    
  • ffmpeg 安装

     tar -jxvf ffmpeg-4.1
     cd ffmpeg
     ./configure --prefix=/usr/local/ffmpeg --enable-libx264 --enable-gpl --enable-libass --enable-fontconfig
     make 
     make install
    
     #配置环境变量
     export FFMPEG=/usr/local/ffmpeg
     export PATH=${FFMPEG}/bin:${PATH}
     
     source /etc/profile
     
     ffmpeg --version
  • 配置

     安装好 x264
     vim /etc/ld.so.conf
     在该文件夹中添加
     /usr/local/ffmpeg/lib
     /usr/local/x264/lib
     
     #配置生效
     ldconfig

PHP 安装扩展包

  • Laravel 安装 php-ffmpeg

      composer require php-ffmpeg/php-ffmpeg
      
      composer 阿里云镜像源报错则在 composer.json中加入
      
      "php-ffmpeg/php-ffmpeg": "^0.18.0"
      

代码示例

  • 初始化对象

      $this->ffmpeg = FFMpeg::create(array(
              'ffmpeg.binaries'  => '/usr/local/ffmpeg/bin/ffmpeg',
              'ffprobe.binaries' => '/usr/local/ffmpeg/bin/ffprobe',
              'timeout'          => 3600, // The timeout for the underlying process
              'ffmpeg.threads'   => 12,   // The number of threads that FFMpeg should use
          ));
  • 截取封面图

       $video = $this->ffmpeg->open($url_1);
       $frame = $video->frame(TimeCode::fromSeconds(2));
       $frame->save(public_path().'/upload/image.jpg');
      
  • 视频裁剪

      $clip = $video->clip(TimeCode::fromSeconds(1),TimeCode::fromSeconds(15));
      $clip->save(new X264(), public_path().'/upload/video.mp4');
  • 视频拼接

      $new_file_name = date('YmdHis').'.mp4';
      $newFile = public_path() . '/upload/'.$new_file_name.'';
      $video->concat(array($url_1,$url_2,$url_3))->saveFromSameCodecs($newFile,false);
      
      ps:
      如果视频地址为url,使用上述方法则会报错:Protocol 'http' not on whitelist 'file,crypto'!
      
      解决视频地址为url:
      
      $ffmpeg_exec = '/usr/local/ffmpeg/bin/ffmpeg -f concat -safe 0 -protocol_whitelist "file,http,https,rtp,udp,tcp,tls"   -i '.$filelist.' -c copy '.$newFile.'';
    
      exec($ffmpeg_exec,$output,$result);
      
      ps:
      $filelist.txt 文件内容格式为:
          file    'http://www.xxxx.com/x.mp4'
          file    'http://www.xxxx.com/x1.mp4'
          ...    
          
      
      若遇到exec() 返回值为 127时,则需要查看文件执行权限  
              
  • 插入字幕

      $video = $this->ffmpeg->open($url_1);
      $new_file_name = date('YmdHis').'.mp4';
      $newFile = public_path() . '/upload/'.$new_file_name.'';
      $clip = $video->clip(TimeCode::fromSeconds(1),TimeCode::fromSeconds(15));
      $clip->save(new X264(), $newFile);
      $new_video_file = time().'_'.rand(100,999).'.mp4';
      $new_video_path = public_path().'/upload/' . $new_video_file;
      $video_srt_path = public_path().'/upload/test.srt';
      $ffmpeg_exec = '/usr/local/ffmpeg/bin/ffmpeg -i '.$newFile.' -vf subtitles='.$video_srt_path.' -y '.$new_video_path.' ';
      
      exec($ffmpeg_exec,$output,$code);
      if ($code == 0){
          echo 'success';
      }
       
      
Copyright

原创 本文由 xiao 发布于 xiao

原文链接:https://blog.joniding.com/index.php/archives/60/

许可协议CC BY-NC-ND 4.0署名-非商业性使用-禁止演绎 4.0 国际

发表评论