pira/ytdl

A simple PHP library for Download Videos from YouTube, Instagram, X and etc.

1.7.0 2024-08-01 08:47 UTC

This package is not auto-updated.

Last update: 2024-10-27 09:50:29 UTC


README

A simple PHP library for downloading videos from YouTube, Instagram, X, and more. Powered by Cobalt

Powered by Cobalt

Powered by Cobalt

This library relies on Cobalt’s free API.

Table of Contents

Installation

To install the pira/ytdl library, you need to use Composer. Run the following command:

composer require pira/ytdl

Usage

After installing the library, you can start using it in your PHP project. Here's a basic example:

<?php

use pira\YTDL;

require_once 'vendor/autoload.php';

try {
    $ytdl = new YTDL('https://www.youtube.com/watch?v=OAr6AIvH9VY');
    $ytdl->setQuality('480');
    $response = $ytdl->sendRequest();
    print_r($response);
} catch (Exception $e) {
    echo $e->getMessage();
}

Methods

__construct(string $url)

Initializes the YTDL class with a URL.

  • Parameters:
    • $url (string): The URL to be used in requests.

setQuality(string $quality): void

Sets the video quality for downloads.

  • Parameters:
    • $quality (string): The desired video quality (e.g., 144, 720, max).
  • Throws:
    • Exception if the provided quality is not valid.

setFilenamePattern(string $pattern): void

Sets the filename pattern for downloaded files.

  • Parameters:
    • $pattern (string): The desired filename pattern. Available patterns:
      • classic:
        • Video: youtube_dQw4w9WgXcQ_640x360_h264.mp4
        • Audio: youtube_dQw4w9WgXcQ_audio.mp3
      • basic:
        • Video: Video Title (360p, h264).mp4
        • Audio: Audio Title - Audio Author.mp3
      • pretty:
        • Video: Video Title (360p, h264, youtube).mp4
        • Audio: Audio Title - Audio Author (soundcloud).mp3
      • nerdy:
        • Video: Video Title (360p, h264, youtube, dQw4w9WgXcQ).mp4
        • Audio: Audio Title - Audio Author (soundcloud, 1242868615).mp3
  • Throws:
    • Exception if the provided pattern is not valid.

setVCodec(string $codec): void

Sets the video codec for downloads.

  • Parameters:
    • $codec (string): The desired video codec (e.g., h264, av1, vp9).
  • Throws:
    • Exception if the provided codec is not valid.

setAFormat(string $format): void

Sets the audio format for downloads.

  • Parameters:
    • $format (string): The desired audio format (e.g., mp3, ogg, wav).
  • Throws:
    • Exception if the provided format is not valid.

enableAudioOnly(): void

Enables downloading only audio.

enableTTFullAudio(): void

Enables downloading the original sound from a TikTok video.

enableAudioMuted(): void

Enables muting the audio track in video downloads.

enableDubLang(): void

Enables using the Accept-Language header for YouTube video audio tracks.

enableDisableMetadata(): void

Enables disabling file metadata.

enableTwitterGif(): void

Enables converting Twitter gifs to .gif format.

enableTiktokH265(): void

Enables preferring 1080p h265 videos for TikTok.

setAcceptLanguage(string $language): void

Sets the custom Accept-Language header value for requests.

  • Parameters:
    • $language (string): The custom Accept-Language header value.

sendRequest(): array

Sends the configured request to the API and returns the response.

  • Returns:

    • An associative array containing the status and data of the response.
  • Throws:

    • Exception if there is an error in sending the request.

Response Body Variables

Source: Cobalt API Documentation - Response Body Variables

Picker Item Variables

item type: object

Source: Cobalt API Documentation - Picker Item Variables

Supported Services

This list is not final and keeps expanding over time.

Source: Cobalt - Supported Services

Additional Notes or Features

Source: Cobalt - Additional Notes or Features per Service

If support for a service you want is missing, create an issue (or a pull request 👀) on Cobalt's GitHub repository.

License

This library is licensed under the MIT License. See the LICENSE file for more details.

Contact

For any queries or issues, please contact:

References