Skip to content

Instantly share code, notes, and snippets.

@litil
Created March 31, 2014 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save litil/9892507 to your computer and use it in GitHub Desktop.
Save litil/9892507 to your computer and use it in GitHub Desktop.
[Java] VideoUtils
package com.litil.utils;
/**
* This class contains methods concerning videos. For now, it can help
* you check if an url is valid and convert a "classic" url to an
* "embed" one.
*
*/
public class VideoUtils {
private static final String VIDEO_TYPE_YOUTUBE = "youtube";
private static final String VIDEO_TYPE_DAILYMOTION = "dailymotion";
private static final String VIDEO_TYPE_VIMEO = "vimeo";
/**
* Cette méthode retourne le type de vidéo correspondant à l'url
* passée en paramètre.
*
* @param url
*
* @return le type de la vidéo, soit youtube/dailymotion/vimeo
*/
public static String getVideoType(String url){
String videoType = null;
if (url != null){
if (checkYoutubeUrl(url)){
videoType = VIDEO_TYPE_YOUTUBE;
} else if (checkDailymotionUrl(url)){
videoType = VIDEO_TYPE_DAILYMOTION;
} else if(checkVimeoUrl(url)){
videoType = VIDEO_TYPE_VIMEO;
}
}
return videoType;
}
/**
* Cette méthode retourne l'url embed à partir d'une url classique
*
* @param url classique
*
* @return l'url embed
*/
public static String convertUrl(String url){
String convertedUrl = null;
if (url != null){
if (checkYoutubeUrl(url)){
convertedUrl = url.replace("https://", "//");
convertedUrl = convertedUrl.replace("http://", "//");
convertedUrl = convertedUrl.replace("watch?v=", "embed/");
} else if (checkDailymotionUrl(url)){
convertedUrl = url.replace("https://", "//");
convertedUrl = convertedUrl.replace("http://", "//");
convertedUrl = convertedUrl.substring(0, convertedUrl.indexOf("_"));
convertedUrl = convertedUrl.replace("/video/", "/embed/video/");
} else if(checkVimeoUrl(url)){
convertedUrl = url.replace("https://", "//player.");
convertedUrl = convertedUrl.replace("http://", "//player.");
convertedUrl = convertedUrl.replace(".com/", ".com/video/");
}
}
return convertedUrl;
}
/**
* This method checks that the given url is a valid Youtube url.
*
* @param url
*
* @return true if it's a valid Youtube url
*/
private static boolean checkYoutubeUrl(String url){
return (url.toLowerCase().indexOf("www.youtube.com") >= 0 || url.toLowerCase().indexOf("www.youtu.be") >= 0 || url.toLowerCase().indexOf("youtu.be") >= 0);
}
/**
* This method checks that the given url is a valid Dailymotion url.
*
* @param url
*
* @return true if it's a valid Dailymotion url
*/
private static boolean checkDailymotionUrl(String url){
return (url.toLowerCase().indexOf("www.dailymotion.com") >= 0 || url.toLowerCase().indexOf("://dai.ly") >= 0);
}
/**
* This method checks that the given url is a valid Vimeo url.
*
* @param url
*
* @return true if it's a valid Vimeo url
*/
private static boolean checkVimeoUrl(String url){
return (url.toLowerCase().indexOf("://vimeo.com") >= 0 || url.toLowerCase().indexOf("player.vimeo.com") >= 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment