TestCommand.php 18 KB

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