The Symfony Validator component provides tens of built-in constraints to validate all kinds of data. In Symfony 7.4, a new Video constraint has been added to validate video files.
The Video constraint works similarly to the existing Image constraint,
but it's specifically designed for video files and includes video-specific
validation options. Before using it, make sure to install FFmpeg, which
provides the ffprobe tool used to extract and validate video metadata.
Use this constraint to enforce requirements on dimensions, aspect ratios, codecs, container formats, and more:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// src/Entity/VideoContent.php
namespace App\Entity;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
class VideoContent
{
#[Assert\Video(
maxWidth: 1920,
maxHeight: 1080,
maxSize: '100M',
mimeTypes: ['video/mp4', 'video/webm'],
)]
private File $videoFile;
}
The constraint supports many validation options, including minWidth,
maxWidth, minHeight, maxHeight, minRatio, maxRatio,
minPixels, and maxPixels. You can also control video orientation by
disabling landscape, portrait, or square videos:
1 2 3 4 5 6 7
#[Assert\Video(
minWidth: 1280,
minHeight: 720,
allowPortrait: false,
allowSquare: false,
)]
private File $videoFile;
By default, the constraint accepts common video codecs and container formats such as H.264, HEVC, VP9, AV1, MP4, WebM, and MKV. You can customize these lists to fit your own requirements:
1 2 3 4 5
#[Assert\Video(
allowedCodecs: ['h264', 'hevc'],
allowedContainers: ['mp4', 'webm'],
)]
private File $videoFile;