PHP 调用ffmpeg

醉菜鸟 2022-06-17 08:00:02

window下载放在同目录下面的bin

http://ffmpeg.org/download.html#build-windows

linux安装yum install ffmpeg

PHP使用FFmpeg

<?php

class FFmpeg{

    private $_ffmpeg = __DIR__.'/bin/ffmpeg.exe';

    private $_ffprobe = __DIR__.'/bin/ffprobe.exe';

    private $_format = '%s -v quiet -print_format json -show_format -show_streams %s';

    private $_jpg = '%s -y -i %s -y -f image2 -ss %s %s -t 0.001 %s';

    private $_gif = '%s -y -ss %s -t %s -i %s %s -f gif %s';

    private $_download = '%s -i %s -c copy %s';

    public function __construct()
    {
        if(strtolower(PHP_OS) === 'linux') {
            $this->_ffmpeg = 'ffmpeg';
            $this->_ffprobe = 'ffprobe';
        }
    }

    /**
     * 视频信息
     * @param string $path
     * @return array
     */
    public function format($path = '')
    {
        $src_path = str_replace("//", "/", str_replace("\\", "/", $path));
        $arr = array( "video" => "", "audio" => "", "duration" => 0, "width" => 0, "height" => 0, "dis_ratio" => "", "size" => 0 );
        if( empty($src_path) )
        {
            return $arr;
        }

        $command = sprintf($this->_format,$this->_ffprobe,$path);
        $format = shell_exec($command);
        $json = json_decode($format);
        foreach( $json->streams as $row )
        {
            if( $row->codec_type == "video" )
            {
                $arr["video"] = $row->codec_name;
                $arr["duration"] = $row->duration;
            }

            if( $row->codec_type == "audio" )
            {
                $arr["audio"] = $row->codec_name;
            }

        }
        $arr["size"] = $json->format->size;
        return $arr;
    }

    /**
     * 生成jpg图片
     * @param $src_path
     * @param $obj_path
     * @param int $jpg_on
     * @param int $time
     * @param string $size
     * @return string
     */
    public function jpg($src_path, $obj_path, int $time = 0, string $size = '')
    {
        creat_dir(dirname($obj_path));
        $jpg_time = $time?:'3';
        $size = $size?:'320x210';
        $jpg_size = (!empty($size) ? "-s " . $size : "");
        $command = sprintf($this->_jpg,$this->_ffmpeg,$src_path,$jpg_time,$jpg_size,$obj_path);
        exec($command, $arr, $log);
        if( $log == 0 )
        {
            return "ok";
        }

        return 'no';
    }

    /**
     * 生成gif图片
     * @param $src_path
     * @param $obj_path
     * @param int $gif_on
     * @param int $time
     * @param int $len
     * @param string $size
     * @return string
     */
    public function gif($src_path, $obj_path,$time = 0,$len = 0,$size = '')
    {
        creat_dir(dirname($obj_path));
        $gif_time = $time?:'2';
        $size = $size?:'320x210';
        $gif_size = (!empty($size) ? "-s " . $size : "");
        $gif_len = $len?:'3';
        $command = sprintf($this->_gif,$this->_ffmpeg,$gif_time,$gif_len,$src_path,$gif_size,$obj_path);
        exec($command, $arr, $log);
        if( $log == 0 )
        {
            return "ok";
        }
        return $command;
    }

    /**
     * 转码成m3u8
     * @param $format
     * @param $path
     * @return string
     */
    public function transcode($format,$path){
        $obj_path = dirname($path);
        if( $format["audio"] == "aac" && $format["video"] == "h264" )
        {
            $make_command = $this->_ffmpeg." -y -i " . $path ." -hls_time " . 60 . " -hls_segment_filename " . $obj_path . "/%04d.ts -hls_list_size 0 index.m3u8";
        }
        else
        {
            $make_command = $this->_ffmpeg." -y -i " . $path ." -c:v libx264 -c:a aac -strict -2 -f hls -hls_list_size 0 -hls_time " . 60 . " -hls_segment_filename " . $obj_path . "/%04d.ts index.m3u8";
        }
        exec($make_command, $arr, $log);
        if( $log == 0 )
        {
            return "m3u8ok";
        }

        return "";
    }

    /**
     * 下载m3u8链接
     * @param string $http
     * @param string $path
     * @return string
     */
    public function download($http = '',$path = ''){
        $command = sprintf($this->_download,$this->_ffmpeg,$http,$path);
        exec($command, $arr, $log);
        if( $log == 0 )
        {
            return "downloadok";
        }
        return "";
    }
}
意见反馈