How to conditionally set a video transformation's format
What is the best way to conditionally set a video transformation's format and codec depending on the length of a video?
For example, if a video is > 30 seconds, I want to format with `f_m3u8`. If < 30 seconds, use `f_auto`. From the docs, the conditional formatting doesn't seem to allow this.
If the answer is that it cannot be done, my idea is to manually check the video duration and set the transformations accordingly.
We are using the Upload Widget to upload videos. Is there a way to get the video duration before the video is uploaded? I know the callback has a `file` object available with just metadata, but wondering if there is a way to leverage this to get the video duration?
useCloudinaryUpload({ ... callback: (uploadError, result) => { if (result.event === 'upload-added') { const file = result?.info?.file
//Can we get video duration here? } }, })
-
Hi Jason,
We currently don't support fetch_format (f_) parameter e.g. f_m3u8 or f_auto in the if/else branches, so sadly it won't be possible at the moment.
However, let me provide you some other ways to solve the problem.
Since you are uploading the files from the client-side using Upload Widget there may be libraries that could read the file from memory (assuming local file selection) and extract duration
so that you can pass different parameters/preset (https://cloudinary.com/documentation/upload_widget#upload_preset_selection) with eager based on the duration.
Otherwise, you could wait for the upload response which will contain the duration and send an explicit() api (https://cloudinary.com/documentation/image_upload_api_reference#explicit) call afterwards as a 2-step process.
If you want you can read more about eager transformations (https://cloudinary.com/documentation/transformations_on_upload) and some more examples on calling some JavaScript before the upload to modify request params (https://cloudinary.com/documentation/upload_widget#pre_batch_validation)
Best regards,
Adam0
Post is closed for comments.
Comments
1 comment