Converting video with php-ffmpeg
Hello all, So am using php-ffmpeg/php-ffmpeg 1.1 version to convert video. my goal is that when i select 1080p video it must convert to, 1080p, 720p and 480p. it must return 3 videos. But I have a strange problem, When it starts converting the first video to 1080p, it uploads like 80mb (I think it must be 80mb ), and when it finishes on 80 mb it overwrites 1080p video again. It doesn't continue converting other resolutions ( 720p and 480p ) I think the problem is here
$video->addFilter($resizeFilter)->save($format['format'], $outputPath);
it does not save when it's converted. It starts overwritten, in one request...
this is my File Model function:
public function convertToResolution($filePath, $formats, $iDontUseThis)
{
if (count($formats) === 0) {
return;
}
$format = $formats[0];
$randomVideoName = uniqid();
$outputPath = storage_path("app/videos/{$randomVideoName}-{$format['resolution']}.mp4");
$resizeFilter = new ResizeFilter($format['dimension']);
$ffmpeg = FFMpeg::create();
$video = $ffmpeg->open($filePath);
$video->addFilter($resizeFilter)->save($format['format'], $outputPath);
$eventClass = match ($format['resolution']) {
'1080p' => VideoConvertedTo1080p::class,
'720p' => VideoConvertedTo720p::class,
'480p' => VideoConvertedTo480p::class,
};
$event = new $eventClass($outputPath);
Event::dispatch($event);
// Sleep for 10 seconds to add a delay between conversions
sleep(10);
$this->convertToResolution($filePath, array_slice($formats, 1), $randomVideoName);
}
and my controller method:
public function convertVideo(Request $request)
{
// Get the uploaded file from the request
$file = $request->file('video');
$randomVideoName = Str::uuid() . '-' . $file->getClientOriginalName();
// Check if the file already exists
$existingFile480p = storage_path("app/videos/{$randomVideoName}-480p.mp4");
$existingFile720p = storage_path("app/videos/{$randomVideoName}-720p.mp4");
$existingFile1080p = storage_path("app/videos/{$randomVideoName}-1080p.mp4");
if (file_exists($existingFile480p) && file_exists($existingFile720p) && file_exists($existingFile1080p)) {
return response()->json(['message' => 'Video has already been processed and saved.']);
}
// Store the uploaded video file and get its path
$filePath = $file->storeAs('temp', $randomVideoName, 'local');
$filePath = storage_path('app/' . $filePath);
$ffmpeg = FFMpeg::create();
$formats = [
['resolution' => '1080p', 'format' => new X264('aac', 'libx264', 'mp4'), 'event' => 'video.converted.1080p', 'dimension' => new Dimension(1920, 1080)],
['resolution' => '720p', 'format' => new X264('aac', 'libx264', 'mp4'), 'event' => 'video.converted.720p', 'dimension' => new Dimension(1280, 720)],
['resolution' => '480p', 'format' => new X264('aac', 'libx264', 'mp4'), 'event' => 'video.converted.480p', 'dimension' => new Dimension(854, 480)],
];
$instance = new File();
$instance->convertToResolution($filePath, $formats, $randomVideoName);
}
I changed many times my function because it is not using the "$iDontUseThis" variable.
Am like struggling with this problem for 4 days... Please if someone can help me with this...
Please or to participate in this conversation.