mirror of
https://github.com/Bigherollc/wticreatorstudio.git
synced 2026-01-16 19:05:08 -05:00
229 lines
8.2 KiB
PHP
229 lines
8.2 KiB
PHP
<?php
|
|
|
|
namespace Modules\StudioPlus\Services;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Modules\StudioPlus\Entities\StudioPlusVideo;
|
|
use \FFMpeg\FFMpeg;
|
|
use \FFMpeg\FFProbe;
|
|
use Illuminate\Support\Str;
|
|
use Modules\StudioPlus\Jobs\ProcessVideo;
|
|
use Modules\StudioPlus\Jobs\ReuploadVideo;
|
|
|
|
class VideoProcessingService
|
|
{
|
|
protected $watermark_service;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->watermark_service = new WatermarkService();
|
|
}
|
|
|
|
public function reprocess($id)
|
|
{
|
|
Log::info('Searching the video with id ' . $id);
|
|
$video = StudioPlusVideo::find($id);
|
|
|
|
if ($video) {
|
|
Log::info('Video found');
|
|
$reprocess = true;
|
|
|
|
$meta = $video->meta()->where('name', 'is_old')->first();
|
|
|
|
if ($meta && $meta->value === 'true') {
|
|
$reprocess = false;
|
|
}
|
|
|
|
if ($reprocess) {
|
|
if (str_contains($video->video_url, 'temp')) {
|
|
$temp_path = $this->getTempUploadStoragePath($id);
|
|
|
|
if ($temp_path) {
|
|
if (Storage::exists("$temp_path/video.mp4")) {
|
|
Log::info('Uploaded video found');
|
|
Log::info('Dispatching reprocess video job');
|
|
ProcessVideo::dispatch($id);
|
|
Log::info('Reprocess video job dispatched');
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
$this->reprocessCloud($id);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public function reprocessCloud($id)
|
|
{
|
|
Log::info('Searching the video with id ' . $id);
|
|
$video = StudioPlusVideo::find($id);
|
|
$studio_plus_video = $video;
|
|
|
|
if ($video) {
|
|
Log::info('Video found');
|
|
|
|
$video->upload_status = StudioPlusVideo::UPLOAD_STATUS_PROCESSING;
|
|
$video->update();
|
|
|
|
$temp_path = $this->getTempLocalStoragePath($id);
|
|
Storage::deleteDirectory($temp_path);
|
|
Storage::put("$temp_path/reprocessing.dat", $id);
|
|
|
|
if ($video->is_short) {
|
|
Storage::put("$temp_path/short.dat", 'Short Video');
|
|
}
|
|
|
|
Log::info('Downloading video to temp path');
|
|
$file = (new GCPService)->download($video, storage_path("app/$temp_path"));
|
|
Log::info('Copying file');
|
|
Storage::copy("$temp_path/{$video->filename}", "$temp_path/video.mp4");
|
|
|
|
$globalSetting = $video->globalSetting;
|
|
|
|
/*if ($globalSetting) {
|
|
$introLocalPath = storage_path('app/public/' . $globalSetting->details['intro']);
|
|
$outroLocalPath = storage_path('app/public/' . $globalSetting->details['outro']);
|
|
$outroOutputPath = storage_path("app/$temp_path/outro.mp4");
|
|
|
|
Storage::delete("$temp_path/outro.mp4");
|
|
VideoService::copyAndRescale($outroLocalPath, $outroOutputPath);
|
|
|
|
Log::info('copying outro');
|
|
Log::info($introLocalPath);
|
|
Log::info($outroLocalPath);
|
|
}
|
|
else {
|
|
$introLocalPath = module_path('StudioPlus') . "/Storage/default.ts";
|
|
Log::info('no global setting');
|
|
}
|
|
|
|
$introOutputPath = storage_path("app/$temp_path/intro.mp4");
|
|
|
|
Storage::delete("$temp_path/intro.mp4");
|
|
VideoService::copyAndRescale($introLocalPath, $introOutputPath);*/
|
|
|
|
$video = storage_path("app/$temp_path/video.mp4");
|
|
//$outroOutputPath = isset($outroOutputPath) ? $outroOutputPath : $introOutputPath;
|
|
|
|
$resultPath = storage_path("app/$temp_path/result.mp4");
|
|
$result_path = $resultPath;
|
|
Storage::delete("$temp_path/result.mp4");
|
|
|
|
try {
|
|
//Storage::copy("$temp_path/video.mp4", "$temp_path/result.mp4");
|
|
|
|
$is_short = Storage::exists("$temp_path/short.dat");
|
|
|
|
/*if ($is_short) {
|
|
Storage::copy("$temp_path/video.mp4", "$temp_path/result.mp4");
|
|
}
|
|
else {
|
|
$this->concat(
|
|
$introOutputPath,
|
|
$video,
|
|
$resultPath,
|
|
$outroOutputPath
|
|
);
|
|
}*/
|
|
|
|
if ($is_short) {
|
|
Storage::copy("$temp_path/video.mp4", "$temp_path/result.mp4");
|
|
}
|
|
else {
|
|
$this->watermark_service->addWatermark($studio_plus_video, $result_path, true);
|
|
}
|
|
}
|
|
catch(\Exception $ex) {
|
|
Log::info($ex);
|
|
}
|
|
|
|
Storage::put("$temp_path/processed.dat", 'Added intro and outro videos.');
|
|
|
|
ReuploadVideo::dispatch($id);
|
|
}
|
|
}
|
|
|
|
public function store($id)
|
|
{
|
|
$video = StudioPlusVideo::find($id);
|
|
|
|
if ($video) {
|
|
$gcp = new GoogleCloudPlatform();
|
|
|
|
$temp_path = $this->getTempLocalStoragePath($id);
|
|
|
|
$is_processed = Storage::exists("$temp_path/processed.dat");
|
|
$is_stored = Storage::exists("$temp_path/stored.dat");
|
|
|
|
if ($is_processed && !$is_stored) {
|
|
$original_video_path = storage_path("app/$temp_path/video.mp4");
|
|
|
|
$ffprobe = FFProbe::create();
|
|
$code_ac = $ffprobe->format($original_video_path)->get('duration');
|
|
|
|
$result_path = storage_path("app/$temp_path/result.mp4");
|
|
$filename = 'P' . Carbon::now()->format('YmdHis') . Str::random() . ".mp4";
|
|
$object_name = $gcp->getGoogleStoragePath($video) . "/$filename";
|
|
$gcp->uploadFileToGCP($result_path, $object_name);
|
|
|
|
Log::info('New video url: '. $object_name);
|
|
|
|
$video->update([
|
|
'video_url' => $object_name,
|
|
'video_duration' => round($code_ac, PHP_ROUND_HALF_UP),
|
|
'upload_status' => StudioPlusVideo::UPLOAD_STATUS_COMPLETE
|
|
]);
|
|
|
|
Storage::put("$temp_path/stored.dat", 'Video has been uploaded.');
|
|
|
|
Storage::deleteDirectory($temp_path);
|
|
|
|
return true;
|
|
}
|
|
else {
|
|
sleep(3);
|
|
ReuploadVideo::dispatch($id);
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function getUnprocessedVideos()
|
|
{
|
|
$videos = StudioPlusVideo::where('upload_status', StudioPlusVideo::UPLOAD_STATUS_PROCESSING)->latest('id')->take(10)->get();
|
|
|
|
return $videos;
|
|
}
|
|
|
|
public function concat(string $intro, string $pathToVideo, string $pathToOutput, string $outro = ''): void
|
|
{
|
|
$newOutro = $outro ?: $intro;
|
|
`ffmpeg -i "$intro" -i "$pathToVideo" -i "$newOutro" -filter_complex "[0:v]scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:-1:-1,setsar=1,fps=30,format=yuv420p[v0];[1:v]scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:-1:-1,setsar=1,fps=30,format=yuv420p[v1];[2:v]scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:-1:-1,setsar=1,fps=30,format=yuv420p[v2];[v0][0:a][v1][1:a][v2][2:a]concat=n=3:v=1:a=1[v][a]" -map "[v]" -map "[a]" -c:v libx264 -c:a aac -movflags +faststart "$pathToOutput"`;
|
|
}
|
|
|
|
public function getTempLocalStoragePath($id)
|
|
{
|
|
return "CreatorStudio/temp/reprocess/videos/$id";
|
|
}
|
|
|
|
public function getTempUploadStoragePath($id)
|
|
{
|
|
$video = StudioPlusVideo::find($id);
|
|
|
|
if ($video) {
|
|
return "CreatorStudio/temp/upload/$id";
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function watermarked($video_path, $watermark_path, $output_video_path)
|
|
{
|
|
`ffmpeg -i "$video_path" -i "$watermark_path" -filter_complex "[1][0]scale2ref=oh*mdar:ih*0.2[logo][video];[video][logo]overlay:enable=between(t\,0\,3)" "$output_video_path"`;
|
|
}
|
|
}
|