将常规 Youtube“链接"转换为嵌入视频

编程入门 行业动态 更新时间:2024-10-14 16:22:19
本文介绍了将常规 Youtube“链接"转换为嵌入视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的目标:我试图允许用户在我的网站中嵌入指向 Youtube 视频的链接,同时让我控制播放器的设置.

My goal: I am trying to allow users to embed a link to a Youtube video in my site, while giving me control over the player's settings.

我只想通过要求用户提供链接(而不是整个嵌入代码)来做到这一点,我可以从那里以某种方式将该链接粘贴到嵌入代码中.

I would like to do this by only asking the user to supply the link (not the entire embed code), from where I can somehow paste that link into the embed code.

我尝试用一​​些 Youtube 链接 (youtu.be/...) 做一个简单的替换,但它们不起作用,说电影未加载".有没有可靠的方法来做到这一点?

I've tried doing a simple substitution with a few Youtube links (youtu.be/...) but they don't work, saying 'movie not loaded'. Is there a dependable way to do this?

推荐答案

我经常为客户这样做,其要点是您从 URL 中解析出 ID,然后生成 iframe HTML 使用这个.

I do this quite often for clients, the gist of it is that you parse out the ID from the URL, then generate the iframe HTML using this.

def youtube_embed(youtube_url) if youtube_url[/youtu\.be\/([^\?]*)/] youtube_id = $1 else # Regex from # stackoverflow/questions/3452546/javascript-regex-how-to-get-youtube-video-id-from-url/4811367#4811367 youtube_url[/^.*((v\/)|(embed\/)|(watch\?))\??v?=?([^\&\?]*).*/] youtube_id = $5 end %Q{<iframe title="YouTube video player" width="640" height="390" src="www.youtube/embed/#{ youtube_id }" frameborder="0" allowfullscreen></iframe>} end youtube_embed('youtu.be/jJrzIdDUfT4') # => <iframe title="YouTube video player" width="640" height="390" src="www.youtube/embed/jJrzIdDUfT4" frameborder="0" allowfullscreen></iframe>

我把它放在一个助手里.根据口味更改高度、宽度和选项.

I put this in a helper. Change the height, width and options to taste.

更多推荐

将常规 Youtube“链接"转换为嵌入视频

本文发布于:2023-11-05 18:40:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561569.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   常规   链接   视频   Youtube

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!