Convert a youtube video url to embed code

前端 未结 7 1505
青春惊慌失措
青春惊慌失措 2020-12-07 10:07

I\'m using this to convert youtube url to embed url.

text(t).html().replace(/(?:http:\\/\\/)?(?:www\\.)?(?:youtube\\.com)\\/(?:watch\\?v=)?(.+)/g, \'

        
7条回答
  •  时光说笑
    2020-12-07 10:48

    To anyone looking at this in 2020, you can get the embed code by using the oembed API.

    https://www.youtube.com/oembed?url=&format=
    

    example:

    https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=gBrmnB5aOSI&format=json
    

    The response you will get is

    {
    "type": "video",
    "thumbnail_width": 480,
    "provider_name": "YouTube",
    "title": "Intro To Live Streaming on YouTube",
    "thumbnail_height": 360,
    "provider_url": "https://www.youtube.com/",
    "version": "1.0",
    "height": 270,
    "author_name": "YouTube Creators",
    "html": "",
    "author_url": "https://www.youtube.com/user/creatoracademy",
    "width": 480,
    "thumbnail_url": "https://i.ytimg.com/vi/gBrmnB5aOSI/hqdefault.jpg"
    }
    

    you can use the html data for the iframe

提交回复
热议问题