Compress Videos with FFmpeg and PHP‑FFMpeg: Command Line and Code Guide
This article explains how to use FFmpeg for command‑line video compression, demonstrates size reduction from 3.8 MiB to 644 KiB, introduces an online compression tool, and provides a PHP‑FFMpeg code example that resizes and re‑encodes videos, showing the resulting file sizes.
FFmpeg Overview
FFmpeg is an open‑source suite for recording, converting and streaming audio/video. It includes libavcodec with many codecs written from scratch for portability and quality. It runs on Linux, Windows, macOS, and other operating systems.
Command‑line video compression
Original file size 3.8 MiB. The following command compresses it to ~644 KiB (≈83 % reduction).
ffmpeg -i "tinywan-input.mp4" -c:v libx264 -tag:v avc1 -movflags faststart -crf 30 -preset superfast "tinywan-output.mp4"Explanation of key options: -c:v libx264 – use the H.264 encoder. -crf 30 – constant‑rate‑factor; higher values give smaller size at lower quality. -preset superfast – trade encoding speed for compression efficiency. -movflags faststart – relocate the MP4 moov atom to the file beginning for progressive streaming. -tag:v avc1 – set the video codec tag for compatibility.
Resulting file size: 644 KiB.
Online lossless compression tool
An online tool (rotato) can achieve a similar reduction without installing FFmpeg. The service is reachable at https://tools.rotato.app.
PHP‑FFMpeg library example
PHP‑FFMpeg requires PHP 8.0+. Install it via Composer: composer require php-ffmpeg/php-ffmpeg The script below creates an FFmpeg instance, opens a source video, resizes it to 640×480, synchronizes timestamps, and saves the output as an H.264 MP4 file.
<?php
declare(strict_types=1);
require_once __DIR__ . '/vendor/autoload.php';
/* 1. Initialize configuration */
$ffmpeg = \FFMpeg\FFMpeg::create([
'ffmpeg.binaries' => '/usr/bin/ffmpeg',
'ffprobe.binaries' => '/usr/bin/ffprobe',
]);
/* 2. Open video file */
$video = $ffmpeg->open('/home/www/build/tinywan-input.mp4');
/* 3. Apply compression (resize) */
$video->filters()
->resize(new \FFMpeg\Coordinate\Dimension(640, 480))
->synchronize();
/* 4. Save compressed video */
$video->save(new \FFMpeg\Format\Video\X264(), '/home/www/build/ffmpeg/php-tinywan-output.mp4');Resulting file size: 735 KiB.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
