Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Convert YouTube/Vimeo URL to Embed Code

Description
Converts standard video service URLs to their embed-code format; very useful
for a "full-screen" that only fills the window. Great for portrait monitor
layouts when window fills either top or bottom 50% of the screen.
Works with "youtube.com" or "youtu.be" and "vimeo.com" links. Supports
YouTube links which contain a start-time.
Language
C#.net
Minimum Version
Created By
Mike Seman
Contributors
-
Date Created
Feb 27, 2017
Date Last Modified
Feb 27, 2017

Macro Code

// Clipboard Fusion Macro: "Convert YouTube/Vimeo URL to Embed Code"
// Michael Seman (@AphixJS)
// v1.0.0
// 2017-02-22

using System;

public static class ClipboardFusionHelper
{
	public static string ProcessText(string url)
	{
		if (url.Contains("youtube.com") || url.Contains("youtu.be"))
		{
			return ClipboardFusionHelper.ConvertYouTubeToEmbed(url);
		}

		if (url.Contains("vimeo.com"))
		{
			return ClipboardFusionHelper.ConvertVimeoToEmbed(url);
		}

		return url;
	}

	private static string ConvertYouTubeToEmbed(string url)
	{
		string[] parts = url.Split(new char[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
		string idWithPotentialQueryParams = parts[parts.Length - 1].ToString().Replace("?t=","?start=");
		string joiner = idWithPotentialQueryParams.Contains("?") ? "&" : "?";
		string suffix = "ecver=2";
		return "https://www.youtube.com/embed/"+idWithPotentialQueryParams+joiner+suffix;
	}

	private static string ConvertVimeoToEmbed(string url)
	{
		string[] parts = url.Split(new char[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
		string id = parts[parts.Length - 1].ToString();
		return "https://player.vimeo.com/video/"+id;
	}
}