Skip to content
Youtube-dl wrapper for PHP
PHP
Branch: master
Clone or download
kardoqadir and norkunas Add Flac extension to allowedAudioFormats (#100)
* Add Flac extension to allowedAudioFormats

Add Flac extension to allowedAudioFormats variable

* Fix style Ci problem

Fix  Style Ci problem
Latest commit 32bcd68 Oct 8, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Add Flac extension to allowedAudioFormats (#100) Oct 8, 2019
tests Fix phpunit deprecations May 29, 2019
.editorconfig yaml indent size Mar 29, 2015
.gitignore fixer config Oct 20, 2015
.php_cs Prepare release Nov 28, 2017
.scrutinizer.yml added scrutinizer config Oct 10, 2015
.styleci.yml Add exception message Sep 2, 2018
.travis.yml Update travis May 29, 2019
LICENSE updated copyright year Jan 14, 2016
README.md Implement download progress Nov 17, 2017
composer.json Cleanup Apr 30, 2019
phpunit.xml.dist Refactoring for v1 Oct 25, 2016

README.md

Youtube-dl PHP

A PHP wrapper for youtube-dl tool.

Latest Stable Version Latest Unstable Version StyleCI Scrutinizer Code Quality Total Downloads Build Status License

Install

First step is to download the youtube-dl and add it's path to environment variables.

Second step is to install the wrapper using Composer:

composer require norkunas/youtube-dl-php

Download video

<?php
require __DIR__ . '/vendor/autoload.php';

use YoutubeDl\YoutubeDl;
use YoutubeDl\Exception\CopyrightException;
use YoutubeDl\Exception\NotFoundException;
use YoutubeDl\Exception\PrivateVideoException;

$dl = new YoutubeDl([
    'continue' => true, // force resume of partially downloaded files. By default, youtube-dl will resume downloads if possible.
    'format' => 'bestvideo',
]);
// For more options go to https://github.com/rg3/youtube-dl#user-content-options

$dl->setDownloadPath('/home/user/downloads');
// Enable debugging
/*$dl->debug(function ($type, $buffer) {
    if (\Symfony\Component\Process\Process::ERR === $type) {
        echo 'ERR > ' . $buffer;
    } else {
        echo 'OUT > ' . $buffer;
    }
});*/
try {
    $video = $dl->download('https://www.youtube.com/watch?v=oDAw7vW7H0c');
    echo $video->getTitle(); // Will return Phonebloks
    // $video->getFile(); // \SplFileInfo instance of downloaded file
} catch (NotFoundException $e) {
    // Video not found
} catch (PrivateVideoException $e) {
    // Video is private
} catch (CopyrightException $e) {
    // The YouTube account associated with this video has been terminated due to multiple third-party notifications of copyright infringement
} catch (\Exception $e) {
    // Failed to download
}

Download only audio (requires ffmpeg or avconv and ffprobe or avprobe)

<?php
require __DIR__ . '/vendor/autoload.php';

use YoutubeDl\YoutubeDl;

$dl = new YoutubeDl([
    'extract-audio' => true,
    'audio-format' => 'mp3',
    'audio-quality' => 0, // best
    'output' => '%(title)s.%(ext)s',
]);
$dl->setDownloadPath('/home/user/downloads');

$video = $dl->download('https://www.youtube.com/watch?v=oDAw7vW7H0c');

Download progress

<?php
$dl->onProgress(function ($progress) {
    $percentage = $progress['percentage'];
    $size = $progress['size'];
    $speed = $progress['speed'] ?? null;
    $eta = $progress['eta'] ?? null;
    
    echo "Percentage: $percentage; Size: $size";
    if ($speed) {
        echo "; Speed: $speed";
    }
    if ($eta) {
        echo "; ETA: $eta";
    }
    // Will print: Percentage: 21.3%; Size: 4.69MiB; Speed: 4.47MiB/s; ETA: 00:01
});

Disabled options which would break download:

list-formats, list-subs, list-thumbnails, get-url, get-title, get-id, get-thumbnail, get-description, get-duration, get-filename, get-format, dump-json, dump-single-json, print-json, write-info-json (used internally), newline, no-progress, console-title, verbose, dump-pages, write-pages, print-traffic, ignore-config (used internally), all-formats, playlist-start, playlist-end, playlist-items, playlist-reverse, yes-playlist, no-playlist (used internally).

You can’t perform that action at this time.