TestCommand.php 16 KB

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