MyBB Community Forums

Full Version: MyCode for starting Youtube videos at a specific time
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
We're all familiar with the Youtube feature, where you can add what time to start a video at, by using &t=x at the end of the url (where x = seconds). Example:

http://www.youtube.com/watch?v=AYjCXjIeNJk&t=210

Now how can I use that in the markup, if I want to use the following tag:

[yt start=210]http://www...[/yt]

I know I have to use the iframe embed that Youtube has on each video, but I'm not sure if there are specific parameters for the iframe (one ones that need changing).
yeah nice hope someone can help i like ur idea as well Big Grin
Bumping this. So far, I've tried the following:

\[yt start=(.*?)\](.*?)\[/yt\]

<iframe width="560" height="315" src="$2&t=$1" frameborder="0" allowfullscreen></iframe>

Testing this code will result in the variables being printed correctly, but it does not display the video. Viewing the page source on a test page, the code is also printed correctly, but it just doesn't display.

Ideas?



EDIT: Okay, I'm making some progress. This works:

\[yt start=(.*?)\](.*?)\[/yt\]

<iframe width="560" height="315" src="//www.youtube.com/embed/$2?rel=0&start=$1" frameborder="0" allowfullscreen></iframe>

As you can see though, this only works if the user puts only the 11 character video identifier in the tags. The question now is what I can do to make it accept standard youtube URLs, as the one in the HTML is the embed code.
can u explain me how to make this work please?
dunno where to add and how it works
Think it's not possible, using iframe in posts is not really safe as far as I know.

You gotta make adjustments to the core features probably to let it work with the builtin Yt editor function
@Tankey, default MyBB youtube video parser uses <iframe> - see video_youtube_embed template.

@|FBI|MMFB, add this as new MyCode in ACP -> Configuration (square bracket code as Regular Expression, HTML as Replacement). Then use in post like other BBCodes.

@hiig, you can edit the MyBB parser (inc/class_parser.php). First this line:
			$message = preg_replace("#\[video=(.*?)\](.*?)\[/video\]#ei", "\$this->mycode_parse_video('$1', '$2');", $message);
Would have to change the regex so it contains also optional start=[youtube time format].

Then edit this function:
function mycode_parse_video($video, $url)
	{
		global $templates;
		
		if(empty($video) || empty($url))
		{
			return "[video={$video}]{$url}[/video]";
		}
		
		$parsed_url = @parse_url(urldecode($url));
		if($parsed_url == false)
		{
			return "[video={$video}]{$url}[/video]";
		}
		
		$fragments = array();
		if($parsed_url['fragment'])
		{
			$fragments = explode("&", $parsed_url['fragment']);
		}
		
		$queries = explode("&", $parsed_url['query']);
		
		$input = array();
		foreach($queries as $query)
		{
			list($key, $value) = explode("=", $query);
			$key = str_replace("amp;", "", $key);
			$input[$key] = $value;
		}
		
		$path = explode('/', $parsed_url['path']);
		
		switch($video)
		{
			case "dailymotion":
				list($id, ) = split("_", $path[2], 1); // http://www.dailymotion.com/video/fds123_title-goes-here
				break;
			case "metacafe":
				$id = $path[2]; // http://www.metacafe.com/watch/fds123/title_goes_here/
				$title = htmlspecialchars_uni($path[3]);
				break;
			case "myspacetv":
				$id = $path[4]; // http://www.myspace.com/video/fds/fds/123
				break;
			case "yahoo":
				$id = $path[1]; // http://xy.screen.yahoo.com/fds-123.html
				// Support for localized portals
				$domain = explode('.', $parsed_url['host']);
				if($domain[0] != 'screen')
				{
					$local = $domain[0].'.';
				}
				else
				{
					$local = '';
				}
				break;
			case "vimeo":
				$id = $path[1]; // http://vimeo.com/fds123
				break;
			case "youtube":
				if($fragments[0])
				{
					$id = str_replace('!v=', '', $fragments[0]); // http://www.youtube.com/watch#!v=fds123
				}
				elseif($input['v'])
				{
					$id = $input['v']; // http://www.youtube.com/watch?v=fds123
				}
				else
				{
					$id = $path[1]; // http://www.youtu.be/fds123
				}
				break;
			default:
				return "[video={$video}]{$url}[/video]";
		}

		if(empty($id))
		{
			return "[video={$video}]{$url}[/video]";
		}
		
		$id = htmlspecialchars_uni($id);
		
		eval("\$video_code = \"".$templates->get("video_{$video}_embed")."\";");
		
		return $video_code;
	}
to take the start time into consideration and in case it's youtube video, write it to variable.

And lastly edit the mentioned template and add the time variable to <iframe>.
@Destroy666,

I've just tried it myself, and I'm getting an object instead of iframe tag which are not really comparable at all as far as I'm aware.