EditVideoMp4.php 3.0 KB
<?php
/**
 * @remark :
 * @name   :EditVideoMp4.php
 * @author :lyh
 * @method :post
 * @time   :2024/4/16 9:44
 */

namespace App\Console\Commands\Test;

use App\Models\Project\Project;
use App\Models\Template\BTemplate;
use App\Models\Template\BTemplateCommon;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class EditVideoMp4 extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'edit_video';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'edit_mp4';


    public function handle(){
        $projectModel = new Project();
        $list = $projectModel->list(['is_upgrade'=>0,'delete_status'=>0,'type'=>['!=',0],'id'=>['<=',82]]);
        $data = [];
        foreach ($list as $v){
            echo date('Y-m-d H:i:s') . 'project_id:'.$v['id'] . PHP_EOL;
            ProjectServer::useProject($v['id']);
            $this->copyTable();
            $this->getHtml();
            DB::disconnect('custom_mysql');
        }
        echo date('Y-m-d H:i:s') . 'end' . PHP_EOL;
    }

    /**
     * @remark :获取需要替换的html
     * @name   :getHtml
     * @author :lyh
     * @method :post
     * @time   :2024/4/16 10:17
     */
    public function getHtml(){
        $templateModel = new BTemplate();
        $templateList = $templateModel->list();
        if(!empty($templateList)){
            foreach ($templateList as $v1){
                $this->getVideoSrc($v1['id'],$v1['main_html'],'main_html',$templateModel);
            }
        }
        return true;
    }

    /**
     * @remark :获取video的src
     * @name   :getVideoSrc
     * @author :lyh
     * @method :post
     * @time   :2024/4/16 9:46
     */
    public function getVideoSrc($id,$html,$filed,$model){
        $pattern = '/<video.*?src="([^"]+)"[^>]*>/i';
        preg_match_all($pattern, $html, $matches);
        $srcLinks = $matches[1];
        if(!empty($srcLinks)){
            foreach ($srcLinks as $link) {
                $newLink = str_replace('ecdn6.globalso.com','v6-file.globalso.com',  $link);
                $html = str_replace($link, $newLink, $html);
            }
            $model->edit([$filed=>$html],['id'=>$id]);
        }
        return true;
    }


    /**
     * @remark :复制表
     * @name   :copyTable
     * @author :lyh
     * @method :post
     * @time   :2024/4/16 9:51
     */
    public function copyTable(){
        // 原始表名和新表名
        $originalTableName = "gl_web_template";
        $newTableName = "gl_web_template_c";
        // 检查原始表是否存在
        DB::connection('custom_mysql')->select("SHOW TABLES LIKE '{$originalTableName}'");
        DB::connection('custom_mysql')->statement("CREATE TABLE {$newTableName} LIKE {$originalTableName}");
        DB::connection('custom_mysql')->statement("INSERT INTO {$newTableName} SELECT * FROM {$originalTableName}");
    }
}