Thanks for yout time helping me!
So What do you mean with “Since you store the generated poster file in the video metadata”.
What is video metadata ?
I have to rename all files to a certain kind of prefix like “bauhaus_art_design_cloud_” and then if the user rename the video file, the image needs to be renamed too. So when the page visitor rigth click the poster image when the video is not loaded yet and try to download the poster image with the option “save image as” the image will have that name with that pattern.
Hum, and also when the user (the page editor) deletes the video file it must delete the poster image together.
I don’t know if I was clear on the explain, let me know if you couldn’t understand.
I’ve made some changes in the code but didn’t understand what you mean about video metadata but now the .jpg image is generated when uploading a video, the name of the video is changed but the name of the .jpg doen’t change, it gives an error instead and when I delete the video file in the panel, the poster is not deleted together.
here is the code:
<?php
@include_once __DIR__ . '/vendor/autoload.php';
use Intervention\Image\ImageManager;
use FFMpeg\FFMpeg;
use FFMpeg\Coordinate\TimeCode;
use Kirby\Cms\File;
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();
$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;
if ($file->type() == 'video') {
$ffmpeg = FFMpeg::create([
'ffmpeg.binaries' => 'C:/ffmpeg/bin/ffmpeg.exe',
'ffprobe.binaries' => 'C:/ffmpeg/bin/ffprobe.exe',
]);
$video = $ffmpeg->open($file->root());
$frame = $video->frame(TimeCode::fromSeconds(1));
//$posterPath = $newFilePath . '.jpg'; // Path to save the poster image
$posterPath = $file->root() . '.jpg';
$filenameWithExtension = basename($posterPath);
try {
$frame->save($posterPath);
} catch (Exception $e) {
throw new Exception('There was an error saving frame: ' . $e->getMessage());
}
// Create a new file for the poster image
// And calls the file.create:before and after hooks I guess
$poster = new File([
'parent' => $page,
'source' => $filenameWithExtension,
'filename' => $filenameWithExtension,
'template' => 'upload-image',
]);
error_log('poster:> ' . $poster);
$file->update([
'image' => [$poster->uuid()->toString()],
]);
}
//calls the changeName hook
/*it changes the name for the .mp4 file
but when the in the line 65 it calls the file.create hook again
the panel alerts an error telling that the first paramenter must be a kirby file
and null was given
*/
$file->changeName($newName);
},
'file.changeName:after' => 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());
}
}
]
];
and here is the error (If I change the changeName:after to changeName:before there is no error but the image is not renamed too):
I’ll show you the project.yml file to that is the blueprint for this page maybe there is some error too that may be affecting somehow.
title: Project
columns:
- width: 3/3
sections:
page-header:
type: fields
fields:
headline:
label: Headline
type: textarea
buttons:
- italic
- width: 1/3
sections:
page-cover:
type: fields
fields:
cover:
label: Cover
type: files
max: 1
uploads: false
empty: Select a image or video
help: Image or video that will be the cover for this project page.
- width: 1/3
sections:
page-alt-cover:
type: fields
fields:
videocover:
label: Alternative Cover
type: files
max: 1
uploads: false
query: page.images
empty: Select an image
help: An alternative image that will be the cover for the cover above in case its a video.
- width: 1/3
sections:
page-study-program:
type: fields
fields:
studyprogram:
label: Study Program
type: checkboxes
options:
fine_art: Fine Art
fine_art_education: Fine Art Education
media_art_and_design: Media Art and Design
product_design: Product Design
scientific_disciplines: Scientific Disciplines
visual_communication: Visual Communication
- width: 2/4
sections:
page-gallery:
type: fields
fields:
gallery:
empty: No items attatched to gallery yet.
type: structure
fields:
link:
type: url
help: If you put a url here you must remove image from field Image
label:
type: text
image:
type: files
max: 1
uploads: false
- width: 2/4
sections:
images:
type: files
headline: Files
layout: list
template: upload-image
- width: 1/3
sections:
information:
type: fields
fields:
information:
label: Project Information
type: textarea
buttons:
- link
- '|'
- italic
- width: 2/3
sections:
description:
type: fields
fields:
description:
label: Project Description
type: textarea
buttons:
- link
- '|'
- italic
- width: 3/3
sections:
tags:
type: fields
fields:
tags:
label: Tags
type: tags
There is the logs from error_log() that i put in the code while trying to debug what is happening.
[Sun Jul 2 20:31:42 2023] poster:> <img alt="" src="http://localhost:8000/media/pages/tixies/82136a0612-1688340702/gradial1.mp4.jpg">
[Sun Jul 2 20:31:42 2023] FILE->image():>
Thanks again and sorry for bothering you.