TestCommand.php 18 KB

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