FFmpeg应用实例

FFmpeg应用实例

1、IOHelper.php(#f4645f)

<?php
/**
 * IO公共操作类
 */
class IOHelper
{
    /**
     * 获取音频或视频信息
     * @param  [string] $path     [文件路径]
     * @return [array]  result    [文件信息]
     */
    public static function getMediaInfo($path)
    {
        $result = array();
        $cmd    = sprintf('/home/samba/ffmpeg/bin/ffprobe -v quiet -print_format json -show_format -show_streams "%s" 2>&1', $path);
        exec($cmd, $arr);
        $obj = json_decode(implode($arr));

        if ($obj && property_exists($obj, "format")) {
            $type           = $obj->format->format_name;
            $result["Type"] = $type;

            if (false === stripos($type, "_pipe")) {
                $result["Duration"]        = (int) $obj->format->duration; //持续时间
                $result["DisplayDuration"] = self::formatTime($obj->format->duration); //格式化后的时间
                $result["byte_size"]       = (int) $obj->format->size; //字节大小
                $result["format_size"]     = self::formatSize($obj->format->size); //格式化后的大小
            }

            if ("mp3" != $type) {
                foreach ($obj->streams as $stream) {
                    //视频流和音频流
                    if ($stream->codec_type == "video") {
                        //找到视频流
                        $result["Resolution"] = sprintf("%sx%s", @$stream->width, @$stream->height); //分辨率
                        break;
                    }
                }
            }
        }

        return $result;
    }

    /**
     * 返回格式化后的时间格式
     */
    private static function formatTime($sec)
    {
        $sec           = $sec % (24 * 3600);
        $hours         = floor($sec / 3600);
        $remainSeconds = $sec % 3600;
        $minutes       = floor($remainSeconds / 60);
        $seconds       = intval($sec - $hours * 3600 - $minutes * 60);

        return sprintf("%s:%s:%s", str_pad($hours, 2, "0", STR_PAD_LEFT), str_pad($minutes, 2, "0", STR_PAD_LEFT), str_pad($seconds, 2, "0", STR_PAD_LEFT));
    }

    /**
     * 返回格式化后的文件尺寸
     */
    public static function formatSize($bytes, $decimals = 2)
    {
        $size       = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
        $factor     = floor((strlen($bytes) - 1) / 3);
        $byte       = $bytes / pow(1024, $factor);
        $formatSize = floor($byte * 100) / 100;

        return sprintf("%.{$decimals}f", $formatSize) . @$size[$factor];
    }
    /**
     * *
     * @return boolean [是否是windows系统]
     */
    public static function isWin()
    {
        if (substr(PHP_OS, 0, 3) === 'WIN') {
            return true;
        }
        return false;
    }
    /**
     * 判断目录是否为空
     * @param  [string] $dir [目录路径]
     * @return [bool]        [true/false]
     */
    public static function isEmptyDir($dir)
    {
        if (!is_dir($dir)) {
            return "$dir is not a directory";
        }
        $handle = opendir($dir);
        while (($file = readdir($handle)) !== false) {
            if ($file != "." && $file != "..") {
                closedir($handle);
                return false;
            }
        }
        closedir($handle);

        return true;
    }

    /**
     * 删除一个目录
     * @param  [string] $dir [目录路径]
     * @return [type]      [description]
     */
    public static function removeDir($dir) 
    {
        if (! is_dir($dir)) 
        {
            return "$dir is not a directory";
        }
        $handle = opendir($dir);
        while (($file = readdir($handle)) !== false) 
        {
            if ($file != "." && $file != "..") 
            {
                is_dir("$dir/$file") ? removeDir("$dir/$file") : unlink("$dir/$file");
            }
        }
        if (readdir($handle) == false) 
        {
            closedir($handle);
            rmdir($dir);
        }
    }

    /**
     * 生成一个文件
     * @param  [type] $path [description]
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function writeFile ($path, $data)
    {
        //$dir = str_replace('/'.basename($path),'',$path);
        //@mkdir($dir,0777,true);
        $fp = fopen($path, 'w');
        if(fwrite($fp, $data)){
            return true;
        }
        fclose($fp);
        return false;
    }

    /**
     * 自定义获取文件md5
     * @param  [string] $file [文件路径]
     * @return [string]       [md5值]
     */
    public static function md5($file)
    {
        if(! $fp = fopen($file, 'r')){
            return false;
        }
        
        $size = filesize($file);
        // 1024*1024 = 1MB
        if (1048576 > $size) {
            return  md5_file($file);
        }

        $file_path = '';
        $file_path_temp = '';
        
        fseek($fp, 0); // 1 - 2012
        $file_path .= fread($fp, 2012);

        fseek($fp, $size / 2 - 1999); // size/2 - 1999
        $file_path .= fread($fp, 1999);

        fseek($fp, -2010, SEEK_END); // -2010
        $file_path .= fread($fp, 2010);
        
        fclose($fp);
        
        //自己定义你的临时文件路径
        $file_path_temp = sprintf("%s/%s_%s", public_path(), time(), basename($file));

        if(! $fp_temp = fopen($file_path_temp, "wb")){
            return false;
        }
        //写入截取的文件片段
        fwrite($fp_temp, $file_path);
        fclose($fp_temp);
        
        $md5 = md5_file($file_path_temp);
        unlink($file_path_temp);

        return $md5;
    }
}

 

posted @ 2017-09-22 14:36  丶老中医  阅读(443)  评论(0编辑  收藏  举报
一切已经开始©2018 丶老中医