PHP的ffmpeg使用

本文记录了在PHP中使用ffmpeg库进行多媒体处理的过程,包括遇到的问题和解决方案。特别指出音频分离和修改速率不能同时操作。ffmpeg是一个功能强大的开源库,此PHP类仍有优化空间,旨在启发更多可能性。
摘要由CSDN通过智能技术生成

因为项目需求,同事写了一个ffmpeg的类,但是因为临时来了一个其他的项目,所以这个未完成的类就交给我来完成,所以我就把这个类的完成过程记录一下

<?php
include('FFmpegSupport.php');
//http://blog.csdn.net/doublefi123/article/details/24325159 基本用法
class FFmpegManage
{
    
    /**
     * ffmpeg默认结构:ffmpeg  [全局选项] [输入文件选项] -i (输入文件路径) [输出文件选项] (输出文件路径)
     * 其中[]括号里的是可选,()括号里的是必选的
     */
    private $ExecString = 'ffmpeg.exe '; //命令语句
    private $GlobalOptions = ''; //全局选项
    private $ErrMessage = array(); //错误信息
    private $InputFileOptionsString = ''; //输入文件选项
    private $InputFileOptions = array(); //输入文件选项数组
    private $InputFilePath = array(); //输入文件路径
    private $OutFileOptions = ''; //输出文件选项
    private $OutFilePath = ''; //输出文件路径
    private $OutFileTybe = ''; //输入文件的后缀名
    //可配置参数
    private $config = array(
        'startTime'=>1,        //文件开始时间
        'fmt'=>1,               //文件格式
        'codec'=>1,             //解码器
        'audioCodec'=>1,       //音频解码器
        'bitRate'=>1,           //比特率
        'sampleRate'=>1,        //采样率
        'audioChannels'=>1,    //声道数
        'endTime'=>1,           //结束时间
        'timeLength'=>1,        //文件时间长度
    );
    /**
        $Config = array(
            'inputFilePath'            =>  array(),   //输入路径(不能为空)
            'outputFilePath'                => '',     //输出路径(不能为空)
            //输入文件选项(为空时不执行)
            'fileOption'        =>array(
                'studyTime'             => '',               //文件开始时间
                'fmt'                    => '',              //文件格式
                'codec'                  => '',             //解码器
                'audioCodec'            => '',              //音频解码器
                'bitRate'               => '',              //比特率
                'sampleRate'            => '',              //采样率
                'audioChannels'         => '',             //声道数
                'endTime'                => '',            //结束时间
                'timeLength'             => '',            //文件时间长度
            ),
        );
     **/

    public function setConfig($config){
    

        @$fileOption = $config['fileOption'];
        @$inputFilePath =  $config['inputFilePath'];
        @$outputFilePath =  $config['outputFilePath'];

        //判断是否有输入或输出路径
        if(empty($inputFilePath) && empty($outputFilePath)){
            $this->setErrMessage('inputFilePath and outputFilePath is null');
        }

        //获取输入文件路径
        if(!is_array($inputFilePath)){
            $filePathArray = explode('+',$inputFilePath);
        }else{
            $filePathArray = $inputFilePath;
        }

        if(!empty($fileOption))
            //遍历输入文件路径
            foreach($filePathArray as $filePath){
                //将配置信息进行遍历
                foreach($fileOption as $key => $value){
                    //判断配置信息是否正确,如果不正确跳过
                    if(!empty($this->config[$key]) && !empty($value)){
                        //加载配置
                        $this->$key($value);
                    }
                }
                //判断文件是否能进行写入
                if( file_exists( $filePath ) ){
                    //将输入配置与输入文件进行关联
                    $this->InputFilePath[] = '-i ' . $filePath . ' ';
                    $this->InputFileOptions[] = '-i ' . $filePath . ' '.$this->InputFileOptionsString;
                    $this->InputFileOptionsString = '';
                }else{
                    $this->setErrMessage('Input File is not Exist!!');
                }
            }

        $this->OutFileTybe = substr( $outputFilePath , -4 );
        $this->OutFilePath = $outputFilePath . ' ';
        return $this;
    }

    /**
     * 设置ffmpeg执行时对所有选项都回答yes,例文件已存在,要覆盖的时候程序会等待回答yes or on ,有时候不添加会出错
     * @return $this
     */
    public function setAnswerAllYes(){
    
        $this->GlobalOptions .= ' -y ';
        return $this;
    }

配置信息载入
    /**
     * 强制设定文件的格式,需要使用ffmpeg当前版本支持的名称(缺省使用扩展名称)
     * @param $_fileType String 文件类型
     */
    private function fmt( $_fileType ){
    
         empty( $_fileType ) ?
             $this->setErrMessage( 'File Type is empty!!'):
             $this->InputFileOptionsString .= '-f ' . $_fileType . ' ';
    }

    /**
     * 设置输入文件的起始时间点,在此时间点开始读取数据 ,
     * @param $_time int 起始时间(单位:秒s)
     */
    private function startTime( $_time ){
    
        $_time = (int)$_time;
        empty($_time) && ($_time < 0) ?
            $this->setErrMessage( " $_time must be biger than 0"):
            $this->InputFileOptionsString .= '-ss ' . $_time . ' ';

    }

    /**
     * 指定解码器,需输入此版本支持的解码器
     * @param $_codecName
     */
    private function codec( $_codecName ){
    
        empty( \FFmpegSupport::$InputProtocolsArray[ $_codecName ] )  && empty( \FFmpegSupport::$OutputProtocolsArray[ $_codecName ] ) ?
            $this->setErrMessage( 'This versions ffmpeg is not support this cod
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值