<?php namespace Source\Lib;
use Dever;
class Video
{
    public function update($data, $time = false)
    {
        $getInfo = false;
        if (isset($data['vid'])) {
            $table = 'info_data_value';
            $info_data_value = Dever::db('info_data_value', 'source')->find($data['id']);

            if (!$info_data_value) {
                Dever::error('附件不存在');
            }
            if (!$info_data_value['value'] && !$info_data_value['vid']) {
                Dever::error('附件不存在');
            }
            if ($info_data_value['vid']) {
                $video = Dever::db('video', 'api')->find($info_data_value['vid']);
                if (!$video) {
                    Dever::error('视频不存在');
                }
            } else {
                $video = $info_data_value;
                $video['author'] = '';
                $video['file'] = $info_data_value['value'];
                $video['pdate'] = $info_data_value['cdate'];
                $video['name'] = $info_data_value['name'];
            }
            $info_data = Dever::db('info_data', 'source')->find($info_data_value['info_data_id']);
            $getInfo = true;
            
        } elseif (isset($data['info_data_id'])) {
            $info_data = Dever::db('info_data', 'source')->find($data['info_data_id']);
            $table = 'info_data_video';
            if ($info_data['type'] == 3) {
                if ($info_data['relation'] == 1) {
                    Dever::error('读取资源为内容，视频处理功能暂时不支持，请更换为附件');
                }
                if (!$info_data['relation_id']) {
                    Dever::error('请先选择附件');
                }
                $info_value = Dever::db('info_value', 'source')->find($info_data['relation_id']);
                if (!$info_value) {
                    Dever::error('附件不存在');
                }
                if (!$info_value['value'] && !$info_value['vid']) {
                    Dever::error('附件不存在');
                }
                if ($info_value['vid']) {
                    $video = Dever::db('video', 'api')->find($info_value['vid']);
                    if (!$video) {
                        Dever::error('视频不存在');
                    }
                } else {
                    $video = $info_value;
                    $video['author'] = '';
                    $video['file'] = $info_value['value'];
                    $video['pdate'] = $info_value['cdate'];
                    $video['name'] = $info_value['name'];
                }
                $getInfo = true;
            } elseif ($info_data['type'] == 10) {
                $video = Dever::db('info_data_collection', 'source')->find(array('info_data_id' => $data['info_data_id']));
                if (!$video) {
                    Dever::error('合集不存在');
                }
                if (!$video['file']) {
                    Dever::error('合集视频不存在');
                }
                $video['pdate'] = $video['cdate'];
                $video['name'] = $info_data['name'];
            }
        }

        if ($data['type'] == 1 && $data['template_id']) {
            $template = Dever::db('video_template', 'api')->find($data['template_id']);
        } elseif ($data['type'] == 2 && $data['template_id']) {
            $template = Dever::db('video_template', 'api')->find($data['template_id']);
        } else {
            $template = array();
        }
        if ($template) {
            $url = $video['file'];
            $local = Dever::load('view', 'upload')->local($url);
            if (!is_file($local)) {
                Dever::error('视频不存在');
            }
            $option = array();

            # 获取视频高度和宽度
            if ($getInfo) {
                $video_info = Dever::load('video', 'api')->info($local);
                $seconds = $video_info['seconds'];
                if ($time) {
                    if ($seconds >= $time*2) {
                        $data['time_length'] = $time;
                        $data['time_start'] = $time;
                    } elseif ($seconds >= $time) {
                        $data['time_length'] = $time;
                        $data['time_start'] = 0;
                    } else {
                        $data['time_length'] = $time-1;
                        $data['time_start'] = 0;
                    }
                }
                if ($video_info['width'] > $video_info['height']) {
                    $option['video']['scale'] = '1920:1080';
                    $split = 'split[a][b];[a]scale=1920:1080,boxblur=20:5[1];[b]scale=1920/1.008:-1[2];[1][2]overlay=(W-w)/2:(H-h)/2';
                } else {
                    $option['video']['scale'] = '1080:1920';
                    $split = 'split[a][b];[a]scale=1080:1920,boxblur=20:5[1];[b]scale=1080/1.008:-1[2];[1][2]overlay=(W-w)/2:(H-h)/2';
                }
            }

            if ($template['flip'] == 1) {
                $option['video']['flip'] = 1;
            }
            if ($template['setpts'] > 0) {
                $option['video']['setpts'] = $template['setpts'];
            }

            # 降噪
            //$option['video']['mp'] = 'denoise3d';
            $option['video']['hqdn3d'] = '';
            # 锐化
            $option['video']['unsharp'] = '';
            # 模糊
            //$option['video']['smartblur'] = '5:0.8:0';

            if ($data['text']) {
                $text_type = 'text';
                if ($data['text_type'] == 2) {
                    $text_type = 'textmove';
                }
                $admin = Dever::load('common', 'manage')->info();
                $video['admin'] = $admin['name'];
                if (!isset($video['author'])) {
                    $video['author'] = $video['admin'];
                }
                $video['data_name'] = $info_data['name'];
                $extend = Dever::load('common', 'manage')->extend();
                $zone = Dever::db('zone', 'source')->find($extend['data_id']);
                if ($zone) {
                    $video['zone_name'] = $zone['name'];
                } else {
                    $video['zone_name'] = '';
                }
                $source = Dever::db('info', 'source')->find($info_data['info_id']);
                $video['source_name'] = $source['name'];
                if ($video['author']) {
                    $option['video'][$text_type][] = \Dever\Helper\Str::val($data['text'], $video);
                    $option['video'][$text_type][] = $data['text_size'];
                    $option['video'][$text_type][] = $data['text_color'];
                    $option['video'][$text_type][] = $data['text_alpha'];
                    if ($data['text_type'] == 2) {
                        $option['video'][$text_type][] = $data['text_move'];
                    } else {
                        $option['video'][$text_type][] = $data['text_x'];
                        $option['video'][$text_type][] = $data['text_y'];
                    }
                }
            }

            # 对视频进行四周模糊处理
            //split[a][b];[a]scale=1080:1920,boxblur=20:5[1];[b]scale=1080/1.015:1920/1.012[2];[1][2]overlay=(W-w)/2:(H-h)/2
            if (isset($split)) {
                $option['video'][$split] = '';
            }
            # hue：调整视频色调、饱和度、亮度
            $option['video']['hue'] = 's=1:b=0.5:h=0.5';
            # 颜色平衡
            //$option['video']['colorbalance'] = 'rh=.3:gh=.3:bh=.3';
            
            if ($data['time_length'] > 0) {
                $option['time'] = array($data['time_length'], $data['time_start']);
            }
            /*
            $option['filter_complex'] = array
            (
                '[1:v]chromakey=0x00d810:0.1:0.2[ckout]',
                '[0:v][ckout]overlay[out]',
            );*/
            $output = array
            (
                'file' => $local . '_clip.mp4',
                'option' => array
                (
                    # 覆盖
                    'y' => '',
                    # 音频码率
                    //'b:a' => '10000k',
                    # 视频码率
                    //'b:v' => '10000k',
                    'r' => '25',
                    'c:v' => 'libx264',
                    'preset' => 'ultrafast',//ultrafast、superfast、veryfast、faster、fast、medium、slow、slower、veryslow、placebo。
                    'movflags' => 'faststart', 
                    'pix_fmt' => 'yuv420p',
                    'crf' => '18',
                    'threads' => '16',
                    'nr' => '500',
                    'an' => '',
                    //'sn' => '',
                    //'vn' => '',
                    'metadata' => 'md5=' . md5($video['id']),
                ),
            );
            Dever::load('video', 'api')->handle($local, $output, $option);

            Dever::db($table, 'source')->update(array('id' => $data['id']), array('file' => $url . '_clip.mp4'));
        }
        
        return $data;
    }
}