TestCommand.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <?php
  2. namespace App\Console\Test;
  3. use App\Cache\UserCache;
  4. use App\Facade\Site;
  5. use App\Libs\TikTok\Kernel\Support\Str;
  6. use App\Models\MpGeneratePicTask;
  7. use App\Services\DeepSeek\DeepSeekService;
  8. use getID3;
  9. use GuzzleHttp\Client;
  10. use Illuminate\Console\Command;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Redis;
  13. use Illuminate\Support\Facades\Storage;
  14. use Vinkla\Hashids\Facades\Hashids;
  15. use function PHPSTORM_META\map;
  16. /**
  17. * 测试
  18. */
  19. class TestCommand extends Command
  20. {
  21. /**
  22. * @var string
  23. */
  24. protected $signature = 'test {--token=} {--uid=} {--eid=}';
  25. /**
  26. * The console command description.
  27. * php artisan Payment:BasePayment --bid='1'
  28. *
  29. * @var string
  30. */
  31. protected $description = '测试';
  32. protected $deepseekService;
  33. public function __construct(DeepSeekService $deepseekService
  34. )
  35. {
  36. parent::__construct();
  37. $this->deepseekService = $deepseekService;
  38. }
  39. /**
  40. * handle
  41. */
  42. public function handle()
  43. {
  44. $this->handleHistoryTasks();
  45. $this->handleHistorySegments();
  46. dd('end!');
  47. // dd(testStoryboardParsing(''));
  48. $content = "";
  49. dd(handleScriptContent($content));
  50. exit();
  51. $url = 'https://zw-audiobook.tos-cn-beijing.volces.com/video/ai_generation_1772704798_69a9541e94bef';
  52. $video_name = 'ai_generation_' . time() . '_' . uniqid();
  53. $video_ext = getVideoExtFromUrl($url);
  54. $video_name = $video_name . $video_ext;
  55. dump($video_name);
  56. $video_url = uploadStreamByTos('video', $url, $video_name);
  57. dd($video_url);
  58. // $url = 'https://zw-audiobook.tos-cn-beijing.volces.com/image/ai_generation_1772525682_69a69872e3a87..png';
  59. // // $url = 'https://zw-audiobook.tos-cn-beijing.volces.com/image/ai_generation_1772525682_69a69872e3a87.png';
  60. // $compressed_url = compressRemoteImageUrlToSize($url);
  61. // // $compressed_url = compressRemoteImageUrlToSizeImagick($url);
  62. // $filename = storage_path('app/public/video_frames/compressed.png');
  63. // // $url = uploadStreamByTos('image', $compressed_url, $filename);
  64. // $url = file_put_contents($filename, $compressed_url);
  65. // dd($url);
  66. // // 转存阿里云oss文件到tos
  67. // $audio_effects = DB::table('mp_timbres')->where('id', 246)->get();
  68. // $total = count($audio_effects);
  69. // $success = 0;
  70. // $continue = 0;
  71. // $fail = 0;
  72. // foreach ($audio_effects as $item) {
  73. // $id = getProp($item, 'id');
  74. // $url = getProp($item, 'audio_url');
  75. // if (!$url) {
  76. // $continue++;
  77. // continue;
  78. // }
  79. // if (!strstr($url, 'https://cdn-zwai.ycsd.cn')) {
  80. // $continue++;
  81. // continue;
  82. // }
  83. // $index = strrpos($url, '/');
  84. // $filename = mb_substr($url, $index + 1);
  85. // $stream = file_get_contents($url);
  86. // $new_url = uploadStreamByTos('demonstrate', $stream, $filename);
  87. // dd($new_url);
  88. // if ($new_url) {
  89. // $boolen = DB::table('mp_timbres')->where('id', $id)->update([
  90. // 'audio_url' => $new_url,
  91. // 'updated_at' => date('Y-m-d H:i:s'),
  92. // ]);
  93. // if ($boolen) $success++;
  94. // else $fail++;
  95. // }else {
  96. // dump('未生成新url');
  97. // $fail++;
  98. // }
  99. // }
  100. // dd("总条数:{$total}, 成功条数:{$success}, 跳过条数:{$continue}, 失败条数:{$fail}");
  101. exit();
  102. // $audio_effects = DB::table('mp_bgms')->where('is_enabled', 1)->where('playtime_seconds', 0)->get();
  103. // foreach ($audio_effects as $audio_effect) {
  104. // $id = getProp($audio_effect, 'id');
  105. // $url = getProp($audio_effect, 'bgm_url');
  106. // $index = strrpos($url, '/');
  107. // $name = 'The_Dawn';
  108. // // 将远程地址文件保存到本地
  109. // $filename = storage_path().'/app/public/audio_effect/' . $name;
  110. // if (!file_exists($filename)) {
  111. // $file = file_get_contents($url);
  112. // file_put_contents($filename, $file);
  113. // }
  114. // $getID3 = new getID3();
  115. // $ThisFileInfo = $getID3->analyze($filename);
  116. // $seconds = floor($ThisFileInfo['playtime_seconds']) ?? 0;
  117. // if ($seconds <= 0) $seconds = 1;
  118. // DB::table('mp_bgms')->where('id', $id)->update([
  119. // 'playtime_seconds' => $seconds,
  120. // 'updated_at' => date('Y-m-d H:i:s'),
  121. // ]);
  122. // unlink($filename);
  123. // }
  124. // exit();
  125. // $wavPath = public_path('audio');
  126. // if (!file_exists($wavPath)) {
  127. // return [];
  128. // }
  129. // $files = scandir($wavPath);
  130. // $error_voice = [];
  131. // foreach ($files as $file) {
  132. // if ($file !== '.' && $file !== '..') {
  133. // $voice_type = pathinfo($file, PATHINFO_FILENAME);
  134. // $boolen = DB::table('mp_timbres')->where('timbre_type', $voice_type)->update([
  135. // 'audio_url' => 'https://cdn-zwai.ycsd.cn/mp_audio/voice_demo/' . $file,
  136. // 'is_enabled' => 1,
  137. // 'updated_at' => date('Y-m-d H:i:s'),
  138. // ]);
  139. // if (!$boolen) $error_voice[] = $file;
  140. // }
  141. // }
  142. // \Log::info('错误文件:' . json_encode($error_voice, 256));
  143. // exit();
  144. // exit();
  145. $bid = 321;
  146. $version_id = 1663;
  147. $cid = 708197;
  148. $redis_key = "select-{$bid}-{$version_id}-{$cid}";
  149. // 查询更新后的信息
  150. $paragraph_urls = DB::table('mp_chapter_paragraph_audios')->where('bid', $bid)->where('version_id', $version_id)
  151. ->where('cid', $cid)->where(function($query) {
  152. return $query->where('generate_status', '!=', '制作中')->orWhere('error_msg', '!=', '');
  153. })->select('id', 'sequence', 'paragraph_audio_url', 'error_msg')
  154. ->get()->map(function ($value) {
  155. return (array)$value;
  156. })->toArray();
  157. $rem_ids = [];
  158. foreach ($paragraph_urls as $item) {
  159. $rem_ids[] = getProp($item, 'id');
  160. }
  161. // 删除已发送的id
  162. Redis::srem($redis_key, $rem_ids);
  163. dd(Redis::smembers($redis_key));
  164. exit();
  165. // $all_timbre = DB::table('mp_timbres')->get();
  166. // foreach($all_timbre as $timbre) {
  167. // $id = getProp($timbre, 'id');
  168. // if ($id > 1) {
  169. // $language = getProp($timbre, 'language');
  170. // $language = str_replace(['、', ','], ',', $language);
  171. // DB::table('mp_timbres')->where('id', $id)->update([
  172. // 'language' => $language,
  173. // 'first_category_id' => 0,
  174. // 'first_category_name' => '',
  175. // 'second_category_id' => 0,
  176. // 'second_category_name' => '',
  177. // 'third_category_id' => 0,
  178. // 'third_category_name' => '',
  179. // ]);
  180. // }
  181. // }
  182. // dd('end');
  183. ini_set('max_execution_time', 0);
  184. \Log::info('~~~~~~~~~~~~~~~~~~~~~~~~开始~~~~~~~~~~~~~~~~~~~~~~~~');
  185. // $a = DB::table('mp_emotion_list')->pluck('emotion_name')->toArray();
  186. // dd(implode('、', $a));
  187. // $emotion_str = '​开心(happy),悲伤(sad),生气(angry),惊讶(surprised),恐惧(fear),厌恶(hate),激动(excited),冷漠(coldness),中性(neutral),沮丧(depressed),撒娇(lovey-dovey),害羞(shy),安慰鼓励(comfort),咆哮/焦急(tension),温柔(tender),讲故事 / 自然讲述(storytelling),情感电台(radio),磁性(magnetic),广告营销(advertising),气泡音(vocal - fry),低语 (ASMR),新闻播报(news),娱乐八卦(entertainment),方言(dialect)';
  188. // $emotion_arr = explode(',', $emotion_str);
  189. // $insert_data = [];
  190. // foreach ($emotion_arr as $emotion) {
  191. // preg_match('/(.*)?((.*?))/', $emotion, $match);
  192. // if (count($match) == 3) {
  193. // $insert_data[] = [
  194. // 'emotion_code' => $match[2],
  195. // 'emotion_name' => $match[1],
  196. // 'is_enabled' => 1,
  197. // 'created_at' => date('Y-m-d H:i:s'),
  198. // 'updated_at' => date('Y-m-d H:i:s'),
  199. // ];
  200. // }
  201. // }
  202. // DB::table('mp_emotion_list')->insert($insert_data);
  203. // exit();
  204. $bid = 10917;
  205. $version_id = 1649;
  206. $cid = 2669608;
  207. // $text = '"旁白:刀尖刺穿手掌的瞬间,疼痛自掌心钻入骨髓。{自然讲述} \n旁白:心脏像是被万箭穿过一般,我痛得几近晕厥。{自然讲述} \n旁白:可赵晏池显然不愿让我那么痛快地昏过去。{自然讲述} \n旁白:他命人用一盆盆冰凉刺骨的冷水强行让我保持清醒。{自然讲述} \n旁白:血迹混杂着水迹在身下漫开,任谁看了都触目惊心。{自然讲述} \n旁白:意识涣散间,我仿佛在脑海中看到走马灯。{自然讲述} \n旁白:结婚七年,圈子里无人不知我是赵晏池宠在心尖上的人。{自然讲述} \n旁白:他会在拍卖会上一掷千金买下我随口说喜欢的东西。{自然讲述} \n旁白:他会在我生病的时候亲自衣不解带地照顾我。{自然讲述} \n旁白:从不落泪的男人,看到我身体难受吃不下饭时却心疼得湿了眼眶。{自然讲述} \n旁白:他会在求婚那天,包下国内外所有大荧幕,高调地向全世界宣布爱我。{自然讲述} \n旁白:那一夜,满城的天都被他准备的烟花照亮。{自然讲述} \n旁白:绚烂的烟火下,他深情款款地看着我的眼睛。{自然讲述} \n赵晏池(男):我就是要让所有人都知道,你是我赵晏池的夫人,是我的心头肉。{温柔} \n赵晏池(男):有我保护你,谁都别想动你一根手指头。{温柔} \n旁白:可偏偏如今,伤我最深的,就是给过我这些承诺的人。{自然讲述} \n旁白:压制着我的保镖终于将我松开。{自然讲述} \n旁白:我想摸摸肚子里的孩子,却根本抬不起手。{自然讲述} \n旁白:我的手已经被彻底废掉,绵软得使不上一点力气。{自然讲述} \n赵晏池(男):把她给我扔出去,别在这边脏了我们的眼睛。{冷漠} \n旁白:保镖正要照做,却又被旁边的女人制止。{自然讲述} \n汪思月(女):等等。{温柔} \n旁白:她声音轻柔,却像是恶鬼的轻语:{自然讲述} \n汪思月(女):晏池,我看他们也挺辛苦的。{温柔} \n汪思月(女):这个女的身材看着倒是不错,不如就赏给他们玩玩吧。{温柔} \n旁白:下一秒,赵晏池宠溺的声音响起。{自然讲述} \n赵晏池(男):思月,还是你小主意最多。{温柔} \n旁白:听到这个有些耳熟的名字,我努力抬起肿起的眼皮。{自然讲述} \n旁白:竟然是她。{自然讲述} \n旁白:汪思月,赵晏池的初恋。{自然讲述} \n旁白:刚在一起的时候我就听过这个名字。{自然讲述} \n旁白:可那时他信誓旦旦地和我保证,他们已经是过去式了。{自然讲述} \n旁白:他的心里只有我一个人。{自然讲述} \n旁白:不等我想太多,赵晏池下一句话像一道惊雷劈下来。{自然讲述} \n赵晏池(男):好,就听你的。{冷漠} \n旁白:他对那几个保镖淡然道:{自然讲述} \n赵晏池(男):她归你们了,别玩死,死在这儿太晦气。{冷漠} \n旁白:保镖的脸上露出欣喜而猥琐的笑容。{自然讲述} \n旁白:我拼命摇头,张开嘴想告诉他们我的真实身份。{自然讲述} \n旁白:可药物作用让我发不出一个完整的音节。{自然讲述} \n旁白:拼尽全力,我也只能呼出嗬哧嗬哧的气声。{自然讲述} \n旁白:他们摩拳擦掌着靠近我。{自然讲述} \n旁白:拖着死鱼一样的身体,我吃力地在地上匍匐,可这全然是徒劳。{自然讲述} \n旁白:很快,我便被他们按住,压在身下。{自然讲述} \n旁白:撕裂一般的疼痛占据了我的身体,生理性的恶心冲撞着我的五脏六腑。{自然讲述} \n旁白:不多时,小腹处传来一阵坠痛。{自然讲述} \n旁白:我眼睁睁看着自己身下晕开一片血迹。{自然讲述} \n旁白:我知道,我的孩子终究是没保住。{自然讲述} \n旁白:保镖愣了一瞬间后也反应过来。{自然讲述} \n保镖(男):这娘们儿还怀孕了?{惊讶} \n保镖(男):怀孕?{惊讶} \n旁白:赵晏池听到这两个字蹙起眉。{自然讲述} \n旁白:他似乎想到了什么,拿起手机发了条消息。{自然讲述} \n旁白:我放在袋子里的手机响起轻微的提示音,可惜没人听见。{自然讲述} \n旁白:他没有再多想:{自然讲述} \n赵晏池(男):你们继续吧。{冷漠} \n旁白:我无神地盯着天花板,泪水糊满了整张脸。{自然讲述} \n旁白:绝望将我包围,整个人都陷入深不见底的深渊。{自然讲述}"';
  208. // $text = '"旁白:刀尖刺穿手掌的瞬间,疼痛自掌心钻入骨髓。 \n旁白:心脏像是被万箭穿过一般,我痛得几近晕厥。 \n"';
  209. // $text = json_decode($text, true);
  210. // dd(handleScriptWords($text, 0));
  211. $text = $this->deepseekService->generateScriptWords($cid, 'r1');
  212. \Log::info('text: '.json_encode($text, 256));
  213. // $token = getTextTokens($text);
  214. $result = handleScriptWords($text);
  215. $role_gender = getProp($result, 'role_gender');
  216. $role_timbre = [];
  217. foreach ($role_gender as $role => $gender) {
  218. $timbre = DB::table('mp_timbres')->where('gender', $gender)->first();
  219. if (!$timbre) {
  220. $role_timbre[$role] = ['timbre_type' => 'zh_male_ruyayichen_emo_v2_mars_bigtts', 'timbre_name' => '儒雅男友'];
  221. }else {
  222. $timbre_name = str_replace('(多情感)', '', $timbre->timbre_name);
  223. $role_timbre[$role] = [
  224. 'timbre_type' => $timbre->timbre_type,
  225. 'timbre_name' => $timbre_name
  226. ];
  227. }
  228. }
  229. // 更新角色-音色信息
  230. $existed_role_info = DB::table('mp_book_version')->where('bid', $bid)->where('id', $version_id)->value('role_info');
  231. $existed_role_info = json_decode($existed_role_info, true);
  232. if ($existed_role_info) $existed_roles = array_keys($existed_role_info);
  233. else $existed_roles = [];
  234. foreach ($role_timbre as $role => $timbre) {
  235. if (!in_array($role, $existed_roles)) {
  236. $existed_role_info[$role] = $timbre;
  237. }
  238. }
  239. // 获取情感信息
  240. $emotion_list = DB::table('mp_emotion_list')->where('is_enabled', 1)->pluck('emotion_name', 'emotion_code')->toArray();
  241. $emotion_list = array_flip($emotion_list);
  242. // 构造生成音频的json
  243. $words = getProp($result, 'words');
  244. foreach($words as &$word) {
  245. $role = getProp($word, 'role');
  246. $word['gender'] = (int)$word['gender'];
  247. if (isset($emotion_list[getProp($word, 'emotion')])) { // 如果有对应情感则赋值,没有则默认为中性(neutral)
  248. $word['emotion_type'] = $emotion_list[getProp($word, 'emotion')];
  249. }else {
  250. $word['emotion'] = '中性';
  251. $word['emotion_type'] = 'neutral';
  252. }
  253. $word['voice_name'] = $role_timbre[$role]['timbre_name'];
  254. $word['voice_type'] = $role_timbre[$role]['timbre_type'];
  255. $word['speed_ratio'] = mt_rand(9,11)/10;
  256. $word['loudness_ratio'] = mt_rand(5,12)/10;
  257. $word['emotion_scale'] = mt_rand(1,5);
  258. }
  259. $generate_json = json_encode($words, 256);
  260. \Log::info('generate_json: '.$generate_json);
  261. try {
  262. DB::beginTransaction();
  263. $role_info = json_encode($existed_role_info, 256);
  264. $boolen = DB::table('mp_book_version')->where('bid', $bid)->where('id', $version_id)->update(['role_info' => $role_info, 'updated_at' => date('Y-m-d H:i:s')]);
  265. if (!$boolen) {
  266. DB::rollBack();
  267. dd('更新角色信息失败');
  268. }
  269. $count = DB::table('mp_audio_tasks')->where('bid', $bid)->where('version_id', $version_id)->where('cid', $cid)->count('id');
  270. $chapter_audio = DB::table('mp_chapter_audios')->where('bid', $bid)->where('version_id', $version_id)->where('cid', $cid)->first();
  271. if (!$count) {
  272. $task_name = getProp($chapter_audio, 'book_name').' '.getProp($chapter_audio, 'chapter_name').'【'.getProp($chapter_audio, 'version_name').'】';
  273. }else {
  274. $task_name = getProp($chapter_audio, 'book_name').' '.getProp($chapter_audio, 'chapter_name').'【'.getProp($chapter_audio, 'version_name').'】('.($count+1).')';
  275. }
  276. $boolen1 = DB::table('mp_chapter_audios')->where('bid', $bid)->where('version_id', $version_id)->where('cid', $cid)->update(['generate_status'=>'执行中', 'generate_json' => $generate_json, 'updated_at' => date('Y-m-d H:i:s')]);
  277. if (!$boolen1) {
  278. DB::rollBack();
  279. dd('更新生成数据失败');
  280. }
  281. $boolen2 = DB::table('mp_audio_tasks')->insert([
  282. 'audio_id' => getProp($chapter_audio, 'id'),
  283. 'status' => '执行中',
  284. 'generate_json' => $generate_json,
  285. 'bid' => $bid,
  286. 'book_name' => getProp($chapter_audio, 'book_name'),
  287. 'version_id' => $version_id,
  288. 'version_name' => getProp($chapter_audio, 'version_name'),
  289. 'cid' => $cid,
  290. 'chapter_name' => getProp($chapter_audio, 'chapter_name'),
  291. 'task_name' => $task_name,
  292. 'created_at' => date('Y-m-d H:i:s'),
  293. 'updated_at' => date('Y-m-d H:i:s')
  294. ]);
  295. if (!$boolen2) {
  296. DB::rollBack();
  297. dd('创建任务失败');
  298. }
  299. } catch (\Exception $e) {
  300. DB::rollBack();
  301. dd('失败');
  302. }
  303. DB::commit();
  304. \Log::info('~~~~~~~~~~~~~~~~~~~~~~~~结束~~~~~~~~~~~~~~~~~~~~~~~~');
  305. dd('成功!');
  306. // \Log::info('generate_json: '.$generate_json);
  307. }
  308. /**
  309. * 处理历史视频任务 - 压缩视频并更新 compressed_url
  310. */
  311. private function handleHistoryTasks()
  312. {
  313. $this->info('开始处理历史视频任务...');
  314. try {
  315. // 查询所有 result_url 有值但 compressed_url 为空的成功任务
  316. $tasks = DB::table('mp_generate_video_tasks')
  317. ->where('status', 'success')
  318. ->whereNotNull('result_url')
  319. ->where('result_url', '!=', '')
  320. ->where(function($query) {
  321. $query->whereNull('compressed_url')
  322. ->orWhere('compressed_url', '');
  323. })
  324. ->select('id', 'result_url')
  325. ->get();
  326. $total = $tasks->count();
  327. $this->info("找到 {$total} 个需要处理的任务");
  328. if ($total === 0) {
  329. $this->info('没有需要处理的任务');
  330. return;
  331. }
  332. $successCount = 0;
  333. $failCount = 0;
  334. foreach ($tasks as $index => $task) {
  335. $taskId = $task->id;
  336. $resultUrl = $task->result_url;
  337. $this->info("处理任务 [{$taskId}] ({$index + 1}/{$total})");
  338. try {
  339. // 压缩视频
  340. $compressedUrl = compressVideo($resultUrl);
  341. // 如果压缩失败,使用原始URL
  342. $finalUrl = $compressedUrl ?: $resultUrl;
  343. // 更新数据库
  344. $updated = DB::table('mp_generate_video_tasks')
  345. ->where('id', $taskId)
  346. ->update([
  347. 'compressed_url' => $finalUrl,
  348. 'updated_at' => date('Y-m-d H:i:s')
  349. ]);
  350. if ($updated) {
  351. $successCount++;
  352. if ($compressedUrl) {
  353. $this->info("✓ 任务 [{$taskId}] 压缩成功");
  354. } else {
  355. $this->warn("⚠ 任务 [{$taskId}] 压缩失败,使用原始URL");
  356. }
  357. } else {
  358. $failCount++;
  359. $this->error("✗ 任务 [{$taskId}] 更新失败");
  360. }
  361. } catch (\Exception $e) {
  362. $failCount++;
  363. $this->error("✗ 任务 [{$taskId}] 处理异常: " . $e->getMessage());
  364. dLog('command')->error('处理历史视频任务异常', [
  365. 'task_id' => $taskId,
  366. 'error' => $e->getMessage()
  367. ]);
  368. }
  369. // 每处理10个任务休息一下,避免压力过大
  370. if (($index + 1) % 10 === 0) {
  371. sleep(1);
  372. }
  373. }
  374. $this->info("历史视频任务处理完成!");
  375. $this->info("总数: {$total}, 成功: {$successCount}, 失败: {$failCount}");
  376. } catch (\Exception $e) {
  377. $this->error('处理历史视频任务失败: ' . $e->getMessage());
  378. dLog('command')->error('处理历史视频任务失败', ['error' => $e->getMessage()]);
  379. }
  380. }
  381. /**
  382. * 处理历史分镜数据 - 根据视频任务更新分镜表
  383. */
  384. private function handleHistorySegments()
  385. {
  386. $this->info('开始处理历史分镜数据...');
  387. try {
  388. // 查询所有有 video_task_id 的分镜
  389. $segments = DB::table('mp_episode_segments')
  390. ->whereNotNull('video_task_id')
  391. ->where('video_task_id', '>', 0)
  392. ->select('segment_id', 'video_task_id')
  393. ->get();
  394. $total = $segments->count();
  395. $this->info("找到 {$total} 个需要处理的分镜");
  396. if ($total === 0) {
  397. $this->info('没有需要处理的分镜');
  398. return;
  399. }
  400. $successCount = 0;
  401. $skipCount = 0;
  402. $failCount = 0;
  403. foreach ($segments as $index => $segment) {
  404. $segmentId = $segment->segment_id;
  405. $videoTaskId = $segment->video_task_id;
  406. $this->info("处理分镜 [{$segmentId}] ({$index + 1}/{$total})");
  407. try {
  408. // 查询对应的视频任务
  409. $videoTask = DB::table('mp_generate_video_tasks')
  410. ->where('id', $videoTaskId)
  411. ->where('status', 'success')
  412. ->whereNotNull('result_url')
  413. ->where('result_url', '!=', '')
  414. ->whereNotNull('compressed_url')
  415. ->where('compressed_url', '!=', '')
  416. ->select('result_url', 'compressed_url')
  417. ->first();
  418. if (!$videoTask) {
  419. $skipCount++;
  420. $this->warn("⊘ 分镜 [{$segmentId}] 对应的视频任务不存在或未完成");
  421. continue;
  422. }
  423. // 更新分镜表
  424. $updated = DB::table('mp_episode_segments')
  425. ->where('segment_id', $segmentId)
  426. ->update([
  427. 'origin_video_url' => $videoTask->result_url,
  428. 'video_url' => $videoTask->compressed_url,
  429. 'updated_at' => date('Y-m-d H:i:s')
  430. ]);
  431. if ($updated) {
  432. $successCount++;
  433. $this->info("✓ 分镜 [{$segmentId}] 更新成功");
  434. } else {
  435. $failCount++;
  436. $this->error("✗ 分镜 [{$segmentId}] 更新失败");
  437. }
  438. } catch (\Exception $e) {
  439. $failCount++;
  440. $this->error("✗ 分镜 [{$segmentId}] 处理异常: " . $e->getMessage());
  441. dLog('command')->error('处理历史分镜数据异常', [
  442. 'segment_id' => $segmentId,
  443. 'video_task_id' => $videoTaskId,
  444. 'error' => $e->getMessage()
  445. ]);
  446. }
  447. // 每处理10个分镜休息一下
  448. if (($index + 1) % 10 === 0) {
  449. sleep(1);
  450. }
  451. }
  452. $this->info("历史分镜数据处理完成!");
  453. $this->info("总数: {$total}, 成功: {$successCount}, 跳过: {$skipCount}, 失败: {$failCount}");
  454. } catch (\Exception $e) {
  455. $this->error('处理历史分镜数据失败: ' . $e->getMessage());
  456. dLog('command')->error('处理历史分镜数据失败', ['error' => $e->getMessage()]);
  457. }
  458. }
  459. }