TestCommand.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. $url = 'https://zw-audiobook.tos-cn-beijing.volces.com/video/ai_generation_1772704798_69a9541e94bef';
  45. $video_name = 'ai_generation_' . time() . '_' . uniqid();
  46. $video_ext = getVideoExtFromUrl($url);
  47. $video_name = $video_name . $video_ext;
  48. dump($video_name);
  49. $video_url = uploadStreamByTos('video', $url, $video_name);
  50. dd($video_url);
  51. // $url = 'https://zw-audiobook.tos-cn-beijing.volces.com/image/ai_generation_1772525682_69a69872e3a87..png';
  52. // // $url = 'https://zw-audiobook.tos-cn-beijing.volces.com/image/ai_generation_1772525682_69a69872e3a87.png';
  53. // $compressed_url = compressRemoteImageUrlToSize($url);
  54. // // $compressed_url = compressRemoteImageUrlToSizeImagick($url);
  55. // $filename = storage_path('app/public/video_frames/compressed.png');
  56. // // $url = uploadStreamByTos('image', $compressed_url, $filename);
  57. // $url = file_put_contents($filename, $compressed_url);
  58. // dd($url);
  59. // // 转存阿里云oss文件到tos
  60. // $audio_effects = DB::table('mp_timbres')->where('id', 246)->get();
  61. // $total = count($audio_effects);
  62. // $success = 0;
  63. // $continue = 0;
  64. // $fail = 0;
  65. // foreach ($audio_effects as $item) {
  66. // $id = getProp($item, 'id');
  67. // $url = getProp($item, 'audio_url');
  68. // if (!$url) {
  69. // $continue++;
  70. // continue;
  71. // }
  72. // if (!strstr($url, 'https://cdn-zwai.ycsd.cn')) {
  73. // $continue++;
  74. // continue;
  75. // }
  76. // $index = strrpos($url, '/');
  77. // $filename = mb_substr($url, $index + 1);
  78. // $stream = file_get_contents($url);
  79. // $new_url = uploadStreamByTos('demonstrate', $stream, $filename);
  80. // dd($new_url);
  81. // if ($new_url) {
  82. // $boolen = DB::table('mp_timbres')->where('id', $id)->update([
  83. // 'audio_url' => $new_url,
  84. // 'updated_at' => date('Y-m-d H:i:s'),
  85. // ]);
  86. // if ($boolen) $success++;
  87. // else $fail++;
  88. // }else {
  89. // dump('未生成新url');
  90. // $fail++;
  91. // }
  92. // }
  93. // dd("总条数:{$total}, 成功条数:{$success}, 跳过条数:{$continue}, 失败条数:{$fail}");
  94. exit();
  95. // $audio_effects = DB::table('mp_bgms')->where('is_enabled', 1)->where('playtime_seconds', 0)->get();
  96. // foreach ($audio_effects as $audio_effect) {
  97. // $id = getProp($audio_effect, 'id');
  98. // $url = getProp($audio_effect, 'bgm_url');
  99. // $index = strrpos($url, '/');
  100. // $name = 'The_Dawn';
  101. // // 将远程地址文件保存到本地
  102. // $filename = storage_path().'/app/public/audio_effect/' . $name;
  103. // if (!file_exists($filename)) {
  104. // $file = file_get_contents($url);
  105. // file_put_contents($filename, $file);
  106. // }
  107. // $getID3 = new getID3();
  108. // $ThisFileInfo = $getID3->analyze($filename);
  109. // $seconds = floor($ThisFileInfo['playtime_seconds']) ?? 0;
  110. // if ($seconds <= 0) $seconds = 1;
  111. // DB::table('mp_bgms')->where('id', $id)->update([
  112. // 'playtime_seconds' => $seconds,
  113. // 'updated_at' => date('Y-m-d H:i:s'),
  114. // ]);
  115. // unlink($filename);
  116. // }
  117. // exit();
  118. // $wavPath = public_path('audio');
  119. // if (!file_exists($wavPath)) {
  120. // return [];
  121. // }
  122. // $files = scandir($wavPath);
  123. // $error_voice = [];
  124. // foreach ($files as $file) {
  125. // if ($file !== '.' && $file !== '..') {
  126. // $voice_type = pathinfo($file, PATHINFO_FILENAME);
  127. // $boolen = DB::table('mp_timbres')->where('timbre_type', $voice_type)->update([
  128. // 'audio_url' => 'https://cdn-zwai.ycsd.cn/mp_audio/voice_demo/' . $file,
  129. // 'is_enabled' => 1,
  130. // 'updated_at' => date('Y-m-d H:i:s'),
  131. // ]);
  132. // if (!$boolen) $error_voice[] = $file;
  133. // }
  134. // }
  135. // \Log::info('错误文件:' . json_encode($error_voice, 256));
  136. // exit();
  137. // exit();
  138. $bid = 321;
  139. $version_id = 1663;
  140. $cid = 708197;
  141. $redis_key = "select-{$bid}-{$version_id}-{$cid}";
  142. // 查询更新后的信息
  143. $paragraph_urls = DB::table('mp_chapter_paragraph_audios')->where('bid', $bid)->where('version_id', $version_id)
  144. ->where('cid', $cid)->where(function($query) {
  145. return $query->where('generate_status', '!=', '制作中')->orWhere('error_msg', '!=', '');
  146. })->select('id', 'sequence', 'paragraph_audio_url', 'error_msg')
  147. ->get()->map(function ($value) {
  148. return (array)$value;
  149. })->toArray();
  150. $rem_ids = [];
  151. foreach ($paragraph_urls as $item) {
  152. $rem_ids[] = getProp($item, 'id');
  153. }
  154. // 删除已发送的id
  155. Redis::srem($redis_key, $rem_ids);
  156. dd(Redis::smembers($redis_key));
  157. exit();
  158. // $all_timbre = DB::table('mp_timbres')->get();
  159. // foreach($all_timbre as $timbre) {
  160. // $id = getProp($timbre, 'id');
  161. // if ($id > 1) {
  162. // $language = getProp($timbre, 'language');
  163. // $language = str_replace(['、', ','], ',', $language);
  164. // DB::table('mp_timbres')->where('id', $id)->update([
  165. // 'language' => $language,
  166. // 'first_category_id' => 0,
  167. // 'first_category_name' => '',
  168. // 'second_category_id' => 0,
  169. // 'second_category_name' => '',
  170. // 'third_category_id' => 0,
  171. // 'third_category_name' => '',
  172. // ]);
  173. // }
  174. // }
  175. // dd('end');
  176. ini_set('max_execution_time', 0);
  177. \Log::info('~~~~~~~~~~~~~~~~~~~~~~~~开始~~~~~~~~~~~~~~~~~~~~~~~~');
  178. // $a = DB::table('mp_emotion_list')->pluck('emotion_name')->toArray();
  179. // dd(implode('、', $a));
  180. // $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)';
  181. // $emotion_arr = explode(',', $emotion_str);
  182. // $insert_data = [];
  183. // foreach ($emotion_arr as $emotion) {
  184. // preg_match('/(.*)?((.*?))/', $emotion, $match);
  185. // if (count($match) == 3) {
  186. // $insert_data[] = [
  187. // 'emotion_code' => $match[2],
  188. // 'emotion_name' => $match[1],
  189. // 'is_enabled' => 1,
  190. // 'created_at' => date('Y-m-d H:i:s'),
  191. // 'updated_at' => date('Y-m-d H:i:s'),
  192. // ];
  193. // }
  194. // }
  195. // DB::table('mp_emotion_list')->insert($insert_data);
  196. // exit();
  197. $bid = 10917;
  198. $version_id = 1649;
  199. $cid = 2669608;
  200. // $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旁白:绝望将我包围,整个人都陷入深不见底的深渊。{自然讲述}"';
  201. // $text = '"旁白:刀尖刺穿手掌的瞬间,疼痛自掌心钻入骨髓。 \n旁白:心脏像是被万箭穿过一般,我痛得几近晕厥。 \n"';
  202. // $text = json_decode($text, true);
  203. // dd(handleScriptWords($text, 0));
  204. $text = $this->deepseekService->generateScriptWords($cid, 'r1');
  205. \Log::info('text: '.json_encode($text, 256));
  206. // $token = getTextTokens($text);
  207. $result = handleScriptWords($text);
  208. $role_gender = getProp($result, 'role_gender');
  209. $role_timbre = [];
  210. foreach ($role_gender as $role => $gender) {
  211. $timbre = DB::table('mp_timbres')->where('gender', $gender)->first();
  212. if (!$timbre) {
  213. $role_timbre[$role] = ['timbre_type' => 'zh_male_ruyayichen_emo_v2_mars_bigtts', 'timbre_name' => '儒雅男友'];
  214. }else {
  215. $timbre_name = str_replace('(多情感)', '', $timbre->timbre_name);
  216. $role_timbre[$role] = [
  217. 'timbre_type' => $timbre->timbre_type,
  218. 'timbre_name' => $timbre_name
  219. ];
  220. }
  221. }
  222. // 更新角色-音色信息
  223. $existed_role_info = DB::table('mp_book_version')->where('bid', $bid)->where('id', $version_id)->value('role_info');
  224. $existed_role_info = json_decode($existed_role_info, true);
  225. if ($existed_role_info) $existed_roles = array_keys($existed_role_info);
  226. else $existed_roles = [];
  227. foreach ($role_timbre as $role => $timbre) {
  228. if (!in_array($role, $existed_roles)) {
  229. $existed_role_info[$role] = $timbre;
  230. }
  231. }
  232. // 获取情感信息
  233. $emotion_list = DB::table('mp_emotion_list')->where('is_enabled', 1)->pluck('emotion_name', 'emotion_code')->toArray();
  234. $emotion_list = array_flip($emotion_list);
  235. // 构造生成音频的json
  236. $words = getProp($result, 'words');
  237. foreach($words as &$word) {
  238. $role = getProp($word, 'role');
  239. $word['gender'] = (int)$word['gender'];
  240. if (isset($emotion_list[getProp($word, 'emotion')])) { // 如果有对应情感则赋值,没有则默认为中性(neutral)
  241. $word['emotion_type'] = $emotion_list[getProp($word, 'emotion')];
  242. }else {
  243. $word['emotion'] = '中性';
  244. $word['emotion_type'] = 'neutral';
  245. }
  246. $word['voice_name'] = $role_timbre[$role]['timbre_name'];
  247. $word['voice_type'] = $role_timbre[$role]['timbre_type'];
  248. $word['speed_ratio'] = mt_rand(9,11)/10;
  249. $word['loudness_ratio'] = mt_rand(5,12)/10;
  250. $word['emotion_scale'] = mt_rand(1,5);
  251. }
  252. $generate_json = json_encode($words, 256);
  253. \Log::info('generate_json: '.$generate_json);
  254. try {
  255. DB::beginTransaction();
  256. $role_info = json_encode($existed_role_info, 256);
  257. $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')]);
  258. if (!$boolen) {
  259. DB::rollBack();
  260. dd('更新角色信息失败');
  261. }
  262. $count = DB::table('mp_audio_tasks')->where('bid', $bid)->where('version_id', $version_id)->where('cid', $cid)->count('id');
  263. $chapter_audio = DB::table('mp_chapter_audios')->where('bid', $bid)->where('version_id', $version_id)->where('cid', $cid)->first();
  264. if (!$count) {
  265. $task_name = getProp($chapter_audio, 'book_name').' '.getProp($chapter_audio, 'chapter_name').'【'.getProp($chapter_audio, 'version_name').'】';
  266. }else {
  267. $task_name = getProp($chapter_audio, 'book_name').' '.getProp($chapter_audio, 'chapter_name').'【'.getProp($chapter_audio, 'version_name').'】('.($count+1).')';
  268. }
  269. $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')]);
  270. if (!$boolen1) {
  271. DB::rollBack();
  272. dd('更新生成数据失败');
  273. }
  274. $boolen2 = DB::table('mp_audio_tasks')->insert([
  275. 'audio_id' => getProp($chapter_audio, 'id'),
  276. 'status' => '执行中',
  277. 'generate_json' => $generate_json,
  278. 'bid' => $bid,
  279. 'book_name' => getProp($chapter_audio, 'book_name'),
  280. 'version_id' => $version_id,
  281. 'version_name' => getProp($chapter_audio, 'version_name'),
  282. 'cid' => $cid,
  283. 'chapter_name' => getProp($chapter_audio, 'chapter_name'),
  284. 'task_name' => $task_name,
  285. 'created_at' => date('Y-m-d H:i:s'),
  286. 'updated_at' => date('Y-m-d H:i:s')
  287. ]);
  288. if (!$boolen2) {
  289. DB::rollBack();
  290. dd('创建任务失败');
  291. }
  292. } catch (\Exception $e) {
  293. DB::rollBack();
  294. dd('失败');
  295. }
  296. DB::commit();
  297. \Log::info('~~~~~~~~~~~~~~~~~~~~~~~~结束~~~~~~~~~~~~~~~~~~~~~~~~');
  298. dd('成功!');
  299. // \Log::info('generate_json: '.$generate_json);
  300. }
  301. }