ActivityMakeMediaJob.php
8.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
namespace App\Jobs;
use App\Enums\ActivityStatusEnum;
use App\Helpers\UploadHelper;
use App\Models\Activity;
use Carbon\Carbon;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use OSS\OssClient;
use ProtoneMedia\LaravelFFMpeg\Support\FFMpeg;
class ActivityMakeMediaJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public Activity $activity;
public int $toStatus;
public bool $buildMedia;
public int $timeout = 20 * 60;
public int $tries = 3;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Activity $activity, ActivityStatusEnum $toStatus, bool $buildMedia = false)
{
$this->activity = $activity;
$this->toStatus = $toStatus->value;
$this->buildMedia = $buildMedia;
}
/**
* 获取应该分配给任务的标记
*
* @return array<int, string>
*/
public function tags(): array
{
return ['activity', 'make-media:' . $this->activity->getKey()];
}
/**
* @param string $key
* @return string
*/
protected function getSourceUrl(string $key): string
{
return Arr::get($this->activity, 'expand.' . $key . '.url', '');
}
/**
* Execute the job.
*
* @return void
* @throws \OSS\Core\OssException
*/
public function handle(): void
{
try {
DB::beginTransaction();
$this->backup();
$attribute = ['status' => $this->toStatus];
$clipTime = '';
if ($this->activity->getAttribute('is_lyric') === 1 && $this->activity->getAttribute('lyric') && $this->activity->getAttribute('clip_lyric')) {
$clipTime = $this->getClipTime($this->activity->getAttribute('lyric'), $this->activity->getAttribute('clip_lyric'));
}
if ($this->getSourceUrl('guide_source')) {
$guideName = Str::uuid()->toString();
$guide = $this->downloadSourceFile('guide_source', $guideName);
$attribute['guide'] = $this->uploadOss($guide);
$attribute['guide_duration'] = $this->getFileDurationInSecond($guide);
if (!empty($clipTime)) {
$guideClip = $this->clipAudio($guide, $guideName, ...$clipTime);
$attribute['guide_clip'] = $this->uploadOss($guideClip);
$attribute['guide_clip_duration'] = $this->getFileDurationInSecond($guideClip);
}
}
if ($this->getSourceUrl('karaoke_source')) {
$karaokeName = Str::uuid()->toString();
$karaoke = $this->downloadSourceFile('karaoke_source', $karaokeName);
$attribute['karaoke'] = $this->uploadOss($karaoke);
if (!empty($clipTime)) {
$karaokeClip = $this->clipAudio($karaoke, $karaokeName, ...$clipTime);
$attribute['karaoke_clip'] = $this->uploadOss($karaokeClip);
}
}
Activity::query()->whereKey($this->activity->getKey())->update($attribute);
DB::commit();
} catch (Exception $exception) {
DB::rollBack();
Log::error(__CLASS__, ['activity_id' => $this->activity->getKey(), 'message' => $exception->getMessage()]);
Activity::query()->whereKey($this->activity->getKey())->update(['status' => 4]);
}
FFMpeg::cleanupTemporaryFiles();
Storage::disk('make')->deleteDirectory($this->activity->getKey());
}
/**
* @param string $key
* @param string $name
* @return string
*/
protected function downloadSourceFile(string $key, string $name): string
{
$path = $this->activity->getKey() . '/' . $name . '.m4a';
$url = UploadHelper::toEndpointUrl($this->getSourceUrl($key), UploadHelper::isInternalServer());
FFMpeg::openUrl($url)->export()->addFilter(['-ar', '44100', '-c:a', 'libfdk_aac', '-vn'])->toDisk('make')->save($path);
return $path;
}
/**
* @param string $sourcePath
* @param string $name
* @param string|null $start
* @param string|null $end
* @return string
*/
protected function clipAudio(string $sourcePath, string $name, ?string $start, ?string $end): string
{
$path = $this->activity->getKey() . '/' . 'clip_' . $name . '.m4a';
$tmpPath = $this->activity->getKey() . '/' . Str::uuid()->toString() . '.m4a';
$clipStart = $this->durationToSecond($start);
$clipEnd = is_null($end) ? [] : ['-t', ($this->durationToSecond($end) - (max($clipStart - 5, 0)))];
$clipStart = $clipStart - 5 <= 0 ? '00:00:00.000' : $this->secondToDuration($clipStart - 5);
FFMpeg::fromDisk('make')->open($sourcePath)->export()->addFilter(array_filter(['-ss', $clipStart, ...$clipEnd, '-acodec', 'copy']))->toDisk('make')->save($tmpPath);
FFMpeg::fromDisk('make')->open($tmpPath)->export()->addFilter(['-ar', '44100', '-c:a', 'libfdk_aac', '-vn'])->toDisk('make')->save($path);
return $path;
}
/**
* @param string $lyric
* @param string $clipLyric
* @return array
*/
protected function getClipTime(string $lyric, string $clipLyric): array
{
$preg = '/\[\d*:\d*\.\d*]/';
preg_match_all($preg, $lyric, $lyricArr);
preg_match_all($preg, $clipLyric, $clipLyricArr);
$lyricArr = array_shift($lyricArr);
$clipLyricArr = array_shift($clipLyricArr);
$clipLyricEnd = end($clipLyricArr);
$sourceIndex = array_search($clipLyricEnd, $lyricArr, true);
$startTime = str_replace(['[', ']'], '', array_shift($clipLyricArr));
if ($sourceIndex && isset($lyricArr[$sourceIndex + 1])) {
$endTime = str_replace(['[', ']'], '', $lyricArr[$sourceIndex + 1]);
} else {
$endTime = NULL;
}
return [$startTime, $endTime];
}
/**
* @param $path
* @return string
*/
public function getMakeFilePath($path): string
{
return Storage::disk('make')->path($path);
}
/**
* @param string $path
* @return int
*/
public function getFileDurationInSecond(string $path): int
{
return FFMpeg::fromDisk('make')->open($path)->getDurationInSeconds();
}
/**
* @param string|null $duration
* @return float
*/
protected function durationToSecond(?string $duration): float
{
$time = date_parse($this->durationFormat($duration));
return ((int)$time['hour'] * 3600) + ((int)$time['minute'] * 60) + (int)$time['second'] + (float)$time['fraction'];
}
/**
* @param float $second
* @return string
*/
protected function secondToDuration(float $second): string
{
$arr = explode('.', $second);
if (count($arr) === 1) {
$arr[] = 0;
}
return gmdate('H:i:s', $arr[0]) . '.' . end($arr);
}
/**
* @param string|null $duration
* @return string
*/
protected function durationFormat(?string $duration): string
{
if (is_null($duration)) {
return '00:00:00.000';
}
$arr = explode(':', $duration);
if (count($arr) === 1) {
array_unshift($arr, '00');
}
if (count($arr) === 2) {
array_unshift($arr, '00');
}
return implode(':', $arr);
}
/**
* @param $file
* @return string
* @throws \OSS\Core\OssException
*/
protected function uploadOss($file): string
{
return UploadHelper::put(
'make/' . now()->format('Ymd') . '/' . Str::afterLast($file, '/'),
$this->getMakeFilePath($file),
[OssClient::OSS_CONTENT_TYPE => 'audio/mp4']
);
}
/**
* @return bool
*/
protected function backup(): bool
{
$guide = $this->activity->getAttribute('guide');
$karaoke = $this->activity->getAttribute('karaoke');
if ($guide && $karaoke) {
DB::table('activity_audios')->insert([
'activity_id' => $this->activity->getKey(),
'guide' => $this->activity->getAttribute('guide'),
'karaoke' => $this->activity->getAttribute('karaoke'),
'created_at' => Carbon::now()->toDateTimeString()
]);
}
return true;
}
}