<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/2/21
 * Time: 12:12
 */

namespace app\admin\model;

use FFMpeg\Coordinate\Dimension;
use FFMpeg\Coordinate\FrameRate;
use FFMpeg\Coordinate\TimeCode;
use FFMpeg\FFMpeg;
use FFMpeg\FFProbe;
use FFMpeg\Format\Audio\Flac;
use FFMpeg\Format\Video\WMV;
use FFMpeg\Format\Video\X264;

class Video
{
//    private static $ffmpeg = "D:/install/ffmpeg/bin/ffmpeg.exe";
    private static $ffmpeg = app_paths."ffmpeg/bin/ffmpeg.exe";
//    private static $ffprobe = "D:/install/ffmpeg/bin/ffprobe.exe";
    private static $ffprobe = app_paths."ffmpeg/bin/ffprobe.exe";

        private static $file_img=Pah."/storage/videoimg/";

//总长度时间和第2帧 图片
    public static function ffmpeg_getTime($file)
    {
        $ffmpeg = FFMpeg::create([
            'ffmpeg.binaries' => self::$ffmpeg ,
            'ffprobe.binaries' => self::$ffprobe,
        ]);
        $video = $ffmpeg ->open($file);
        $frame = $video->frame(TimeCode::fromSeconds(2));
        $time=time();
        $file_name=self::$file_img.$time.".png";
        $frame->save($file_name);
        $data["img"]="/storage/videoimg/".$time.".png";// 背景图片
            $dimension=self::video_time($file);
        $data["seconds"]=$dimension;
        return $data;
    }

//    获取总时长
   public static function video_time($file) {
       define('FFMPEG_PATH', self::$ffmpeg.' -i "%s" 2>&1');
        ob_start();
        passthru(sprintf(FFMPEG_PATH, $file));  //passthru()类似exec()
        $info = ob_get_contents();
        ob_end_clean();
        // 通过使用输出缓冲,获取到ffmpeg所有输出的内容。
        $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]; // 提取出播放时间
            $arr_dian = explode('.', $match[1]);
            $arr_duration = explode(':', $arr_dian[0]);
            $seconds = $arr_duration[0] * 3600 + $arr_duration[1] * 60 + $arr_duration[2]; //转换播放时间为秒数
//            $arr_duration = explode(':', $arr_dian[0]);
            return $seconds;
        }
        return 0;
    }

    // 获取所有参数
    public  static function getVideoInfo($file) {

        define('FFMPEG_PATH', self::$ffmpeg.' -i "%s" 2>&1');

        $command = sprintf(FFMPEG_PATH, $file);



        ob_start();

        passthru($command);

        $info = ob_get_contents();

        ob_end_clean();



        $data = array();

        if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $info, $match)) {

            $data['duration'] = $match[1]; //播放时间

            $arr_duration = explode(':', $match[1]);

            $data['seconds'] = $arr_duration[0] * 3600 + $arr_duration[1] * 60 + $arr_duration[2]; //转换播放时间为秒数

            $data['start'] = $match[2]; //开始时间

            $data['bitrate'] = $match[3]; //码率(kb)
        }

        if (preg_match("/Video: (.*?), (.*?), (.*?)[,\s]/", $info, $match)) {

            $data['vcodec'] = $match[1]; //视频编码格式

            $data['vformat'] = $match[2]; //视频格式

            $data['resolution'] = $match[3]; //视频分辨率

            $arr_resolution = explode('x', $match[3]);

            $data['width'] = $arr_resolution[0];

            $data['height'] = $arr_resolution[1];

        }

        if (preg_match("/Audio: (\w*), (\d*) Hz/", $info, $match)) {

            $data['acodec'] = $match[1]; //音频编码

            $data['asamplerate'] = $match[2]; //音频采样频率

        }

        if (isset($data['seconds']) && isset($data['start'])) {

            $data['play_time'] = $data['seconds'] + $data['start']; //实际播放时间

        }

//        $data['size'] = filesize($file); //文件大小

        return $data;

    }

    // 视频处理
    public function file_con($file)
    {
        $ffmpeg = FFMpeg::create([
            'ffmpeg.binaries' => self::$ffmpeg ,
            'ffprobe.binaries' => self::$ffprobe,
            "timeout"=>3600,
            "ffmpeg.threads"=>12,
        ]);
//        $ffmpeg = FFMpeg::create();
        $video=$ffmpeg->open(Pah.$file);

        $this->video_size($ffmpeg,$video);
    }

    // 设置视频大小
    public function video_size($ffmpeg,$video)
    {
        $saves=Pah."storage/zhuan/text26.mp4";
        $video->filters()->resize(new Dimension(750, 400),"inset");
        $video->save(new X264("aac"),$saves);
    }

    //   视频转码 H264
    public static function mp4_video($file)
    {
       $saves=Pah."storage/zhuan/texts11.mkv";

        $ffmpeg = FFMpeg::create([
            'ffmpeg.binaries' => self::$ffmpeg ,
            'ffprobe.binaries' => self::$ffprobe,
            "timeout"=>3600,
            "ffmpeg.threads"=>12,
        ]);

        $video=$ffmpeg->open(Pah.$file);//FrameRate
        $video->filters()->resize(new Dimension(750, 400),"inset")->framerate(new FrameRate(10),1)->synchronize();

//        $format=new X264();
//        $format
//        $video->save(new WMV(),$saves);
        $video->save(new X264("aac"),$saves);
        dump(1212);

    }

    

}