Generating an image from uploaded video from panel

So I have this panel here:

When the user uploads a file it must be renamed to a certain pattern like “bauhaus_art_and_design_cloud_001, 002 003…”

But when the file is a video, it must autogenerate and image to be used on HTML as a poster image while the video loads.

I make some code on site/config/config.php to solve my this issue but it still needs the autogenerated image to be binded to the video file, then when the video file gets renamed the image binded should be renamed as well.

I’ll show you the code from site/config/config.php

<?php
@include_once __DIR__ . '/vendor/autoload.php';

use Intervention\Image\ImageManager;
use FFMpeg\FFMpeg;
use FFMpeg\Coordinate\TimeCode;



return [
    'hooks' => [
        'file.create:before' => function (Kirby\Cms\File $file, Kirby\Filesystem\File $upload) {
            // Check if file is image type            
            if ($file->type() === 'image') {
                // Full path of the file
                $filePath = $upload->realpath();
                try {
                    // Loads image using Intervention Image
                    $imageManager = new ImageManager();
                    $image = $imageManager->make($filePath);

                    // Save the image
                    $image->save($filePath, 10);
                } catch (Exception $e) {
                    throw new Exception('There was an error compressing the image: ' . $e->getMessage());
                }
            }
        },
        'file.create:after' => function (Kirby\Cms\File $file) {
            $page = $file->page();
            error_log('studyprogram:> ' . $page->studyprogram()->text());
            $lastNumber = 0;
            // increment the last number by one and pad it with zeros
            if ($file->type() == 'image') {
                $lastNumber = $page->images()->count();
            } else if ($file->type() == 'video') {
                $lastNumber = $page->videos()->count();
            }

            $number = str_pad($lastNumber, 3, '0', STR_PAD_LEFT);
            $newName = 'bauhaus_art_design_cloud_' . $number;
            // rename the file and the txt
            $file->changeName($newName);
            if ($file->type() == 'video') {
                $ffmpeg = FFMpeg::create([
                    'ffmpeg.binaries' => 'C:/ffmpeg/bin/ffmpeg.exe',
                    'ffprobe.binaries' => 'C:/ffmpeg/bin/ffprobe.exe',
                ]);

                // Get the directory path
                $directory = dirname($file->root());                

                // Change the file name
                $newFileName = $newName;

                // Create the new file path
                $newFilePath = $directory . '/' . $newFileName;

                $video = $ffmpeg->open($newFilePath . '.' . $file->extension());
                $frame = $video->frame(TimeCode::fromSeconds(1));                

                $posterPath = $newFilePath . '.jpg';  // Path to save the poster image
               
                error_log('POSTERPATH:> ' . $posterPath);
                $frame->save($posterPath);

                // Create a new file for the poster image
                $poster = $page->files()->create([
                    'parent' => $page,
                    'source' => $posterPath,
                    'filename' => $newFileName . '.jpg',
                    'template' => 'poster',
                ]);

                // Attach the poster file to the video file
                $new = $file->update([
                    'image' => $poster,
                ]);
            }
        },
        'file.changeName:before' => function (Kirby\Cms\File $file, string $name) {
            try {
                // Generate a new filename based on your desired format
                $newName = $name;
                error_log('FILE->image():> ' . $file->image());
                $file->changeName($newName);
            } catch (Exception $e) {
                throw new Exception('There was an error renaming the file: ' . $e->getMessage());
            }
        }
    ]
];

I also checked this post here and tried to create a plugin, but I don’t know how it works, I’m new on Kirby.

Do you think you can help me?