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
$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 83 to 147

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';
    if (
is_banned_extension($extension)) {
        
$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 drawtext=text=%info_text'
                    
":font='Times New Roman':fontsize=10:fontcolor=black:box=1:boxcolor=white:boxborderw=5";
            
$cmd_vf_params = [
                
'%info_text' => new CommandPlaceholderArg(
                    
$info['text'],
                    fn(
$val): bool => preg_match('/^[a-zA-Z0-9\#\s]*$/'$val) === 1
                
),
            ];
            }
        else
            {
            
$cmd_vf '';
            
$cmd_vf_params = [];
            }

        
// Create video file
        
$path get_temp_dir() . DIRECTORY_SEPARATOR safe_file_name($filename) . ".{$extension}";
        
$cmd_output run_command(
            
"$ffmpeg -i testsrc=duration=%duration:size=%wx%h:rate=30 $cmd_vf %outfile",
            
true,
            
array_merge(
                [
                    
'%duration' => (int) $duration,
                    
'%w' => (int) $width,
                    
'%h' => (int) $height,
                    
'%outfile' => new CommandPlaceholderArg($path, fn(): bool => true),
                ]
                ,
                
$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 26th July 2024 21:35 Europe/London time based on the source file dated 22nd May 2024 17:25 Europe/London time.