使用 Android Youtube ApI v3 从 youtube url 获取视频 ID [英] Get video id from youtube url using Android Youtube ApI v3

查看:52
本文介绍了使用 Android Youtube ApI v3 从 youtube url 获取视频 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Android Youtube API v3.我的应用程序播放 YouTube 视频.我想从 youtube 网址获取 videoID.
我看到有很多示例使用模式来解析 url 以获取视频 ID.
但是我担心将来youtube更改其网址时这种方式会失败.
youtube视频网址有几种格式:

I'm using Android Youtube API v3. My application plays youtube video. I want to get videoID from youtube url.
I see there are a lot of sample that uses pattern to parse the url to get the video ID.
But I'm afraid of this way will fail when youtube change their url in the future.
There are some formats of youtube video url:

http://www.youtube.com/watch?v=0zM4nApSvMg&feature=feedrec_grec_index
http://www.youtube.com/user/SomeUser#p/a/u/1/QDK8U-VIH_o
http://www.youtube.com/v/0zM4nApSvMg?fs=1&hl=en_US&rel=0
http://www.youtube.com/watch?v=0zM4nApSvMg#t=0m10s
http://www.youtube.com/embed/0zM4nApSvMg?rel=0
http://www.youtube.com/watch?v=0zM4nApSvMg
http://youtu.be/0zM4nApSvMg

是否有任何 API 可以从 url 中获取视频 ID?
提前致谢.

Are there any API to get video ID from the url?
Thanks in advance.

推荐答案

是的,正如您已经发现的那样,由于不一致,获取 youtube 视频的视频 ID 并不是那么简单.您需要在要使用正则表达式解析 URL 的 PHP(或任何语言脚本)中包含所有可能的排列和组合.

Yes, as you already figured this out that it is not that simple to get the video ID of a youtube video because of the inconsistency. You need to include all the possible permutations and combinations in the PHP (or what ever language script) you are going to parse the URL using regular expressions.

这个问题之前已经问过,所以你可以参考它并添加你自己的逻辑来解析 URL.我正在粘贴其中一些的链接:

This question has been asked previously on SO you can refer that and add your own logic to parse the URL. I am pasting the links of some of them:

Youtube API - 提取视频 ID

如何从 url 获取 youtube 视频 ID

JavaScript 正则表达式:如何获取 YouTube来自 URL 的视频 ID?

https://halgatewood.com/php-get-the-youtube-video-id-from-a-youtube-url

http://www.microtuts.com/c-get-youtube-video-id-from-url-with-regex/

这篇关于使用 Android Youtube ApI v3 从 youtube url 获取视频 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆