1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-25 04:07:17 +08:00

优化VodEventTask

This commit is contained in:
koogua 2022-06-09 10:26:02 +08:00
parent 582c6bdb58
commit 8a8da8fa35

View File

@ -25,19 +25,25 @@ class VodEventTask extends Task
foreach ($events as $event) { foreach ($events as $event) {
$handles[] = $event['EventHandle']; $result = true;
if ($event['EventType'] == 'NewFileUpload') { if ($event['EventType'] == 'NewFileUpload') {
$this->handleNewFileUploadEvent($event); $result = $this->handleNewFileUploadEvent($event);
} elseif ($event['EventType'] == 'ProcedureStateChanged') { } elseif ($event['EventType'] == 'ProcedureStateChanged') {
$this->handleProcedureStateChangedEvent($event); $result = $this->handleProcedureStateChangedEvent($event);
} elseif ($event['EventType'] == 'FileDeleted') { } elseif ($event['EventType'] == 'FileDeleted') {
$this->handleFileDeletedEvent($event); $result = $this->handleFileDeletedEvent($event);
}
if ($result) {
$handles[] = $event['EventHandle'];
} }
} }
if (count($handles) > 0) {
$this->confirmEvents($handles); $this->confirmEvents($handles);
} }
}
protected function handleNewFileUploadEvent($event) protected function handleNewFileUploadEvent($event)
{ {
@ -46,13 +52,13 @@ class VodEventTask extends Task
$height = $event['FileUploadEvent']['MetaData']['Width'] ?? 0; $height = $event['FileUploadEvent']['MetaData']['Width'] ?? 0;
$duration = $event['FileUploadEvent']['MetaData']['Duration'] ?? 0; $duration = $event['FileUploadEvent']['MetaData']['Duration'] ?? 0;
if ($fileId == 0) return; if ($fileId == 0) return false;
$chapterRepo = new ChapterRepo(); $chapterRepo = new ChapterRepo();
$chapter = $chapterRepo->findByFileId($fileId); $chapter = $chapterRepo->findByFileId($fileId);
if (!$chapter) return; if (!$chapter) return false;
$attrs = $chapter->attrs; $attrs = $chapter->attrs;
@ -63,43 +69,43 @@ class VodEventTask extends Task
$duration = $this->getFileDuration($fileId); $duration = $this->getFileDuration($fileId);
} }
/** $isVideo = $width > 0 && $height > 0;
* 获取不到时长视为失败
*/
if ($duration == 0) {
$attrs['file']['status'] = ChapterModel::FS_FAILED;
$attrs['duration'] = 0;
$chapter->update(['attrs' => $attrs]);
return;
}
$vodService = new VodService(); $vodService = new VodService();
if ($width == 0 && $height == 0) { if ($duration > 0) {
$vodService->createTransAudioTask($fileId); if ($isVideo) {
} else {
$vodService->createTransVideoTask($fileId); $vodService->createTransVideoTask($fileId);
} else {
$vodService->createTransAudioTask($fileId);
}
$attrs['file']['status'] = ChapterModel::FS_TRANSLATING;
} else {
$attrs['file']['status'] = ChapterModel::FS_FAILED;
} }
$attrs['file']['status'] = ChapterModel::FS_TRANSLATING;
$attrs['duration'] = (int)$duration; $attrs['duration'] = (int)$duration;
$chapter->update(['attrs' => $attrs]); $chapter->attrs = $attrs;
$chapter->update();
$this->updateCourseVodAttrs($chapter->course_id); $this->updateCourseVodAttrs($chapter->course_id);
return true;
} }
protected function handleProcedureStateChangedEvent($event) protected function handleProcedureStateChangedEvent($event)
{ {
$fileId = $event['ProcedureStateChangeEvent']['FileId'] ?? 0; $fileId = $event['ProcedureStateChangeEvent']['FileId'] ?? 0;
if ($fileId == 0) return; if ($fileId == 0) return false;
$chapterRepo = new ChapterRepo(); $chapterRepo = new ChapterRepo();
$chapter = $chapterRepo->findByFileId($fileId); $chapter = $chapterRepo->findByFileId($fileId);
if (!$chapter) return; if (!$chapter) return false;
$attrs = $chapter->attrs; $attrs = $chapter->attrs;
@ -110,19 +116,11 @@ class VodEventTask extends Task
$attrs['duration'] = $this->getFileDuration($fileId); $attrs['duration'] = $this->getFileDuration($fileId);
} }
$processResult = $event['ProcedureStateChangeEvent']['MediaProcessResultSet'] ?? [];
/**
* 获取不到处理结果视为失败
*/
if (empty($processResult)) {
$attrs['file']['status'] = ChapterModel::FS_FAILED;
$chapter->update(['attrs' => $attrs]);
return;
}
$failCount = $successCount = 0; $failCount = $successCount = 0;
$processResult = $event['ProcedureStateChangeEvent']['MediaProcessResultSet'] ?? [];
if ($processResult) {
foreach ($processResult as $item) { foreach ($processResult as $item) {
if ($item['Type'] == 'Transcode') { if ($item['Type'] == 'Transcode') {
if ($item['TranscodeTask']['Status'] == 'SUCCESS') { if ($item['TranscodeTask']['Status'] == 'SUCCESS') {
@ -132,9 +130,14 @@ class VodEventTask extends Task
} }
} }
} }
}
$fileStatus = ChapterModel::FS_TRANSLATING; $fileStatus = ChapterModel::FS_TRANSLATING;
if (!$processResult) {
$fileStatus = ChapterModel::FS_FAILED;
}
/** /**
* 当有一个成功标记为成功 * 当有一个成功标记为成功
*/ */
@ -144,17 +147,21 @@ class VodEventTask extends Task
$fileStatus = ChapterModel::FS_FAILED; $fileStatus = ChapterModel::FS_FAILED;
} }
if ($fileStatus == ChapterModel::FS_TRANSLATING) return;
$attrs['file']['id'] = $fileId; $attrs['file']['id'] = $fileId;
$attrs['file']['status'] = $fileStatus; $attrs['file']['status'] = $fileStatus;
$chapter->update(['attrs' => $attrs]); $chapter->attrs = $attrs;
$chapter->update();
$this->updateCourseVodAttrs($chapter->course_id);
return true;
} }
protected function handleFileDeletedEvent($event) protected function handleFileDeletedEvent($event)
{ {
return true;
} }
protected function pullEvents() protected function pullEvents()