PHP code example of hazel / ytdlphp

1. Go to this page and download the library: Download hazel/ytdlphp library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hazel / ytdlphp example snippets


use Ytdlphp\Options;
use Ytdlphp\YtDlp;

$ytDlp = new YtDlp();

$ytDlp->download(
    'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
    Options::create()
        ->format('bestvideo+bestaudio')
        ->output('%(title)s.%(ext)s'),
);

$info = $ytDlp->extractInfo('https://www.youtube.com/watch?v=dQw4w9WgXcQ');

echo $info->title;
echo $info->url?->toAsciiString();

use Ytdlphp\Options;
use Ytdlphp\YtDlp;

$ytDlp = new YtDlp()
    ->withBinary('/usr/local/bin/yt-dlp')
    ->withWorkingDirectory('/tmp/downloads')
    ->withTimeout(300)
    ->withDefaultOptions(Options::create()->noWarnings());

use Ytdlphp\Option\AudioFormat;
use Ytdlphp\Option\Browser;
use Ytdlphp\Options;

$options = Options::create()
    ->extractAudio()
    ->audioFormat(AudioFormat::Mp3)
    ->cookiesFromBrowser(Browser::Firefox)
    ->noPlaylist()
    ->merge($extraOptions);

use Uri\WhatWg\Url;
use Ytdlphp\UrlParser;

$url = UrlParser::parse('https://example.com/watch?v=abc');
$ytDlp->extractInfo($url);

$mediaUrls = $ytDlp->getUrls('https://example.com/watch?v=abc');
foreach ($mediaUrls as $mediaUrl) {
    echo $mediaUrl->toAsciiString();
}