Video Upload and Transcoding Implementation
Video upload requires separate pipeline: file is received on server or directly to cloud, then transcoded into multiple qualities (360p, 720p, 1080p) and formats (MP4/H.264, WebM/VP9). Transcoding is CPU-intensive operation, executed in background.
Pipeline architecture
[Client] → [Presigned S3 Upload] → [S3: original/]
↓
[S3 Event → SQS/Lambda] → [Transcoding Worker (FFmpeg)]
↓
[S3: processed/{quality}/] → [CDN CloudFront]
↓
[Update DB: video.status = ready, paths = {...}]
↓
[WebSocket/Webhook → Client notification]
Step 1: Presigned Upload to S3
class VideoController extends Controller
{
public function initiateUpload(Request $request): JsonResponse
{
$request->validate([
'filename' => 'required|string|max:255',
'content_type' => 'required|in:video/mp4,video/webm,video/quicktime,video/x-msvideo',
'size' => 'required|integer|max:5368709120', // 5 GB
]);
$key = sprintf(
'original/%d/%s/%s',
auth()->id(),
now()->format('Y/m'),
Str::uuid() . '.' . pathinfo($request->filename, PATHINFO_EXTENSION)
);
$s3 = app('aws')->createClient('s3');
$command = $s3->getCommand('PutObject', [
'Bucket' => config('filesystems.disks.s3.bucket'),
'Key' => $key,
'ContentType' => $request->content_type,
]);
$presigned = $s3->createPresignedRequest($command, '+2 hours');
$video = Video::create([
'user_id' => auth()->id(),
'original_key' => $key,
'original_name' => $request->filename,
'status' => 'pending',
'size' => $request->size,
]);
return response()->json([
'video_id' => $video->id,
'upload_url' => (string) $presigned->getUri(),
'key' => $key,
]);
}
public function confirmUpload(Request $request, Video $video): JsonResponse
{
$this->authorize('update', $video);
$video->update(['status' => 'uploaded']);
TranscodeVideoJob::dispatch($video);
return response()->json(['status' => 'processing']);
}
}
Step 2: Transcoding with FFmpeg
class TranscodeVideoJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable;
public int $timeout = 7200; // 2 hours
public int $tries = 2;
const QUALITIES = [
'360p' => ['width' => 640, 'height' => 360, 'bitrate' => '800k', 'audiorate' => '96k'],
'720p' => ['width' => 1280, 'height' => 720, 'bitrate' => '2500k', 'audiorate' => '128k'],
'1080p' => ['width' => 1920, 'height' => 1080, 'bitrate' => '5000k', 'audiorate' => '192k'],
];
public function __construct(private Video $video) {}
public function handle(): void
{
$this->video->update(['status' => 'transcoding']);
$inputUrl = Storage::disk('s3')->temporaryUrl($this->video->original_key, now()->addHour());
$paths = [];
foreach (self::QUALITIES as $quality => $params) {
$outputKey = sprintf('processed/%d/%s/%s.mp4', $this->video->user_id, $this->video->id, $quality);
$outputPath = sys_get_temp_dir() . "/{$this->video->id}_{$quality}.mp4";
$scale = "scale={$params['width']}:{$params['height']}:force_original_aspect_ratio=decrease,pad={$params['width']}:{$params['height']}:(ow-iw)/2:(oh-ih)/2";
$command = [
'ffmpeg', '-y', '-i', $inputUrl,
'-vf', $scale,
'-c:v', 'libx264',
'-preset', 'medium',
'-crf', '23',
'-maxrate', $params['bitrate'],
'-bufsize', (int)($params['bitrate']) * 2 . 'k',
'-c:a', 'aac',
'-b:a', $params['audiorate'],
'-movflags', '+faststart',
$outputPath,
];
$process = new \Symfony\Component\Process\Process($command);
$process->setTimeout(3600);
$process->run();
if (!$process->isSuccessful()) {
throw new \RuntimeException("FFmpeg failed for {$quality}");
}
Storage::disk('s3')->putFileAs(dirname($outputKey), new \Illuminate\Http\File($outputPath), basename($outputKey));
$paths[$quality] = $outputKey;
unlink($outputPath);
}
$this->video->update([
'status' => 'ready',
'paths' => $paths,
]);
$this->video->user->notify(new VideoReadyNotification($this->video));
}
public function failed(\Throwable $e): void
{
$this->video->update(['status' => 'failed', 'error' => $e->getMessage()]);
}
}
AWS MediaConvert for large volumes
For large video volumes, use managed service:
import boto3
def transcode_with_mediaconvert(input_key: str, output_prefix: str) -> str:
client = boto3.client('mediaconvert', region_name='eu-west-1')
job = client.create_job(
Role='arn:aws:iam::123456789:role/MediaConvertRole',
Settings={...}
)
return job['Job']['Id']
Transcoding progress
Route::get('/videos/{video}/status', function (Video $video) {
return response()->json([
'status' => $video->status,
'progress' => $video->transcoding_progress,
'paths' => $video->status === 'ready' ? $video->paths : null,
]);
});
Implementation timeline
| Task | Timeline |
|---|---|
| Presigned upload + FFmpeg queue | 4–5 days |
| Thumbnail generation + metadata | +1–2 days |
| AWS MediaConvert integration | 2–3 days |
| HLS adaptive streaming | +3–4 days |







