justijndepover/embed-video

Extract the embed url from Youtube and Vimeo videos

1.0.5 2021-12-10 09:36 UTC

This package is auto-updated.

Last update: 2024-10-28 20:47:55 UTC


README

Latest Version on Packagist Software License Build Status Total Downloads

This package makes it easier to work with both Youtube and Vimeo video url's. It automatically detects the reference from the url.

For example:

https://www.youtube.com/watch?v=dQw4w9WgXcQ
https://youtu.be/dQw4w9WgXcQ
https://www.youtube.com/embed/dQw4w9WgXcQ

are all valid youtube links, but to embed an iFrame, you need the third option.

This package allows all the options as input, and is able to generate the correct output urls to embed the iframe.

Installation

You can install the package with composer

composer require justijndepover/embed-video

Usage

use Justijndepover\EmbedVideo\Video;

$video = Video::from('https://www.youtube.com/watch?v=dQw4w9WgXcQ');

If you provide the class with a faulty link, a Justijndepover\EmbedVideo\VideoException will be thrown.

Validate

$validated = Video::validate('https://www.youtube.com/watch?v=dQw4w9WgXcQ');

This will validate the given url and return either true or false

Embed

$html = $video->embed();

This will generate the embedded iframe for either Youtube or Vimeo.

<iframe type="text/html" src="https://www.youtube.com/embed/dQw4w9WgXcQ?autoplay=0&rel=0" frameborder="0"></iframe>

Embed url

$url = $video->embedUrl();

This will generate the embedded url used in the iframe for either Youtube or Vimeo.

https://www.youtube.com/embed/dQw4w9WgXcQ

Thumbnail

$thumbnail = $video->thumbnail();

This will generate a thumbnail url for the cover image;

http://img.youtube.com/vi/dQw4w9WgXcQ/0.jpg

Reference

$reference = $video->reference();

This will return the video reference;

dQw4w9WgXcQ

Autoplay

To add autoplay to the embed iframe, you can make use of the fluent syntax:

$html = $video->autoplay()->embed();
<iframe type="text/html" src="https://www.youtube.com/embed/dQw4w9WgXcQ?autoplay=1&rel=0" frameborder="0"></iframe>

Class

To add a class to the embed iframe, you can make use of the fluent syntax:

$html = $video->class('video-container')->embed();
<iframe class="video-container" type="text/html" src="https://www.youtube.com/embed/dQw4w9WgXcQ?autoplay=0&rel=0" frameborder="0"></iframe>

Additional attributes

To add additional attributes to the embed iframe, you can make use of the fluent syntax:

$html = $video->addAttribute('width', 'auto')->embed();
<iframe type="text/html" src="https://www.youtube.com/embed/dQw4w9WgXcQ?autoplay=0&rel=0" frameborder="0" width="auto"></iframe>

Security

If you find any security related issues, please open an issue or contact me directly at justijndepover@gmail.com.

Contribution

If you wish to make any changes or improvements to the package, feel free to make a pull request.

License

The MIT License (MIT). Please see License File for more information.