| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034 |
- <?php
- namespace App\Http\Controllers\Anime;
- use App\Transformer\Anime\AnimeTransformer;
- use App\Facade\Site;
- use App\Consts\ErrorConst;
- use App\Exceptions\ApiException;
- use App\Libs\ApiResponse;
- use App\Libs\Utils;
- use App\Models\MpGenerateVideoTask;
- use App\Services\AIGeneration\AIImageGenerationService;
- use App\Services\AIGeneration\AIVideoGenerationService;
- use App\Services\Anime\AnimeService;
- use Illuminate\Http\Request;
- use Illuminate\Routing\Controller as BaseController;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Redis;
- use Illuminate\Support\Facades\Validator;
- class AnimeController extends BaseController
- {
- use ApiResponse;
- protected $AnimeService;
- protected $AIImageGenerationService;
- protected $AIVideoGenerationService;
- public function __construct(
- AnimeService $AnimeService,
- AIImageGenerationService $AIImageGenerationService,
- AIVideoGenerationService $AIVideoGenerationService
- ) {
- $this->AnimeService = $AnimeService;
- $this->AIImageGenerationService = $AIImageGenerationService;
- $this->AIVideoGenerationService = $AIVideoGenerationService;
- }
- // 文字模型
- public function textModel() {
- $query = DB::table('mp_text_models')->where('is_enabled', 1);
-
- // 支持 model 搜索
- if ($model = request('model')) {
- $query->where('model', 'like', "%{$model}%");
- }
-
- // 支持 name 搜索
- if ($name = request('name')) {
- $query->where('name', 'like', "%{$name}%");
- }
-
- $models = $query->select('model', 'name')->get()->map(function ($value) {
- return (array)$value;
- })->toArray();
-
- return $this->success($models);
- }
- // 图片模型
- public function imageModel() {
- $query = DB::table('mp_image_models')->where('is_enabled', 1);
-
- // 支持 model 搜索
- if ($model = request('model')) {
- $query->where('model', 'like', "%{$model}%");
- }
-
- // 支持 name 搜索
- if ($name = request('name')) {
- $query->where('name', 'like', "%{$name}%");
- }
-
- $models = $query->select('model', 'name')->get()->map(function ($value) {
- return (array)$value;
- })->toArray();
-
- return $this->success($models);
- }
- // 视频模型
- public function videoModel() {
- $query = DB::table('mp_video_models')->where('is_enabled', 1);
-
- // 支持 model 搜索
- if ($model = request('model')) {
- $query->where('model', 'like', "%{$model}%");
- }
-
- // 支持 name 搜索
- if ($name = request('name')) {
- $query->where('name', 'like', "%{$name}%");
- }
-
- $models = $query->select('model', 'name')->get()->map(function ($value) {
- return (array)$value;
- })->toArray();
-
- return $this->success($models);
- }
- public function artStyleList()
- {
- $result = [
- [
- 'art_style' => '唯美真人风格',
- 'pic_url' => "http://zw-ai.oss-cn-hangzhou.aliyuncs.com/mp_audio/art_styles/%E5%94%AF%E7%BE%8E%E7%9C%9F%E4%BA%BA.jpg"
- ],
- [
- 'art_style' => '真人古风风格',
- 'pic_url' => "http://zw-ai.oss-cn-hangzhou.aliyuncs.com/mp_audio/art_styles/%E7%9C%9F%E4%BA%BA%E5%8F%A4%E9%A3%8E.jpg"
- ],
- [
- 'art_style' => '日系动漫风格',
- 'pic_url' => "https://cdn-zwai.ycsd.cn/mp_audio/art_styles/日系动漫风格.jpg"
- ],
- [
- 'art_style' => '国漫风格',
- 'pic_url' => "https://cdn-zwai.ycsd.cn/mp_audio/art_styles/国漫风格.jpg"
- ],
- [
- 'art_style' => 'Q版卡通风格',
- 'pic_url' => "https://cdn-zwai.ycsd.cn/mp_audio/art_styles/Q版卡通风格.jpg"
- ],
- [
- 'art_style' => '简约扁平风格',
- 'pic_url' => "https://cdn-zwai.ycsd.cn/mp_audio/art_styles/简约扁平风格.jpg"
- ],
- [
- 'art_style' => '古风仙侠风格',
- 'pic_url' => "https://cdn-zwai.ycsd.cn/mp_audio/art_styles/古风仙侠风格.jpg"
- ],
- [
- 'art_style' => '武侠风格',
- 'pic_url' => "https://cdn-zwai.ycsd.cn/mp_audio/art_styles/武侠风格.jpg"
- ],
- [
- 'art_style' => '新中式水墨风格',
- 'pic_url' => "https://cdn-zwai.ycsd.cn/mp_audio/art_styles/新中式水墨风格.jpg"
- ],
- [
- 'art_style' => '写实插画风格',
- 'pic_url' => "https://cdn-zwai.ycsd.cn/mp_audio/art_styles/写实插画风格.jpg"
- ],
- [
- 'art_style' => '3D卡通风格',
- 'pic_url' => "https://cdn-zwai.ycsd.cn/mp_audio/art_styles/3D卡通风格.jpg"
- ],
- [
- 'art_style' => '条漫风格',
- 'pic_url' => "https://cdn-zwai.ycsd.cn/mp_audio/art_styles/条漫风格.jpg"
- ],
- [
- 'art_style' => '赛博朋克风格',
- 'pic_url' => "https://cdn-zwai.ycsd.cn/mp_audio/art_styles/赛博朋克风格.jpg"
- ],
- [
- 'art_style' => '暗黑悬疑风格',
- 'pic_url' => "https://cdn-zwai.ycsd.cn/mp_audio/art_styles/暗黑悬疑风格.jpg"
- ],
- [
- 'art_style' => '治愈清新风格',
- 'pic_url' => "https://cdn-zwai.ycsd.cn/mp_audio/art_styles/治愈清新风格.jpg"
- ],
- ];
- return $this->success($result);
- }
- // 新建动漫
- public function createAnime(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->createAnime($data);
- return $this->success(['anime_id' => $result]);
- }
- // 编辑动漫
- public function editAnime(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->editAnime($data);
- return $this->success(['success'=>$result ? 1 : 0]);
- }
- // 删除动漫
- public function delAnime(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->delAnime($data);
- return $this->success(['success'=>$result ? 1 : 0]);
- }
- // 批量生成主体图片
- public function batchSetRoleImg(Request $request) {
- // 忽略所有超时限制
- set_time_limit(0);
- ini_set('max_execution_time', '0');
- $data = $request->all();
- $result = $this->AnimeService->batchSetRoleImg($data);
- return $this->success($result);
- }
- // 批量生成场景图片
- public function batchSetSceneImg(Request $request) {
- // 忽略所有超时限制
- set_time_limit(0);
- ini_set('max_execution_time', '0');
- $data = $request->all();
- $result = $this->AnimeService->batchSetSceneImg($data);
- return $this->success($result);
- }
- // 修改分集主体列表
- public function changeEpisodeRoles(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->changeEpisodeRoles($data);
- return $this->success(['success'=>$result ? 1 : 0]);
- }
- // 修改分集场景列表
- public function changeEpisodeScenes(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->changeEpisodeScenes($data);
- return $this->success(['success'=>$result ? 1 : 0]);
- }
- // 编辑分镜剧本
- public function editSegment(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->editSegment($data);
- return $this->success(['success'=>$result ? 1 : 0]);
- }
- // 动漫对话列表
- public function chatList(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->chatList($data);
- return $this->success($result, [new AnimeTransformer(), 'newBuildChatList']);
- }
- // 动漫对话历史记录
- public function chatHistory(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->chatHistory($data);
- return $this->success($result);
- }
- // 动漫大纲
- public function animeDetail(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->animeDetail($data);
- return $this->success($result);
- }
- // 动漫剧集
- public function episodeInfo(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->episodeInfo($data);
- return $this->success($result);
- }
- // 分镜信息
- public function segmentInfo(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->segmentInfo($data);
- return $this->success($result);
- }
- // 复制剧集副本
- public function copyEpisodeVersion(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->copyEpisodeVersion($data);
- return $this->success($result);
- }
- // 剧集副本列表
- public function episodeVersions(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->episodeVersions($data);
- return $this->success($result);
- }
- // 绑定剧集副本
- public function bindEpisodeVersion(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->bindEpisodeVersion($data);
- return $this->success(['success' => $result ? 1 : 0]);
- }
- // 对话改图
- public function chatChangeImg(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->chatChangeImg($data);
- return $this->success(['img_url' => $result]);
- }
- public function segmentChatHistory(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->segmentChatHistory($data);
- return $this->success($result);
- }
- // 一键生成分镜
- public function batchSetSegmentPics(Request $request) {
- // 忽略所有超时限制
- set_time_limit(0);
- ini_set('max_execution_time', '0');
- $data = $request->all();
- $result = $this->AnimeService->batchSetSegmentPics($data);
- return $this->success($result);
- }
- // 重新生成分镜
- public function reGenerateSegment(Request $request) {
- // 忽略所有超时限制
- set_time_limit(0);
- ini_set('max_execution_time', '0');
- $data = $request->all();
- $result = $this->AnimeService->reGenerateSegment($data);
- return $this->success(['img_url'=>$result]);
- }
- // 添加分镜
- public function addSegment(Request $request) {
- // 忽略所有超时限制
- set_time_limit(0);
- ini_set('max_execution_time', '0');
- $data = $request->all();
- $result = $this->AnimeService->addSegment($data);
- return $this->success($result);
- }
- // 复制分镜
- public function copySegment(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->copySegment($data);
- return $this->success($result);
- }
- // 移动分镜
- public function moveSegment(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->moveSegment($data);
- return $this->success(['success'=>$result ? 1: 0]);
- }
- // 删除分镜
- public function delSegment(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->delSegment($data);
- return $this->success(['success'=>$result ? 1: 0]);
- }
- public function applySegment(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->applySegment($data);
- return $this->success(['success'=>$result ? 1: 0]);
- }
- // 分镜历史图片|视频
- public function segmentHistory(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->segmentHistory($data);
- return $this->success($result);
- }
- public function createSegmentVideoTask(Request $request) {
- $uid = Site::getUid();
- // 忽略所有超时限制
- set_time_limit(0);
- ini_set('max_execution_time', '0');
- $data = $request->all();
-
- // 验证参数
- $validator = Validator::make($data, [
- 'segment_id' => 'required|string',
- 'tail_frame' => 'nullable|string|max:500',
- ], [
- 'segment_id.required' => '分镜ID不能为空',
- 'tail_frame.max' => '尾帧描述不能超过500个字符',
- ]);
-
- if ($validator->fails()) {
- Utils::throwError('1002:' . $validator->errors()->first());
- }
-
- // 创建视频生成任务
- $result = $this->AnimeService->createSegmentVideoTask($data);
- $taskId = $result['task_id'];
-
- // 设置 SSE 响应头
- return response()->stream(function () use ($taskId, $result, $uid) {
- // 设置 SSE 响应头
- echo "data: " . json_encode([
- 'type' => 'init',
- 'data' => $result
- ]) . "\n\n";
- ob_flush();
- flush();
-
- $startTime = time();
- $maxDuration = 300; // 5分钟超时
- $checkInterval = 5; // 每5秒检查一次
-
- while (time() - $startTime < $maxDuration) {
- try {
- // 查询任务状态
- $task = \App\Models\MpGenerateVideoTask::find($taskId);
- if (!$task) {
- echo "data: " . json_encode([
- 'type' => 'error',
- 'message' => '任务不存在'
- ]) . "\n\n";
- ob_flush();
- flush();
- break;
- }
-
- // 如果任务还在处理中,查询最新状态
- if ($task->status === 'processing') {
- // 根据任务类型查询不同的状态
- if ($task->task_type === 'jimeng') {
- $statusResult = $this->AIVideoGenerationService->queryJimengTaskStatus($task);
- } elseif ($task->task_type === 'keling') {
- $statusResult = $this->AIVideoGenerationService->queryKelingOmniTaskStatus($task);
- } else {
- $statusResult = $this->AIVideoGenerationService->querySeedanceTaskStatus($task);
- }
- if (isset($statusResult['status'])) {
- // 更新任务状态
- $task->update([
- 'status' => $statusResult['status'],
- 'result_url' => $statusResult['result_url'] ?? null,
- 'compressed_url' => $statusResult['compressed_url'] ?? null,
- 'last_frame_url' => $statusResult['last_frame_url'] ?? '',
- 'error_message' => $statusResult['error_message'] ?? null,
- 'completed_at' => in_array($statusResult['status'], [
- 'success',
- 'failed'
- ]) ? now() : null
- ]);
-
- // 如果任务成功,更新分镜表
- if ($statusResult['status'] === 'success' && isset($statusResult['result_url'])) {
- try {
- DB::beginTransaction();
-
- $now = date('Y-m-d H:i:s');
-
- // 获取分镜ID
- $segment = DB::table('mp_episode_segments')
- ->where('video_task_id', $taskId)
- ->first();
-
- if (!$segment) {
- throw new \Exception('未找到对应的分镜记录');
- }
-
- $segmentId = $segment->segment_id;
- $anime_id = $segment->anime_id;
- $episode_number = $segment->episode_number;
- $tail_frame = $segment->tail_frame;
-
- // 更新分镜表
- $segmentUpdateData = [
- 'origin_video_url' => $statusResult['result_url'],
- 'video_task_status' => '已完成',
- 'last_frame_url' => $statusResult['last_frame_url'] ?? '',
- 'current_type' => 2,
- 'updated_at' => $now
- ];
- $compressed_video_url = compressVideo($statusResult['result_url']);
- $segmentUpdateData['video_url'] = $compressed_video_url ?: $statusResult['result_url'];
-
- // 只有当video_duration存在且大于0时才更新
- if (isset($statusResult['video_duration']) && $statusResult['video_duration'] > 0) {
- $segmentUpdateData['video_duration'] = $statusResult['video_duration'];
- $segmentUpdateData['video_time_point_start'] = 0;
- // $segmentUpdateData['video_time_point_end'] = $statusResult['video_duration'];
- $segmentUpdateData['video_time_point_end'] = $segment->audio_duration ?? 0;
- }
-
- $updateResult = DB::table('mp_episode_segments')
- ->where('segment_id', $segmentId)
- ->update($segmentUpdateData);
-
- if (!$updateResult) {
- Utils::throwError('20003:更新分镜表失败');
- }
- // 更新分镜视频成功后新增对话记录
- // 保存对话记录
- $records = [
- [
- 'uid' => $uid,
- 'anime_id' => $anime_id,
- 'sequence' => $episode_number,
- 'role' => 'user',
- 'content' => '图片转视频',
- 'segment_id' => $segmentId,
- 'video_url' => '',
- 'created_at' => $now,
- 'updated_at' => $now
- ],
- [
- 'uid' => $uid,
- 'anime_id' => $anime_id,
- 'sequence' => $episode_number,
- 'role' => 'assistant',
- 'content' => $tail_frame,
- 'segment_id' => $segmentId,
- 'video_url' => $statusResult['result_url'],
- 'created_at' => $now,
- 'updated_at' => $now
- ]
- ];
- DB::table('mp_anime_records')->insert($records);
-
- DB::commit();
-
- } catch (\Exception $e) {
- DB::rollBack();
-
- $logData = [
- 'task_id' => $taskId,
- 'error' => $e->getMessage()
- ];
- dLog('anime')->error('视频任务处理失败', $logData);
- logDB('anime', 'error', '视频任务处理失败', $logData);
- }
- } elseif ($statusResult['status'] === 'failed') {
- DB::table('mp_episode_segments')
- ->where('video_task_id', $taskId)
- ->update([
- 'video_task_status' => '失败',
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- }
- }
- }
-
- // 发送当前状态
- echo "data: " . json_encode([
- 'type' => 'update',
- 'data' => [
- 'task_id' => $task->id,
- 'status' => $task->status,
- 'result_url' => $task->result_url,
- 'error_message' => $task->error_message,
- 'elapsed_time' => time() - $startTime
- ]
- ]) . "\n\n";
- ob_flush();
- flush();
-
- // 如果任务完成(成功或失败),结束连接
- if (in_array($task->status, [
- 'success',
- 'failed'
- ])) {
- echo "data: " . json_encode([
- 'type' => 'completed',
- 'data' => [
- 'task_id' => $task->id,
- 'status' => $task->status,
- 'video_url' => $task->compressed_url ?: $task->result_url,
- 'origin_video_url' => $task->result_url,
- 'last_frame_url' => $task->last_frame_url,
- 'error_message' => $task->error_message
- ]
- ]) . "\n\n";
- ob_flush();
- flush();
- break;
- }
-
- sleep($checkInterval);
-
- } catch (\Exception $e) {
- echo "data: " . json_encode([
- 'type' => 'error',
- 'message' => '查询任务状态失败: ' . $e->getMessage()
- ]) . "\n\n";
- ob_flush();
- flush();
- sleep($checkInterval);
- }
- }
-
- // 超时处理
- if (time() - $startTime >= $maxDuration) {
- echo "data: " . json_encode([
- 'type' => 'time_out',
- 'message' => '任务执行超时,请稍后查询任务状态',
- 'data' => [
- 'task_id' => $taskId
- ]
- ]) . "\n\n";
- ob_flush();
- flush();
- }
-
- }, 200, [
- 'Content-Type' => 'text/event-stream',
- 'Cache-Control' => 'no-cache',
- 'Connection' => 'keep-alive',
- 'X-Accel-Buffering' => 'no', // 禁用 Nginx 缓冲
- ]);
- }
- /**
- * 批量生成分镜视频
- */
- public function batchSetSegmentVideos(Request $request) {
- // 忽略所有超时限制
- set_time_limit(0);
- ini_set('max_execution_time', '0');
- $data = $request->all();
-
- // 验证参数
- $validator = Validator::make($data, [
- 'anime_id' => 'required|string',
- 'episode_id' => 'required|integer',
- ], [
- 'anime_id.required' => '动漫对话ID不能为空',
- 'episode_id.required' => '剧集ID不能为空',
- 'episode_id.integer' => '剧集ID必须是整数',
- ]);
-
- if ($validator->fails()) {
- Utils::throwError('1002:' . $validator->errors()->first());
- }
-
- $animeId = $data['anime_id'];
- $episodeId = $data['episode_id'];
- $episode = DB::table('mp_anime_episodes')->where('id', $episodeId)->where('anime_id', $animeId)->first();
- if (!$episode) Utils::throwError('20003:分集不存在');
- $episodeNumber = $episode->episode_number;
- $ratio = !empty($episode->ratio) ? $episode->ratio : '9:16';
- $generate_audio = getProp($data, 'generate_audio', 0);
-
- // 处理视频模型(在循环外统一处理)
- $model = getProp($data, 'model');
- if (!$model) {
- // 用户没有输入,从episode表获取
- $model = getProp($episode, 'video_model');
- if (!$model) {
- // episode表也没有,使用默认值
- $model = 'doubao-seedance-1-5-pro-251215';
- }
- }
- // 验证模型是否在可用模型表中
- if (!DB::table('mp_video_models')->where('model', $model)->where('is_enabled', 1)->exists()) {
- $model = 'doubao-seedance-1-5-pro-251215';
- }
-
- // 获取所有分镜信息
- $segments = DB::table('mp_episode_segments')
- ->where('anime_id', $animeId)
- ->where('episode_id', $episodeId)
- ->whereNotIn('video_task_status', ['已完成', '生成中'])
- ->orderBy('segment_number')
- ->get();
-
- if ($segments->isEmpty()) {
- Utils::throwError('20003:未找到分镜数据');
- }
-
- // 批量创建视频任务
- $taskIds = [];
- $segmentTasks = [];
-
- foreach ($segments as $segment) {
- try {
- // 检查视频任务正在生成中或当前状态为视频
- if ($segment->video_task_status === '生成中' || (int)$segment->current_type === 2) {
- continue;
- }
-
- // 获取分镜内容
- $segmentContent = $segment->segment_content ?: '';
- $tailFrame = $segment->tail_frame ?: '';
-
- // 判断 $segmentContent 是否有台词内容,如果有则确保使用中文左右双引号
- $hasDialogue = false;
- if (preg_match('/(?:台词内容|台词|对白|对话)[::]\s*([^\n]+)/u', $segmentContent, $dialogueMatch)) {
- $dialogue = trim($dialogueMatch[1]);
-
- // 如果台词不为空且不是"无"
- if (!empty($dialogue) && $dialogue !== '无') {
- // 过滤掉所有符号(引号、标点、空格等),只保留文字内容来判断是否有实际台词
- $dialogueFiltered = preg_replace('/[""\'\',。!?、;:…—\-\s\p{P}\p{S}]/u', '', $dialogue);
- if (!empty($dialogueFiltered) && $dialogueFiltered !== '无') {
- $hasDialogue = true;
- }
-
- $dialogue = preg_replace('/^["”]/u', '“', $dialogue); // 替换句首英文双引号或中文右双引号
- $dialogue = preg_replace('/["“]$/u', '”', $dialogue); // 替换句尾英文双引号或中文左双引号
- // 句首或句尾没有中文双引号,则分别添加中文左右双引号
- if (!preg_match('/^[“]/', $dialogue)) {
- $dialogue = '“' . $dialogue;
- }
- if (!preg_match('/[”]$/', $dialogue)) {
- $dialogue .= '”';
- }
-
- // 将修改后的台词替换回 $segmentContent
- $originalDialogue = $dialogueMatch[1];
- $segmentContent = str_replace($originalDialogue, $dialogue, $segmentContent);
- }
- }
-
- // 检查 $segmentContent 中是否已有尾帧描述
- $contentHasTailFrame = preg_match('/(?:尾帧描述|尾帧)[::]\s*([^\n]+)/u', $segmentContent, $contentTailFrameMatch);
-
- // 确定最终使用的尾帧描述(优先级:segmentContent自带 > 分镜表字段)
- $finalTailFrame = '';
- if ($contentHasTailFrame) {
- // segmentContent 中有尾帧描述,使用它
- $finalTailFrame = trim($contentTailFrameMatch[1]);
- } else {
- // segmentContent 中没有,使用分镜表中的尾帧描述
- $finalTailFrame = $tailFrame;
- }
-
- // 构建完整的提示词
- $fullPrompt = $segmentContent;
- if ($finalTailFrame && !$contentHasTailFrame) {
- // 只有当 segmentContent 中没有尾帧描述时才追加
- $fullPrompt .= "\n尾帧描述:$finalTailFrame";
- }
-
- // 去除多余的换行符(将连续多个换行符替换为单个换行符)
- $fullPrompt = preg_replace('/\n{2,}/', "\n", $fullPrompt);
- $fullPrompt = trim($fullPrompt);
- // 当前是否启用音效同出(没台词强制关闭音效同出,节省资源收费)
- $current_generate_audio = $hasDialogue ? $generate_audio : 0;
-
- // 智能选择视频时长
- // $videoDuration = $this->AnimeService->calculateOptimalVideoDuration($segmentContent, $tailFrame);
- $videoDuration = -1;
-
- // 构建视频生成参数
- $videoParams = [
- 'model' => $model,
- 'alias_segment_id' => $segment->segment_id,
- 'prompt' => $fullPrompt,
- 'video_duration' => $videoDuration,
- 'video_resolution' => '720P',
- 'seed' => -1,
- 'ratio' => $ratio,
- 'generate_audio' => (int)$current_generate_audio === 0 ? false : true,
- 'draft' => false,
- 'watermark' => false,
- 'camera_fixed' => false,
- // 'callback_url' => 'http://mpaudio.yqsd.cn/api/video/seedanceCallback'
- ];
-
- // 如果分镜有图片,作为首帧
- $hasImage = !empty($segment->img_url);
- $hasVideo = !empty($segment->video_url);
-
- if ($hasImage) {
- $videoParams['first_frame_url'] = $segment->img_url;
- }
-
- // 构建content数组
- $videoParams['content'] = [
- [
- 'type' => 'text',
- 'text' => $videoParams['prompt'],
- ]
- ];
-
- // 如果有首帧图片,添加到content中
- if ($hasImage) {
- if (in_array($model, ['doubao-seedance-2-0-260128', 'doubao-seedance-2-0-fast-260128'])) {
- $videoParams['content'][] = [
- 'type' => 'image_url',
- 'image_url' => [
- 'url' => $videoParams['first_frame_url'],
- ],
- 'role' => 'reference_image',
- ];
- }else {
- $videoParams['content'][] = [
- 'type' => 'image_url',
- 'image_url' => [
- 'url' => $videoParams['first_frame_url'],
- ],
- 'role' => 'first_frame',
- ];
- }
- }
-
- // 获取配音音频URL和时长
- $audioUrl = $segment->audio_url ?? '';
- $audioDuration = $segment->audio_duration ?? 0;
-
- // 音频传入的前提:必须有至少一张图片或一个视频
- $canUseAudio = ($hasImage || $hasVideo) && !empty($audioUrl);
-
- // 根据模型选择不同的视频生成方法
- if (strpos($model, 'jimeng') !== false) {
- // 即梦模型参数调整
- $videoParams['video_duration'] = $audioDuration > 5 ? 10 : 5; // 即梦只支持5s或10s
- $videoParams['aspect_ratio'] = $videoParams['ratio']; // 即梦使用aspect_ratio
- unset($videoParams['ratio']);
- unset($videoParams['content']); // 即梦不使用content格式
- $task = $this->AIVideoGenerationService->createJimengTask($videoParams);
- } elseif (strpos($model, 'kling') !== false) {
- // Keling模型参数调整
- $videoParams['video_duration'] = max(3, min(15, ceil($audioDuration))); // Keling支持3-15秒
- $videoParams['aspect_ratio'] = $videoParams['ratio']; // Keling使用aspect_ratio
- unset($videoParams['ratio']);
- unset($videoParams['content']); // Keling不使用content格式
- $task = $this->AIVideoGenerationService->createKelingOmniTask($videoParams);
- } else {
- // 检查是否为 Seedance 2.0 模型,如果是则需要处理配音音频
- if ($canUseAudio && in_array($model, ['doubao-seedance-2-0-260128', 'doubao-seedance-2-0-fast-260128'])) {
- // 添加配音音频到content中
- $videoParams['content'][] = [
- 'type' => 'audio_url',
- 'audio_url' => [
- 'url' => $audioUrl,
- ],
- 'role' => 'reference_audio',
- ];
-
- // 优化提示词,增加音频相关描述
- $audioPrompt = "全程使用音频1作为视频配音。" . $fullPrompt;
- $videoParams['prompt'] = $audioPrompt;
- $videoParams['content'][0]['text'] = $audioPrompt;
- } else if ($audioDuration) {
- // 有音频时长则需将视频时长设置为比音频时长更长的整数
- $videoParams['video_duration'] = max(4, min(12, ceil($audioDuration))); // seedance支持2-12s
- }
- // Seedance模型(默认)
- $task = $this->AIVideoGenerationService->createSeedanceTask($videoParams);
- }
- $taskIds[] = $task->id;
-
- // 更新分镜表的视频任务信息
- DB::table('mp_episode_segments')
- ->where('segment_id', $segment->segment_id)
- ->update([
- 'video_task_id' => $task->id,
- 'video_task_status' => '生成中',
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
-
- $segmentTasks[] = [
- 'segment_id' => $segment->segment_id,
- 'task_id' => $task->id,
- 'segment_number' => $segment->segment_number,
- 'tail_frame' => $segment->tail_frame,
- 'audio_duration' => $segment->audio_duration,
- ];
-
- } catch (\Exception $e) {
- $logData = [
- 'segment_id' => $segment->segment_id,
- 'anime_id' => $animeId,
- 'error' => $e->getMessage(),
- ];
- dLog('anime')->error('创建分镜视频任务失败', $logData);
- logDB('anime', 'error', '创建分镜视频任务失败', $logData);
- continue;
- }
- }
-
- if (empty($taskIds)) {
- Utils::throwError('20003:没有需要生成视频的分镜');
- }
-
- // 保存模型到episode表
- DB::table('mp_anime_episodes')->where('id', $episodeId)->update([
- 'video_model' => $model,
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
-
- // 记录开始时间(包含创建任务的时间)
- $startTime = time();
- $maxDuration = 1800; // 30分钟超时
-
- $uid = Site::getUid();
- // 设置 SSE 响应头并开始长连接
- return response()->stream(function () use ($taskIds, $segmentTasks, $animeId, $episodeNumber, $episodeId, $startTime, $maxDuration, $uid) {
- $checkInterval = 10; // 每10秒检查一次
-
- // 初始化任务数组
- $totalTasks = $taskIds; // 所有任务ID
- $completeTasks = []; // 已完成的任务ID(包含失败)
- $successTasks = []; // 成功的任务ID
-
- // 发送初始任务创建信息
- echo "data: " . json_encode([
- 'type' => 'init',
- 'total_tasks_count' => count($totalTasks),
- 'completed_tasks_count' => count($completeTasks),
- 'success_tasks_count' => count($successTasks)
- ]) . "\n\n";
- ob_flush();
- flush();
-
- while (time() - $startTime < $maxDuration) {
- try {
- $hasUpdate = false; // 标记是否有更新
-
- // 获取所有任务的最新状态
- foreach ($totalTasks as $taskId) {
- // 如果任务已经在完成列表中,跳过
- if (in_array($taskId, $completeTasks)) {
- continue;
- }
-
- $task = \App\Models\MpGenerateVideoTask::find($taskId);
- if (!$task) {
- continue;
- }
-
- // 如果任务还在处理中,查询最新状态
- if ($task->status === 'processing') {
- // 根据任务类型查询不同的状态
- if ($task->task_type === 'jimeng') {
- $statusResult = $this->AIVideoGenerationService->queryJimengTaskStatus($task);
- } elseif ($task->task_type === 'keling') {
- $statusResult = $this->AIVideoGenerationService->queryKelingOmniTaskStatus($task);
- } else {
- $statusResult = $this->AIVideoGenerationService->querySeedanceTaskStatus($task);
- }
-
- if (isset($statusResult['status'])) {
- // 更新任务状态
- $task->update([
- 'status' => $statusResult['status'],
- 'result_json' => $statusResult['result_json'] ?? [],
- 'result_url' => $statusResult['result_url'] ?? null,
- 'last_frame_url' => $statusResult['last_frame_url'] ?? '',
- 'error_message' => $statusResult['error_message'] ?? null,
- 'completed_at' => in_array($statusResult['status'], [
- 'success',
- 'failed'
- ]) ? now() : null
- ]);
-
- // 如果任务成功,更新分镜表
- if ($statusResult['status'] === 'success' && isset($statusResult['result_url'])) {
- try {
- DB::beginTransaction();
-
- $now = date('Y-m-d H:i:s');
-
- // 获取分镜ID
- $segmentId = null;
- foreach ($segmentTasks as $segmentTask) {
- if ($segmentTask['task_id'] == $taskId) {
- $segmentId = $segmentTask['segment_id'];
- $tail_frame = $segmentTask['tail_frame'];
- $audio_duration = $segmentTask['audio_duration'];
- break;
- }
- }
-
- if ($segmentId) {
- // 更新分镜表
- $segmentUpdateData = [
- 'origin_video_url' => $statusResult['result_url'],
- 'video_task_status' => '已完成',
- 'last_frame_url' => $statusResult['last_frame_url'] ?? '',
- 'current_type' => 2,
- 'updated_at' => $now
- ];
- $compressed_video_url = compressVideo($statusResult['result_url']);
- $segmentUpdateData['video_url'] = $compressed_video_url ?: $statusResult['result_url'];
-
- // 只有当video_duration存在且大于0时才更新
- if (isset($statusResult['video_duration']) && $statusResult['video_duration'] > 0) {
- $segmentUpdateData['video_duration'] = $statusResult['video_duration'];
- $segmentUpdateData['video_time_point_start'] = 0;
- // $segmentUpdateData['video_time_point_end'] = $statusResult['video_duration'];
- $segmentUpdateData['video_time_point_end'] = $audio_duration ?? 0;
- }
-
- $updateResult = DB::table('mp_episode_segments')
- ->where('segment_id', $segmentId)
- ->update($segmentUpdateData);
-
- if ($updateResult) {
- // 更新分镜视频成功后新增对话记录
- $records = [
- [
- 'uid' => $uid,
- 'anime_id' => $animeId,
- 'sequence' => $episodeNumber,
- 'role' => 'user',
- 'content' => '图片转视频',
- 'segment_id' => $segmentId,
- 'video_url' => '',
- 'created_at' => $now,
- 'updated_at' => $now
- ],
- [
- 'uid' => $uid,
- 'anime_id' => $animeId,
- 'sequence' => $episodeNumber,
- 'role' => 'assistant',
- 'content' => $tail_frame ?? '',
- 'segment_id' => $segmentId,
- 'video_url' => $statusResult['result_url'],
- 'created_at' => $now,
- 'updated_at' => $now
- ]
- ];
-
- DB::table('mp_anime_records')->insert($records);
- }
- }
-
- DB::commit();
-
- } catch (\Exception $e) {
- DB::rollBack();
-
- $logData = [
- 'task_id' => $taskId,
- 'error' => $e->getMessage()
- ];
- dLog('anime')->error('视频任务处理失败', $logData);
- logDB('anime', 'error', '视频任务处理失败', $logData);
- }
-
- } elseif ($statusResult['status'] === 'failed') {
- // 获取分镜ID并更新失败状态
- foreach ($segmentTasks as $segmentTask) {
- if ($segmentTask['task_id'] == $taskId) {
- DB::table('mp_episode_segments')
- ->where('segment_id', $segmentTask['segment_id'])
- ->update([
- 'video_task_status' => '失败',
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- break;
- }
- }
- }
- }
- }
-
- // 检查任务是否完成(成功或失败)
- if (in_array($task->status, ['success', 'failed'])) {
- $completeTasks[] = $taskId;
- $hasUpdate = true;
-
- if ($task->status === 'success') {
- $successTasks[] = $taskId;
- }
- }
- }
-
- // 如果有更新,发送update通知
- if ($hasUpdate) {
- echo "data: " . json_encode([
- 'type' => 'update',
- 'total_tasks_count' => count($totalTasks),
- 'completed_tasks_count' => count($completeTasks),
- 'success_tasks_count' => count($successTasks)
- ]) . "\n\n";
- ob_flush();
- flush();
- }
-
- // 如果所有任务都完成了,结束连接
- if (count($completeTasks) >= count($totalTasks)) {
- // 获取所有成功任务的分镜信息
- $successSegments = [];
- foreach ($successTasks as $taskId) {
- // 通过taskId找到对应的segmentTask
- foreach ($segmentTasks as $segmentTask) {
- if ($segmentTask['task_id'] == $taskId) {
- $segment = DB::table('mp_episode_segments')
- ->where('segment_id', $segmentTask['segment_id'])
- ->select('segment_id', 'video_url')
- ->first();
-
- if ($segment && !empty($segment->video_url)) {
- $successSegments[] = [
- 'segment_id' => $segment->segment_id,
- 'video_url' => $segment->video_url
- ];
- }
- break;
- }
- }
- }
-
- echo "data: " . json_encode([
- 'type' => 'completed',
- 'total_tasks_count' => count($totalTasks),
- 'completed_tasks_count' => count($completeTasks),
- 'success_tasks_count' => count($successTasks),
- 'success_segments' => $successSegments
- ]) . "\n\n";
- ob_flush();
- flush();
- break;
- }
-
- // 检查是否超时
- if (time() - $startTime >= $maxDuration) {
- break;
- }
-
- sleep($checkInterval);
-
- } catch (\Exception $e) {
- echo "data: " . json_encode([
- 'type' => 'error',
- 'message' => '查询任务状态失败: ' . $e->getMessage(),
- 'total_tasks_count' => count($totalTasks),
- 'completed_tasks_count' => count($completeTasks),
- 'success_tasks_count' => count($successTasks)
- ]) . "\n\n";
- ob_flush();
- flush();
-
- // 检查是否超时,如果超时则停止循环
- if (time() - $startTime >= $maxDuration) {
- break;
- }
-
- sleep($checkInterval);
- }
- }
-
- // 超时处理
- if (time() - $startTime >= $maxDuration) {
- echo "data: " . json_encode([
- 'type' => 'time_out',
- 'message' => '批量视频生成已超时30分钟,停止状态查询',
- 'total_tasks_count' => count($totalTasks),
- 'completed_tasks_count' => count($completeTasks),
- 'success_tasks_count' => count($successTasks)
- ]) . "\n\n";
- ob_flush();
- flush();
- }
-
- }, 200, [
- 'Content-Type' => 'text/event-stream',
- 'Cache-Control' => 'no-cache',
- 'Connection' => 'keep-alive',
- 'X-Accel-Buffering' => 'no', // 禁用 Nginx 缓冲
- ]);
- }
- // 应用有声制作参数
- public function applyAudioData(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->applyAudioData($data);
- return $this->success($result);
- }
- // 获取分集生成图片信息
- public function episodePicsInfo(Request $request) {
- // 忽略所有超时限制
- set_time_limit(0);
- ini_set('max_execution_time', '0');
- $data = $request->all();
- // 获取生成器函数
- $streamGenerator = $this->AnimeService->episodePicsInfo($data);
- // 返回 SSE 响应
- return response()->stream($streamGenerator, 200, [
- 'Content-Type' => 'text/event-stream',
- 'Cache-Control' => 'no-cache',
- 'Connection' => 'keep-alive',
- 'X-Accel-Buffering' => 'no', // 禁用 Nginx 缓冲
- ]);
- }
- public function clipSegmentVideo(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->clipSegmentVideo($data);
- return $this->success(['success'=>$result ? 1: 0]);
- }
- /**
- * 创建完整视频合成任务
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- */
- public function createCompleteVideoTask(Request $request)
- {
- // 忽略所有超时限制
- set_time_limit(0);
- ini_set('max_execution_time', '0');
- $data = $request->all();
-
- // 验证参数
- $validator = Validator::make($data, [
- 'anime_id' => 'required|string',
- 'episode_id' => 'required|string',
- ], [
- 'anime_id.required' => '请选择动漫',
- 'episode_id.required' => '请选择分集',
- ]);
-
- if ($validator->fails()) {
- Utils::throwError('1002:' . $validator->errors()->first());
- }
-
- try {
- $result = $this->AnimeService->createCompleteVideoTask($data);
- return $this->success($result);
- } catch (\Exception $e) {
- $logData = [
- 'anime_id' => $data['anime_id'] ?? '',
- 'episode_id' => $data['episode_id'] ?? '',
- 'error' => $e->getMessage()
- ];
- dLog('anime')->error('创建完整视频合成任务失败', $logData);
- logDB('anime', 'error', '创建完整视频合成任务失败', $logData);
- return $this->error('20003:'.$e->getMessage());
- }
- }
- // 文生图(通用)
- public function generateImg(Request $request) {
- // 忽略所有超时限制
- set_time_limit(0);
- ini_set('max_execution_time', '0');
- $data = $request->all();
- $result = $this->AnimeService->generateImg($data);
- return $this->success($result);
- }
- /**
- * 导出分集的全部图片(下载并打包)
- */
- public function exportEpisodeImages(Request $request) {
- // 忽略所有超时限制
- set_time_limit(0);
- ini_set('max_execution_time', '0');
- ini_set('memory_limit', '512M');
- $data = $request->all();
-
- // 验证参数
- $validator = Validator::make($data, [
- 'anime_id' => 'required|string',
- 'episode_id' => 'required|string',
- ], [
- 'anime_id.required' => '动漫ID不能为空',
- 'episode_id.required' => '剧集ID不能为空',
- ]);
-
- if ($validator->fails()) {
- Utils::throwError('1002:' . $validator->errors()->first());
- }
-
- $animeId = $data['anime_id'];
- $episodeId = $data['episode_id'];
-
- try {
- // 获取剧集信息
- $episode = DB::table('mp_anime_episodes')
- ->where('anime_id', $animeId)
- ->where('id', $episodeId)
- ->select('episode_number', 'title')
- ->first();
-
- if (!$episode) {
- Utils::throwError('20003:未找到剧集信息');
- }
-
- // 获取所有分镜图片和音频
- $segments = DB::table('mp_episode_segments')
- ->where('anime_id', $animeId)
- ->where('episode_id', $episodeId)
- ->whereNotNull('img_url')
- ->where('img_url', '!=', '')
- ->orderBy('segment_number')
- ->select('segment_id', 'segment_number', 'img_url', 'video_url', 'audio_url', 'act_number', 'act_title')
- ->get();
-
- if ($segments->isEmpty()) {
- Utils::throwError('20003:该剧集暂无图片可导出');
- }
-
- // 创建临时目录
- $tempDir = storage_path('app/temp/episode_images_' . time() . '_' . mt_rand(1000, 9999));
- if (!file_exists($tempDir)) {
- mkdir($tempDir, 0755, true);
- }
-
- // 下载图片或视频
- $downloadedFiles = [];
- foreach ($segments as $segment) {
- try {
- // 优先使用video_url,如果没有则使用img_url
- $mediaUrl = !empty($segment->video_url) ? $segment->video_url : $segment->img_url;
-
- if (empty($mediaUrl)) {
- continue;
- }
-
- // 获取文件扩展名
- $extension = pathinfo(parse_url($mediaUrl, PHP_URL_PATH), PATHINFO_EXTENSION);
- if (empty($extension)) {
- // 根据URL类型设置默认扩展名
- $extension = !empty($segment->video_url) ? 'mp4' : 'jpg';
- }
-
- // 构建文件名:分镜序号.扩展名
- $fileName = sprintf(
- '%03d.%s',
- $segment->segment_number,
- $extension
- );
- $filePath = $tempDir . '/' . $fileName;
-
- // 下载文件(使用file_get_contents,兼容性更好)
- $context = stream_context_create([
- 'http' => [
- 'timeout' => 30,
- 'follow_location' => 1,
- 'ignore_errors' => true
- ],
- 'ssl' => [
- 'verify_peer' => false,
- 'verify_peer_name' => false
- ]
- ]);
-
- $fileData = @file_get_contents($mediaUrl, false, $context);
-
- if ($fileData !== false && !empty($fileData)) {
- file_put_contents($filePath, $fileData);
- $downloadedFiles[] = $filePath;
- } else {
- $logData = [
- 'segment_id' => $segment->segment_id,
- 'media_url' => $mediaUrl,
- 'type' => !empty($segment->video_url) ? 'video' : 'image'
- ];
- dLog('anime')->warning('文件下载失败', $logData);
- logDB('anime', 'warning', '文件下载失败', $logData);
- }
-
- } catch (\Exception $e) {
- $logData = [
- 'segment_id' => $segment->segment_id,
- 'video_url' => $segment->video_url,
- 'img_url' => $segment->img_url,
- 'error' => $e->getMessage()
- ];
- dLog('anime')->error('下载文件异常', $logData);
- logDB('anime', 'error', '下载文件异常', $logData);
- continue;
- }
- }
-
- if (empty($downloadedFiles)) {
- // 清理临时目录
- if (file_exists($tempDir)) {
- array_map('unlink', glob("$tempDir/*"));
- rmdir($tempDir);
- }
- Utils::throwError('20003:没有成功下载任何图片');
- }
-
- // 处理音频合成
- $mergedAudioPath = null;
- try {
- // 检查 FFmpeg 是否可用
- exec('which ffmpeg 2>&1', $ffmpegCheck, $ffmpegReturnCode);
- $hasFfmpeg = ($ffmpegReturnCode === 0);
-
- if ($hasFfmpeg) {
- // 下载所有分镜的音频文件
- $audioFiles = [];
- foreach ($segments as $segment) {
- if (!empty($segment->audio_url)) {
- try {
- $context = stream_context_create([
- 'http' => [
- 'timeout' => 30,
- 'follow_location' => 1,
- 'ignore_errors' => true
- ],
- 'ssl' => [
- 'verify_peer' => false,
- 'verify_peer_name' => false
- ]
- ]);
-
- $audioData = @file_get_contents($segment->audio_url, false, $context);
-
- if ($audioData !== false && !empty($audioData)) {
- // 获取音频文件扩展名
- $audioExtension = pathinfo(parse_url($segment->audio_url, PHP_URL_PATH), PATHINFO_EXTENSION);
- if (empty($audioExtension)) {
- $audioExtension = 'wav';
- }
-
- $audioFileName = sprintf('audio_%03d.%s', $segment->segment_number, $audioExtension);
- $audioFilePath = $tempDir . '/' . $audioFileName;
- file_put_contents($audioFilePath, $audioData);
- $audioFiles[] = $audioFilePath;
- } else {
- $logData = [
- 'segment_id' => $segment->segment_id,
- 'audio_url' => $segment->audio_url
- ];
- dLog('anime')->warning('音频下载失败', $logData);
- logDB('anime', 'warning', '音频下载失败', $logData);
- }
- } catch (\Exception $e) {
- $logData = [
- 'segment_id' => $segment->segment_id,
- 'audio_url' => $segment->audio_url,
- 'error' => $e->getMessage()
- ];
- dLog('anime')->error('下载音频异常', $logData);
- logDB('anime', 'error', '下载音频异常', $logData);
- }
- }
- }
-
- // 如果有音频文件,则进行合成
- if (!empty($audioFiles)) {
- // 创建 FFmpeg concat 文件列表
- $concatFile = $tempDir . '/concat.txt';
- $concatContent = '';
- foreach ($audioFiles as $audioFile) {
- // 使用相对路径,避免路径中的特殊字符问题
- $concatContent .= "file '" . basename($audioFile) . "'\n";
- }
- file_put_contents($concatFile, $concatContent);
-
- // 合成音频文件
- $mergedAudioPath = $tempDir . '/tts.wav';
-
- // 使用 FFmpeg 合并音频,转换为统一的 WAV 格式
- $ffmpegCommand = sprintf(
- 'cd %s && ffmpeg -f concat -safe 0 -i %s -ar 44100 -ac 2 -y %s 2>&1',
- escapeshellarg($tempDir),
- escapeshellarg(basename($concatFile)),
- escapeshellarg(basename($mergedAudioPath))
- );
-
- exec($ffmpegCommand, $ffmpegOutput, $ffmpegReturnCode);
-
- if ($ffmpegReturnCode !== 0 || !file_exists($mergedAudioPath)) {
- $logData = [
- 'command' => $ffmpegCommand,
- 'output' => implode("\n", $ffmpegOutput),
- 'return_code' => $ffmpegReturnCode
- ];
- dLog('anime')->error('FFmpeg 合成音频失败', $logData);
- logDB('anime', 'error', 'FFmpeg 合成音频失败', $logData);
- $mergedAudioPath = null;
- } else {
- $logData = [
- 'audio_count' => count($audioFiles),
- 'output_file' => $mergedAudioPath,
- 'file_size' => filesize($mergedAudioPath)
- ];
- dLog('anime')->info('音频合成成功', $logData);
- logDB('anime', 'info', '音频合成成功', $logData);
- }
-
- // 清理临时音频文件和 concat 文件
- foreach ($audioFiles as $audioFile) {
- if (file_exists($audioFile)) {
- @unlink($audioFile);
- }
- }
- if (file_exists($concatFile)) {
- @unlink($concatFile);
- }
- }
- } else {
- dLog('anime')->warning('FFmpeg 未安装,跳过音频合成');
- logDB('anime', 'warning', 'FFmpeg 未安装,跳过音频合成', []);
- }
- } catch (\Exception $e) {
- $logData = [
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ];
- dLog('anime')->error('音频合成过程异常', $logData);
- logDB('anime', 'error', '音频合成过程异常', $logData);
- // 音频合成失败不影响图片导出
- $mergedAudioPath = null;
- }
-
- // 创建zip文件
- $zipFileName = sprintf(
- '%s.zip',
- $episode->title ?: '未命名'
- );
- $zipFilePath = storage_path('app/temp/' . $zipFileName);
-
- $zip = new \ZipArchive();
- if ($zip->open($zipFilePath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE) !== true) {
- // 清理临时文件
- array_map('unlink', $downloadedFiles);
- rmdir($tempDir);
- Utils::throwError('20003:创建ZIP文件失败');
- }
-
- // 添加图片文件到zip
- foreach ($downloadedFiles as $file) {
- $zip->addFile($file, basename($file));
- }
-
- // 添加合成的音频文件到zip
- if ($mergedAudioPath && file_exists($mergedAudioPath)) {
- $zip->addFile($mergedAudioPath, 'tts.wav');
- }
-
- $zip->close();
-
- // 清理临时图片文件
- array_map('unlink', $downloadedFiles);
-
- // 清理合成的音频文件
- if ($mergedAudioPath && file_exists($mergedAudioPath)) {
- @unlink($mergedAudioPath);
- }
-
- rmdir($tempDir);
-
- // 返回文件下载响应
- return response()->download($zipFilePath, $zipFileName)->deleteFileAfterSend(true);
-
- } catch (\Exception $e) {
- $logData = [
- 'anime_id' => $animeId,
- 'episode_id' => $episodeId,
- 'error' => $e->getMessage()
- ];
- dLog('anime')->error('导出分集图片失败', $logData);
- logDB('anime', 'error', '导出分集图片失败', $logData);
-
- // 清理可能存在的临时文件
- if (isset($tempDir) && file_exists($tempDir)) {
- array_map('unlink', glob("$tempDir/*"));
- rmdir($tempDir);
- }
- if (isset($zipFilePath) && file_exists($zipFilePath)) {
- unlink($zipFilePath);
- }
-
- throw $e;
- }
- }
- /**
- * 监控分镜图片和音频生成任务进度(SSE)
- */
- public function monitorSegmentTasks(Request $request) {
- // 忽略所有超时限制
- set_time_limit(0);
- ini_set('max_execution_time', '0');
- $data = $request->all();
-
- // 验证参数
- $validator = Validator::make($data, [
- 'anime_id' => 'required|string',
- 'episode_id' => 'required|string',
- ], [
- 'anime_id.required' => '动漫ID不能为空',
- 'episode_id.required' => '剧集ID不能为空',
- ]);
-
- if ($validator->fails()) {
- Utils::throwError('1002:' . $validator->errors()->first());
- }
-
- $animeId = $data['anime_id'];
- $episodeId = $data['episode_id'];
-
- // 查询没有pic_task_id的分镜个数
- $noPicTaskCount = DB::table('mp_episode_segments')
- ->where('anime_id', $animeId)
- ->where('episode_id', $episodeId)
- ->where(function($query) {
- $query->whereNull('pic_task_id')
- ->orWhere('pic_task_id', '');
- })
- ->count('id');
-
- // 获取所有分镜信息
- $segments = DB::table('mp_episode_segments')
- ->where('anime_id', $animeId)
- ->where('episode_id', $episodeId)
- ->where('pic_task_id', '!=', '')
- ->orderBy('segment_number')
- ->get();
- // 判断是否有未完成的任务
- $uncompleted_pic_count = DB::table('mp_episode_segments')->where('anime_id', $animeId)
- ->where('episode_id', $episodeId)->where('pic_task_id', '<>', '')->whereNotIn('pic_task_status', ['已完成', '失败'])->count('id');
-
- // 获取dialogue和voice_type存在但audio_url不存在的分镜个数
- $uncompleted_audio_count = DB::table('mp_episode_segments')
- ->where('anime_id', $animeId)
- ->where('episode_id', $episodeId)
- ->where('dialogue', '<>', '')
- ->whereNotNull('dialogue')
- ->where('voice_type', '<>', '')
- ->whereNotNull('voice_type')
- ->where(function($query) {
- $query->whereNull('audio_url')
- ->orWhere('audio_url', '');
- })
- ->count('id');
-
- if ($segments->isEmpty()) {
- Utils::throwError('20003:未找到分镜任务数据');
- }
-
- $startTime = time();
- $maxDuration = 900; // 15分钟超时
- $checkInterval = 5; // 每5秒检查一次
-
- // 设置 SSE 响应头并开始长连接
- return response()->stream(function () use ($segments, $animeId, $episodeId, $startTime, $maxDuration, $checkInterval, $uncompleted_pic_count, $uncompleted_audio_count, $noPicTaskCount) {
- if ($uncompleted_pic_count == 0 && $uncompleted_audio_count == 0) {
- $completedCount = DB::table('mp_episode_segments')->where('anime_id', $animeId)->where('episode_id', $episodeId)->where('pic_task_id', '<>', '')->where('pic_task_status', '已完成')->count('id');
- // 获取剧集信息之前先处理视频截止时间点
- $this->handleVideoTimePointEnd();
- // 获取剧集信息
- $episode = DB::table('mp_anime_episodes')
- ->where('anime_id', $animeId)
- ->where('id', $episodeId)
- ->select('id as episode_id', 'anime_id', 'episode_number', 'title', 'intro', 'art_style', 'roles', 'scenes')
- ->first();
-
- if ($episode) {
- $episode = (array)$episode;
- $episode['roles'] = json_decode($episode['roles'], true);
- $episode['scenes'] = json_decode($episode['scenes'], true);
-
- // 获取所有分镜信息
- $allSegments = DB::table('mp_episode_segments')
- ->where('anime_id', $animeId)
- ->where('episode_id', $episodeId)
- ->orderBy('segment_number')
- ->select('*')
- ->get()
- ->map(function ($value) {
- return (array)$value;
- })->toArray();
-
- // 按照episodeInfo的格式组织数据
- $segmentsStructure = $this->AnimeService->buildEpisodeSegmentsStructure($allSegments);
- $episode['acts'] = $segmentsStructure['acts'];
- $episode['total_duration'] = $segmentsStructure['total_duration'];
- }
- echo "data: " . json_encode([
- 'type' => 'completed',
- 'data' => [
- 'anime_id' => $animeId,
- 'episode_id' => $episodeId,
- 'completed_count' => $completedCount, // 成功完成的数量
- 'processed_count' => count($segments) + $noPicTaskCount, // 已处理完成的数量(成功+失败)+ 没有pic_task_id的分镜
- 'total_count' => count($segments) + $noPicTaskCount, // 总数包含没有pic_task_id的分镜
- 'total_elapsed_time' => time() - $startTime,
- 'episode_info' => $episode ?? null
- ]
- ]) . "\n\n";
- ob_flush();
- flush();
- return;
- }
- // 发送初始任务信息
- echo "data: " . json_encode([
- 'type' => 'init',
- 'data' => [
- 'anime_id' => $animeId,
- 'episode_id' => $episodeId,
- 'total_segments' => count($segments) + $noPicTaskCount, // 总数包含没有pic_task_id的分镜
- 'start_time' => $startTime,
- 'max_duration' => $maxDuration
- ]
- ]) . "\n\n";
- ob_flush();
- flush();
-
- $completedSegments = [];
- $processedSegments = []; // 记录已处理完成的分镜(成功或失败)
- $notifiedSegments = []; // 记录已通知过的分镜,避免重复通知
-
- while (time() - $startTime < $maxDuration) {
- try {
- $updatedImages = []; // 记录本次循环中更新的图片信息
-
- foreach ($segments as $segment) {
- $segmentId = $segment->segment_id;
- $picTaskId = $segment->pic_task_id;
- $audioUrl = $segment->audio_url;
-
- // 跳过已处理完成的分镜
- if (in_array($segmentId, $processedSegments)) {
- continue;
- }
-
- $picCompleted = false;
- $audioCompleted = false;
- $picProcessed = false;
- $audioProcessed = false;
-
- // 检查图片任务状态
- if ($picTaskId) {
- $picTask = DB::table('mp_generate_pic_tasks')
- ->where('id', $picTaskId)
- ->select('status', 'result_url', 'error_message')
- ->first();
-
- if ($picTask && in_array($picTask->status, ['success', 'failed'])) {
- $updateData = [
- 'pic_task_status' => $picTask->status === 'success' ? '已完成' : '失败',
- 'updated_at' => date('Y-m-d H:i:s')
- ];
-
- $imgUrl = '';
- if ($picTask->status === 'success' && !empty($picTask->result_url)) {
- // 更新分镜表的图片URL
- $resultUrls = json_decode($picTask->result_url, true);
- $imgUrl = is_array($resultUrls) ? $resultUrls[0] : $picTask->result_url;
- $updateData['img_url'] = $imgUrl;
- $picCompleted = true;
- } else if ($picTask->status === 'failed') {
- // 失败时清空图片URL
- $updateData['img_url'] = '';
- }
-
- DB::table('mp_episode_segments')
- ->where('segment_id', $segmentId)
- ->update($updateData);
-
- // 记录更新的图片信息(只有未通知过的才记录)
- if (!in_array($segmentId, $notifiedSegments)) {
- $updatedImages[] = [
- 'segment_id' => $segmentId,
- 'segment_number' => $segment->segment_number,
- 'img_url' => $imgUrl,
- 'status' => $picTask->status === 'success' ? 'success' : 'failed'
- ];
-
- // 标记为已通知
- $notifiedSegments[] = $segmentId;
- }
-
- $picProcessed = true;
- }
- }
-
- // 检查音频状态
- // 获取分镜的dialogue、voice_type和audio_url信息
- $segmentInfo = DB::table('mp_episode_segments')
- ->where('segment_id', $segmentId)
- ->select('dialogue', 'voice_type', 'audio_url')
- ->first();
-
- $needAudio = false;
- $currentAudioUrl = '';
-
- if ($segmentInfo) {
- $dialogue = trim((string)getProp($segmentInfo, 'dialogue'));
- $voiceType = trim((string)getProp($segmentInfo, 'voice_type'));
- $currentAudioUrl = trim((string)getProp($segmentInfo, 'audio_url'));
-
- // 如果有对话和音色类型,则需要音频
- if (!empty($dialogue) && !empty($voiceType)) {
- $needAudio = true;
- }
- }
-
- // 判断音频状态
- if ($needAudio) {
- // 需要音频的情况下,检查audio_url是否有值
- if (!empty($currentAudioUrl)) {
- $audioCompleted = true;
- $audioProcessed = true;
- } else {
- // 需要音频但还没有,继续等待
- $audioCompleted = false;
- $audioProcessed = false;
- }
- } else {
- // 不需要音频,直接标记为已处理
- $audioCompleted = false;
- $audioProcessed = true;
- }
-
- // 如果图片和音频都成功完成,标记为已完成
- if ($picCompleted && ($audioCompleted || !$needAudio)) {
- $completedSegments[] = $segmentId;
- }
-
- // 如果图片和音频都已处理完成(成功或失败),标记为已处理
- if ($picProcessed && $audioProcessed) {
- $processedSegments[] = $segmentId;
- }
-
- }
-
- // 如果有图片更新,发送具体的图片更新信息
- if (!empty($updatedImages)) {
- echo "data: " . json_encode([
- 'type' => 'update',
- 'data' => [
- 'anime_id' => $animeId,
- 'episode_id' => $episodeId,
- 'updated_images' => $updatedImages,
- 'completed_count' => count($completedSegments), // 成功完成的数量
- 'processed_count' => count($processedSegments) + $noPicTaskCount, // 已处理完成的数量(成功+失败)+ 没有pic_task_id的分镜
- 'total_count' => count($segments) + $noPicTaskCount, // 总数包含没有pic_task_id的分镜
- 'elapsed_time' => time() - $startTime,
- 'remaining_time' => max(0, $maxDuration - (time() - $startTime))
- ]
- ]) . "\n\n";
- ob_flush();
- flush();
- }
-
- // 如果所有分镜都已处理完成(成功或失败),获取最终结果并结束连接
- if (count($processedSegments) === count($segments)) {
- // 获取剧集信息之前先处理视频截止时间点
- $this->handleVideoTimePointEnd();
- // 获取剧集信息
- $episode = DB::table('mp_anime_episodes')
- ->where('anime_id', $animeId)
- ->where('id', $episodeId)
- ->select('id as episode_id', 'anime_id', 'episode_number', 'title', 'intro', 'art_style', 'roles', 'scenes')
- ->first();
-
- if ($episode) {
- $episode = (array)$episode;
- $episode['roles'] = json_decode($episode['roles'], true);
- $episode['scenes'] = json_decode($episode['scenes'], true);
-
- // 获取所有分镜信息
- $allSegments = DB::table('mp_episode_segments')
- ->where('anime_id', $animeId)
- ->where('episode_id', $episodeId)
- ->orderBy('segment_number')
- ->select('*')
- ->get()
- ->map(function ($value) {
- return (array)$value;
- })->toArray();
-
- // 按照episodeInfo的格式组织数据
- $segmentsStructure = $this->AnimeService->buildEpisodeSegmentsStructure($allSegments);
- $episode['acts'] = $segmentsStructure['acts'];
- $episode['total_duration'] = $segmentsStructure['total_duration'];
- }
-
- echo "data: " . json_encode([
- 'type' => 'completed',
- 'data' => [
- 'anime_id' => $animeId,
- 'episode_id' => $episodeId,
- 'completed_count' => count($completedSegments), // 成功完成的数量
- 'processed_count' => count($processedSegments) + $noPicTaskCount, // 已处理完成的数量(成功+失败)+ 没有pic_task_id的分镜
- 'total_count' => count($segments) + $noPicTaskCount, // 总数包含没有pic_task_id的分镜
- 'total_elapsed_time' => time() - $startTime,
- 'episode_info' => $episode ?? null
- ]
- ]) . "\n\n";
- ob_flush();
- flush();
- break;
- }
-
- // 检查是否超时
- if (time() - $startTime >= $maxDuration) {
- break;
- }
-
- sleep($checkInterval);
-
- } catch (\Exception $e) {
- echo "data: " . json_encode([
- 'type' => 'error',
- 'message' => '查询任务状态失败: ' . $e->getMessage(),
- 'elapsed_time' => time() - $startTime,
- 'remaining_time' => max(0, $maxDuration - (time() - $startTime))
- ]) . "\n\n";
- ob_flush();
- flush();
-
- if (time() - $startTime >= $maxDuration) {
- break;
- }
-
- sleep($checkInterval);
- }
- }
-
- // 超时处理
- if (time() - $startTime >= $maxDuration) {
- echo "data: " . json_encode([
- 'type' => 'time_out',
- 'message' => '分镜任务监控已超时15分钟,停止状态查询',
- 'data' => [
- 'anime_id' => $animeId,
- 'episode_id' => $episodeId,
- 'completed_count' => count($completedSegments), // 成功完成的数量
- 'processed_count' => count($processedSegments) + $noPicTaskCount, // 已处理完成的数量(成功+失败)+ 没有pic_task_id的分镜
- 'total_count' => count($segments) + $noPicTaskCount, // 总数包含没有pic_task_id的分镜
- 'total_elapsed_time' => time() - $startTime,
- 'timeout_duration' => $maxDuration
- ]
- ]) . "\n\n";
- ob_flush();
- flush();
- }
-
- }, 200, [
- 'Content-Type' => 'text/event-stream',
- 'Cache-Control' => 'no-cache',
- 'Connection' => 'keep-alive',
- 'X-Accel-Buffering' => 'no', // 禁用 Nginx 缓冲
- ]);
- }
- /**
- * 音频试听接口
- * 创建音频任务并返回结果
- */
- public function previewAudio(Request $request) {
- $data = $request->all();
- $result = $this->AnimeService->previewAudio($data);
- return $this->success($result);
- }
- private function handleVideoTimePointEnd() {
- // 处理音频时长,循环查询dub_video_task_queue列表中的任务
- $queueLength = Redis::llen('dub_video_task_queue');
- if ($queueLength > 0) {
- $processedTaskIds = []; // 记录已处理的任务ID
-
- // 遍历队列中的所有任务
- for ($i = 0; $i < $queueLength; $i++) {
- $dubTaskId = Redis::lindex('dub_video_task_queue', $i);
- if (!$dubTaskId) continue;
-
- // 查询任务状态
- $dubTask = DB::table('mp_dub_video_tasks')
- ->where('id', $dubTaskId)
- ->first();
-
- if (!$dubTask) {
- // 任务不存在,标记为需要删除
- $processedTaskIds[] = $dubTaskId;
- continue;
- }
-
- // 如果任务状态是已完成
- if ($dubTask->generate_status === '执行成功') {
- // 获取对应的分镜信息
- $segment = DB::table('mp_episode_segments')
- ->where('segment_id', $dubTask->alias_segment_id)
- ->first();
-
- // 如果分镜存在且audio_duration大于0
- if ($segment && isset($segment->audio_duration) && $segment->audio_duration > 0) {
- // 更新video_time_point_end字段
- DB::table('mp_episode_segments')
- ->where('segment_id', $dubTask->alias_segment_id)
- ->update([
- 'video_time_point_end' => $segment->audio_duration,
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
-
- dLog('anime')->info('更新分镜视频截止时间点', [
- 'task_id' => $dubTaskId,
- 'segment_id' => $dubTask->alias_segment_id,
- 'audio_duration' => $segment->audio_duration
- ]);
- }
-
- // 标记为需要从队列中删除
- $processedTaskIds[] = $dubTaskId;
- }
- }
-
- // 从Redis列表中删除已处理的任务ID
- foreach ($processedTaskIds as $taskId) {
- Redis::lrem('dub_video_task_queue', 0, $taskId);
- }
- }
- }
- }
|