<?php
namespace Video\Lib;

use Dever;

class Vod
{
	# 根据id 获取信息
    public function get($data, $uid = false)
    {
        if (!is_array($data)) {
            $data = Dever::db('video/vod')->getOne($data);
        }
        
        if (!$data) {
            Dever::alert('错误的信息');
        }
    
        $data['content'] = Dever::filter($data['content']);

        $data['video'] = convert($data['video'], 'mp4', 'video/vod', $data['id'], 'video');

        $data = $this->avinfo($data);
        
        return $data;
    }


    # 获取相关推荐
    public function getRelation($info)
    {
        $where['noid'] = $info['id'];
        $where['cate_id'] = $info['cate_id'];
        return Dever::db('video/vod')->getRelation($where);
    }

    # 增加浏览量
    public function addView($id)
    {
        Dever::db('video/vod')->addView($id);
    }

    # 解析元信息
    public function avinfo($data, $url = 'video', $table = 'video/vod')
    {
        # 解析视频元信息
        if (!$data['video_info']) {
            $video_info = Dever::curl($data[$url] . '?avinfo');
            Dever::db($table)->update(array('where_id' => $data['id'], 'video_info' => $video_info));
            $video_info = Dever::json_decode($video_info);
        } else {
            $video_info = Dever::json_decode($data['video_info']);
        }

        unset($data['video_info']);

        $data['video_width'] = 0;
        $data['video_height'] = 0;
        # 默认横屏
        $data['video_type'] = 1;
        if (isset($video_info['streams']) && $video_info['streams']) {
            foreach ($video_info['streams'] as $k => $v) {
                if (isset($v['width']) && isset($v['height'])) {
                    $data['video_width'] = $v['width'];
                    $data['video_height'] = $v['height'];
                    if ($data['video_width'] >= $data['video_height']) {
                        $data['video_type'] = 1;
                    } else {
                        $data['video_type'] = 2;
                    }
                    
                    break;
                }
            }
        }

        return $data;
    }
}
