<?php

namespace Aliyun\Src;

use Dever;

class Demo
{
    public function __construct()
    {
        $this->path = Dever::path(Dever::data(), 'video/');
        $this->sc_path = $this->path;
    }

    /**
     * 音频合成
     *
     * @return mixed
     */
    public function dudao($text = '')
    {
        $text = $text ? $text : '2019 感恩有你，值此佳节，读道祝您元旦快乐，读万卷书、行万里路，是谓读道。不落地、不创意，不辜负每块土地和每位客户的期望！2020读道愿与您携手同型、智造未来！';

        $text = '2019感恩有你，元旦佳节，尹家峪田园综合体祝您节日快乐，崮乡田园美、沂蒙花盛开，农旅融合、居游共享！欢迎来尹家峪体验游览、投资投智，2020让我们一起峪见奇迹！';
        $token = Dever::load('token/lib/aliyun')->token();

        $text = Dever::input('text', $text);

        $url = 'https://nls-gateway.cn-shanghai.aliyuncs.com/stream/v1/tts';

        $param['appkey'] = 'G0D0ySFZrtm9xjnY';
        $param['text'] = $text;
        $param['token'] = $token;
        $param['format'] = 'wav';
        $param['voice'] = 'Aimei';//Aimei
        $param['volume'] = 100;
        $param['speech_rate'] = -200;
        $param['pitch_rate'] = 0;

        $data = Dever::curl($url, $param, 'post', true);

        if (!$data) {
            return false;
        }

        $file = $this->sc_path . 'yinjiayu.wav';
        file_put_contents($file, $data);

        return $file;
    }

	/**
     * 音频合成
     *
     * @return mixed
     */
    public function audio($text = '', $name = '')
    {
        $file = $this->sc_path . 'cur.wav';
        if (file_exists($file)) {
            //return $file;
        }

        //$text = $text ? $text : '2019感恩有你，2020携手同行。值此佳节来临之际，'.$name.'祝您元旦快乐，更懂门窗更懂你，2020携手开启合作之窗！';

        $text = $text ? $text : '把最真的祝福化作风，吹到你的身边，把最诚的问候变成雨，飘到你的窗前，2019感恩有你，在新的一年里，'.$name.'愿与你携手同型，更懂门窗更懂你。';

       // '把最真的祝福化作风，吹到你的身边，把最诚的问候变成雨，飘到你的窗前，2019感恩有你，2020携手同行，更懂门窗更懂你。';

        $token = Dever::load('token/lib/aliyun')->token();

        $text = Dever::input('text', $text);

        $url = 'https://nls-gateway.cn-shanghai.aliyuncs.com/stream/v1/tts';

        $param['appkey'] = 'k6ZkLekn5NfRuhR4';
        $param['text'] = $text;
        $param['token'] = $token;
        $param['format'] = 'wav';
        $param['voice'] = 'Aixia';//Aimei
        $param['volume'] = 100;
        $param['speech_rate'] = -150;
        $param['pitch_rate'] = 0;

        $data = Dever::curl($url, $param, 'post', true);

        if (!$data) {
            return false;
        }

        
        file_put_contents($file, $data);

        return $file;
    }

    /**
     * 图片合成
     *
     * @return mixed
     */
    public function pic($mobile)
    {
        //$mobile = $mobile ? $mobile : '18682592018';

        if (!$mobile) {
            return false;
        }

        $file = $this->sc_path . 'cur.jpg';
        if (file_exists($file)) {
            return $file;
        }

        preg_match_all('/(1([3456789][0-9]))\d{8}/', $mobile, $match);
        if (isset($match[0][0])) {
            $mobile = $match[0][0];
        } else {
            return false;
        }
        
        $url = 'http://trade.5dev.cn/cmmc/seller/?l=code.poster&id=75&mobile=';

        $url .= trim($mobile);

        $data = Dever::curl($url);

        if (!strstr($data, '下载海报')) {
            //return false;
        }

        if (strstr($data, 'Whoops! There was an error.')) {
            return false;
        }

        $data = str_replace(array('<a href=', '>下载海报<a/>'), '', $data);

        
        file_put_contents($file, file_get_contents($data));

        return $file;
    }

    # 视频合成
    public function video($path, $name, $pic, $audio)
    {
        $mv = $this->path . 'cur.mp4';
        $file = Dever::path($this->path, '生成的视频/感恩/' . $path . '/' . $name . '.mp4');

        if (is_file($file)) {
            return;
        }

        if (!is_file($pic)) {
            return;
        }

        if (!is_file($audio)) {
            return;
        }

        //-b:a 32k
        $command = 'ffmpeg -y -loop 1 -i '.$pic.' -i '.$audio.' -c:v libx264 -tune stillimage -c:a aac -b:a 192k -filter_complex "[1]adelay=1000|1000[s2]" -map 0:v -map "[s2]" -pix_fmt yuvj420p -preset veryfast -r 10 -shortest ' . $file;

        $command .= ' && ffmpeg -y -i '.$file.' -i '.$mv.' -filter_complex "[0:a]amerge=inputs=1[aout];[0:v]pad=iw:ih[a];[1:v]scale=450:450[b];[a][b]overlay=300:600" -map "[aout]" -ac 2 -pix_fmt yuv420p -preset veryfast -r 10 -shortest ' . $file . '.mp4 && mv ' . $file . '.mp4 ' . $file;

        #system($command);

        //echo $command;die;

        Dever::run($command);

        return $command;
    }

    private function myTrim($str)
    {
        $search = array(" ","　","\n","\r","\t", "(", ")");
        $replace = array("","","","","","","");
        return str_replace($search, $replace, $str);
    }

    # 合并
    public function combine($path, $qname, $name, $mobile)
    {
        $qname = $this->myTrim($qname);
        $this->sc_path = Dever::path($this->path, '感恩素材/' . $path . '/' . $qname . '/');

        $file = Dever::path($this->path, '生成的视频/感恩/' . $path . '/' . $qname . '.mp4');

        if (is_file($file)) {
            return;
        }

        $audio = $this->audio(false, $name);
        $pic = $this->pic($mobile);
        if ($pic && $audio) {
            $this->video($path, $qname, $pic, $audio);
        }
        //echo 1;die;

        return 'ok';
    }

    public function load()
    {
        $key = Dever::input('key', 1);

        if($key == 10) {
            return;
        }

        $config = array
        (
            //1 => array('安徽', 0, 'mc3'),
            //2 => array('内蒙', 1, 'mc3'),
            //3 => array('浙江', 2, 'mc3'),
            //4 => array('福建', 3, 'mc3'),
            //5 => array('湖南', 4, 'mc3'),
            //6 => array('湖北', 5, 'mc3'),
            //7 => array('陕西', 6, 'mc3'),
            //8 => array('江苏', 7, 'mc3'),
            //9 => array('窗小妹', 0, 'mc2'),
            //10 => array('窗小花', 0, 'mc4'),
            //11 => array('北京', 0, 'mc5'),
            //12 => array('江西', 0, 'jx'),
            //13 => array('四川', 0, 'sc'),

            //21 => array('广西', 0, 'ys'),
            //22 => array('新疆', 1, 'ys'),
            //23 => array('黑龙江', 2, 'ys'),
            //24 => array('上海', 3, 'ys'),
            //25 => array('云南', 4, 'ys'),
            //26 => array('宁夏', 5, 'ys'),
           // 27 => array('西藏', 6, 'ys'),
            //28 => array('海南', 7, 'ys'),
            //29 => array('贵州', 8, 'ys'),
            //30 => array('青海', 9, 'ys'),

            //窗小妹
            1 => array('吉林', 0, 'mc1'),
            2 => array('河北', 1, 'mc1'),
            3 => array('河南', 2, 'mc1'),
            4 => array('甘肃', 3, 'mc1'),
            5 => array('辽宁', 4, 'mc1'),

            //李广彬
            6 => array('安徽', 0, 'mc2'),
            7 => array('湖南', 1, 'mc2'),
            8 => array('湖北', 2, 'mc2'),
            9 => array('陕西', 3, 'mc2'),
            10 => array('江苏', 4, 'mc2'),

            //郭阔
            12 => array('江西', 0, 'mc6'),

            //尹爽
            13 => array('广西', 0, 'mc4'),
            14 => array('上海', 1, 'mc4'),
            15 => array('青海', 2, 'mc4'),
            16 => array('海南', 0, 'mc5'),
            17 => array('贵州', 1, 'mc5'),
            18 => array('贵州', 0, 'mc7'),
        );
        if (isset($config[$key])) {
            $set = $config[$key];
        } else {
            return 1;
        }
        $path = $set[0];
        $sheet = $set[1];

        if ($set[2] == 'mc61') {
            $file = $this->path . $set[2].'.xlsx';
        } else {
            $file = $this->path . $set[2].'.xls';
        }

        $data = Dever::excelImport($file, $sheet);

        if ($data) {
            foreach ($data as $k => $v) {
                if ($v['A'] && $v['A'] != '群名' && isset($v['B']) && $v['B'] && isset($v['C']) && $v['C']) {
                    $this->combine($path, $v['A'], $v['B'], $v['C']);
                }
            }
        }

        return 'ok';
    }
}
 