开发者

How to get the embed HTML code for a video hosted in youtube programmatically

How to get the embed HTML code for a video hosted in youtube 开发者_JAVA百科programmatically. What Java API is available


Use the YouTube Data API (there's pre-built GData client libraries, or you can do the HTTP/XML stuff yourself).

One of the <media:content/> entries will contain a URL for the embeddable SWF, if the video is embeddable.


Assuming you have the URL of the video, it's fairly simple to generate one. You need the end of the URL (the part after the /watch?v=, let's call it ID). To generate the iframe embed html, just place it in the appropriate place (in the src attribute, don't include the brackets):

<iframe title="YouTube video player" class="youtube-player" type="text/html" width="640"
height="390" src="http://www.youtube.com/embed/{ID}" frameborder="0"
allowFullScreen></iframe>

There are a couple of ways to get the v parameter from the URL. A regular expression would work.


Though the accepted answer works, if you want to do this programmatically you need the correct aspect ratio in order to generate optimal iframe dimensions for your video. I wrote the following php function that can generate a link for you on the fly. It uses the bash utility youtube-dl to get information about the video from any youtube link, so you'll need to make sure that's installed (apt-get install youtube-dl should work on Ubuntu or other debian flavors)

function getYoutubeEmbed($link, $size = [], $options = [], $privacy = false) {

    $options += [
        'rel'      => true, // Show suggested videos when the video finishes.
        'controls' => true, // Show player controls.
        'showinfo' => true, // Show video title and player actions.
    ];

    $json = json_decode(exec('youtube-dl -j --no-warnings ' . $link . ' 2>/dev/null'));
    if ($json && !empty($id = $json->id) && !empty($width = $json->width) && !empty($height = $json->height)) {

        $args = [];
        foreach ($options as $option => $value) {
            if (!$value) {
                $args[] = $option . '=0';
            }
        }

        if ($size) {
            if (!empty($size['width']) && !empty($size['height'])) {
                $width  = $size['width'];
                $height = $size['height'];
            } else if (!empty($size['width'])) {
                $height = ceil(($height * $size['width']) / $width);
                $width  = $size['width'];
            } else if (!empty($size['height'])) {
                $width  = ceil(($width * $size['height']) / $height);
                $height = $size['height'];
            }
        }

        $url = ($privacy ? 'www.youtube-nocookie.com/embed/' : 'www.youtube.com/embed/') . $id . ($args ? '?' . implode('&amp;',$args) : '');
        $iframe = '<iframe width="' . $width . '" height="' . $height . '" src="//' . $url . '" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>';
        return $iframe;
    } else {
        return false;
    }
}

The function is fairly self explanatory but here's the breakdown:

  • At the minimum you need to supply a link for the first argument.
  • The second argument is an array of width, height or both. If you only specify one it will treat keep the default aspect ratio and calculate the other dimension for you (this is how I'd typically use it).
  • The third argument is an optional array of arguments which are documented in the function itself.
  • The fourt is an optional boolean argument for 'privacy' which is explained as:

Enable privacy-enhanced mode. When you turn on privacy-enhanced mode, YouTube won't store information about visitors on your website unless they play the video.

Usage example:

$link = 'https://www.youtube.com/watch?v=adAqQct3vRI';
echo getYoutubeEmbed($link, ['width' => 560], ['rel' => false]);

Output:

<iframe width="560" height="315" src="//www.youtube.com/embed/605gdJGdaPE?rel=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜