DEV Community

Cover image for How to Build a TikTok Video Downloader Using Laravel and yt-dlp
Faheem Zia
Faheem Zia

Posted on

How to Build a TikTok Video Downloader Using Laravel and yt-dlp

Building a **[TikTok video downloader](https://ssvtiktok.com/)** sounds simple—until you try deploying it in production. Between short URLs, slideshow posts, 403 errors, anti-bot restrictions, and platform updates, a basic implementation can quickly fail.

In this guide, I’ll walk you through how to build a reliable TikTok video downloader using Laravel and yt-dlp, structured for real-world usage.


Why Use yt-dlp Instead of an API?

You have two main approaches:

  • Using a third-party API
  • Using yt-dlp directly on your server

APIs often break, require subscriptions, or enforce rate limits. yt-dlp is open-source, actively maintained, and handles TikTok extraction natively.

For production-level reliability, yt-dlp is the better long-term solution.


Project Architecture

  1. User pastes TikTok URL
  2. Laravel validates and sanitizes input
  3. Backend probes URL using yt-dlp JSON mode
  4. Determine video vs slideshow
  5. Download file
  6. Return file to user

Step 1: Install yt-dlp on Ubuntu

sudo curl -L https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -o /usr/local/bin/yt-dlp
sudo chmod a+rx /usr/local/bin/yt-dlp
yt-dlp --version

Always keep yt-dlp updated:

yt-dlp -U

Step 2: Create Laravel Controller

php artisan make:controller TikTokController

We’ll use Symfony Process to execute yt-dlp securely.


Step 3: Probe Video Metadata


use Symfony\Component\Process\Process;

private function probeVideo($url)
{
    $process = new Process([
        'yt-dlp',
        '-J',
        '--no-warnings',
        $url
    ]);

    $process->setTimeout(20);
    $process->run();

    if (!$process->isSuccessful()) {
        throw new \Exception("Unable to fetch video metadata.");
    }

    return json_decode($process->getOutput(), true);
}

This allows us to detect if the link is a video or slideshow.


Step 4: Detect Video vs Slideshow


$info = $this->probeVideo($url);

$isSlideshow = isset($info['entries']) && is_array($info['entries']);

If slideshow → handle multiple images. If video → proceed with MP4 extraction.


Step 5: Download Video


private function downloadVideo($url)
{
    $outputPath = storage_path('app/downloads/%(id)s.%(ext)s');

    $process = new Process([
        'yt-dlp',
        '-f', 'bv*+ba/b',
        '--merge-output-format', 'mp4',
        '-o', $outputPath,
        $url
    ]);

    $process->setTimeout(120);
    $process->run();

    if (!$process->isSuccessful()) {
        throw new \Exception("Download failed.");
    }

    return true;
}

Handling 403 Errors

TikTok frequently blocks unauthenticated scraping. The fix: use browser-exported cookies.


'yt-dlp',
'--cookies', storage_path('app/cookies.txt'),

This significantly improves reliability.


Security Best Practices

  • Validate TikTok domain
  • Use Symfony Process (not shell_exec)
  • Set timeouts
  • Apply rate limiting
  • Auto-delete old files

Add rate limiting:


Route::middleware('throttle:10,1')->post('/download', ...);

Production Optimization

Use Queue Jobs

php artisan make:job DownloadTikTokVideo

Cleanup Old Files

php artisan make:command CleanupDownloads

Schedule cron to delete files older than 1 hour.


Common Issues

Unsupported URL

Usually caused by:

  • /photo/ slideshow links
  • Outdated yt-dlp
  • Short URL not resolved

Random Download Failures

  • Missing cookies
  • Server timeout
  • Bandwidth limits

Legal Considerations

  • Add proper disclaimer
  • Do not store content permanently
  • Respect copyright laws
  • Avoid misleading branding

Final Thoughts

Building a TikTok downloader isn’t just about running a command. It requires proper validation, secure execution, server optimization, and continuous updates.

Laravel provides structure. yt-dlp provides extraction power. Together, they create a scalable solution.

If implemented properly, this can evolve from a simple utility into a serious side project or SaaS tool.

Top comments (0)