mpratt / Embera

A Oembed consumer library, that gives you information about urls. It helps you replace urls to youtube or vimeo for example, with their html embed code. It has advanced features like offline support, responsive embeds and caching support.
MIT License
335 stars 59 forks source link

Get video_id from Youtube URL #79

Closed tomaszkane closed 3 years ago

tomaszkane commented 3 years ago

Embera actually don't return video_id for YT URL like https://www.youtube.com/watch?v=_wkhNDiofsY

How about implement some regexp for this? https://stackoverflow.com/a/17030234/1829368

mpratt commented 3 years ago

Hi @tomaszkane !

You are right, Embera doesnt return the video_id, but that is because youtube does not do it either. Check this request for example https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=J---aiyznGQ&format=json

However you can use filters and fetch the video_id and return it in the response if you really need it.


$embera = new Embera(...........);
$embera->addFilter(function ($response) {
    if (strtolower($response['embera_provider_name']) == 'youtube' && preg_match('~/embed/(.+)\?feature=oembed~', $response['html'], $matches)) {
        $response['video_id'] = $matches['1'];
    }
    return $response;
});

var_dump($embera->getUrlData('https://www.youtube.com/watch?v=J---aiyznGQ'));

Something like that should work. The code is untested but you get the idea...

Let me know if that helps..

tomaszkane commented 3 years ago

Hi, yes I know, YT don't return video_id. But why Embera not adding this information to (new Embera())->getUrlData($videoUrl); by own / automatically? ID always must be present in given URI, so add it to results from getUrlData() ?

BTW, thanks for simpler solution for get this is (filter instead of long regexp).

mpratt commented 3 years ago

Hi @tomaszkane ! I am glad the filter option worked for you!

In regards to adding the video_id into the Embera response for Youtube, I dont think I will add it. I want to respect the endpoint response as much as possible, only on a few providers I modify the html response, but I mostly want to leave the response as close as possible to the original.

Perhaps if many users ask for this functionality to be added, I'll consider doing it, but for now, I think with filters you have the option to get the info you need for your usecase. Filters are very versatile since it allows access to the raw response.