Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test 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 97 to 161

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 17th November 2024 15:35 Europe/London time based on the source file dated 8th November 2024 11:45 Europe/London time.