Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource functions

create_random_video()

Description

Generate a random video which can be used during testing (e.g to upload, or create previews for)

- duration (default: 5 seconds)
- width (default: 300)
- height (default: 300)
- filename (default: random)
- extension (default: mp4)
- text -> Video content text (optional)

Parameters

ColumnTypeDefaultDescription
$info: array
$GLOBALS['ffmpeg_supported_extensions']
$info array Set video parameters:

Return

array Returns an "error" key if something went wrong, otherwise provides some useful info (e.g path)

Location

include/test_functions.php lines 81 to 139

Definition

 
function create_random_video(array $info): array
    {
    
$duration $info['duration'] ?? 5;
    
$width $info['width'] ?? 300;
    
$height $info['height'] ?? 300;
    
$filename $info['filename'] ?? generateSecureKey(32);
    
$extension $info['extension'] ?? 'mp4';

    
$ffmpeg get_utility_path('ffmpeg');
    if (
$ffmpeg !== false && in_array($extension$GLOBALS['ffmpeg_supported_extensions']))
        {
        
// Add text to video only if supported
        
if (isset($info['text']) && mb_strpos(run_command($ffmpegtrue), '--enable-libfontconfig') !== false)
            {
            
$cmd_vf '-vf %filtergraph';
            
$cmd_vf_params = [
                
'%filtergraph' => "drawtext=text='{$info['text']}'"
                    
":font='Times New Roman':fontsize=10:fontcolor=black:box=1:boxcolor=white:boxborderw=5",
            ];
            }
        else
            {
            
$cmd_vf '';
            
$cmd_vf_params = [];
            }

        
// Create video file
        
$path get_temp_dir() . DIRECTORY_SEPARATOR "{$filename}.{$extension}";
        
$cmd_output run_command(
            
"$ffmpeg -i testsrc=duration=%duration:size=%wx%h:rate=30 $cmd_vf %outfile",
            
true,
            
array_merge(
                [
                    
'%duration' => $duration,
                    
'%w' => $width,
                    
'%h' => $height,
                    
'%outfile' => $path,
                ]
                ,
                
$cmd_vf_params
            
)
        );

        if (
mb_strpos($cmd_output' Error ') !== false)
            {
            return [
                
'error' => $cmd_output,
            ];
            }

        return [
            
'path' => $path,
        ];
        }

    return [
        
'error' => 'FFMpeg missing',
    ];
    }

This article was last updated 27th April 2024 13:35 Europe/London time based on the source file dated 4th April 2024 10:05 Europe/London time.