Trying to create a YouTube code with start time and optional end time

Get help developing custom BBCodes or request one.
Post Reply
Bruce Banner
Registered User
Posts: 834
Joined: Thu Sep 25, 2014 10:36 am

Trying to create a YouTube code with start time and optional end time

Post by Bruce Banner »

and get my head around XSL in the process. Here's what I have so far.

BBCode usage,

Code: Select all

[YouTubeSE end={NUMBER2?}]https://www.youtube.com/watch?v={SIMPLETEXT} {NUMBER1}[/YouTubeSE]
HTML replacement

Code: Select all

<xsl:choose>
	<xsl:when test="@end">
<center><div class="container">
 <iframe class="responsive-iframe" src="https://www.youtube.com/embed/{SIMPLETEXT}?start={NUMBER1}&end={NUMBER2}">
</iframe>
</div></center>
	</xsl:when>
	<xsl:otherwise>
<center><div class="container">
 <iframe class="responsive-iframe" src="https://www.youtube.com/embed/{SIMPLETEXT}?start={NUMBER1}">
</iframe>
</div></center>
	</xsl:otherwise>
</xsl:choose>
Works absolutely fine if I only enter the start time. If I don't bother with the XSL stuff and make the start time and end time compulsory, it works fine then too, as long as the BBCode usage box says,

Code: Select all

[YouTubeSE}]https://www.youtube.com/watch?v={SIMPLETEXT} {NUMBER1} {NUMBER2}[/YouTubeSE]
and the HTML replacement box says,

Code: Select all

<center><div class="container">
 <iframe class="responsive-iframe" src="https://www.youtube.com/embed/{SIMPLETEXT}?start={NUMBER1}&end={NUMBER2}">
</iframe>
</div></center>
But I can't figure out how to get it to work the way I want. If someone enters 10 and 20, say, the video starts playing at 0:10 and stops at 0:20, but if someone doesn't bother to enter an end time, the video starts at 0:10 and just goes on until the end. With all the XSL code above in place and the BBVode usage box reading as it does at the top of the post, like I said, if I enter a start time, it plays from there fine and goes on until the end, but if I enter an end time too, the video appears blank, just black. And if I hit the play button, I get a grey YouTube error message.
User avatar
Lumpy Burgertushie
Registered User
Posts: 68294
Joined: Mon May 02, 2005 3:11 am
Contact:

Re: Trying to create a YouTube code with start time and optional end time

Post by Lumpy Burgertushie »

make sure that your url actually works in the browser the way you are using it in the bbcode.

robert
I'm baaaaaccckkkk. still doing work on donation basis. PM your needs.

Premium phpBB 3.3 Styles by PlanetStyles.net

If nobody is in the forest, does a tree really fall?
User avatar
EA117
Registered User
Posts: 1858
Joined: Wed Aug 15, 2018 3:23 am
Contact:

Re: Trying to create a YouTube code with start time and optional end time

Post by EA117 »

Bruce Banner wrote:
Thu Nov 19, 2020 2:18 am
With all the XSL code above in place and the BBVode usage box reading as it does at the top of the post, like I said, if I enter a start time, it plays from there fine and goes on until the end, but if I enter an end time too, the video appears blank, just black. And if I hit the play button, I get a grey YouTube error message.
Your proposed code seemed to work fine when tested here. I entered both a start time and an end time, to create the stated problem scenario:

[YouTubeSE end=100]https://www.youtube.com/watch?v=cMz_sTgoydQ 49[/YouTubeSE]

And this rendered into the message as:

Code: Select all

<center>
  <div class="container">
    <iframe class="responsive-iframe" src="https://www.youtube.com/embed/cMz_sTgoydQ?start=49&end=100"></iframe>
  </div>
</center>
And at least on my Chrome 86.0.4240.198, that works. No blank embedding, no error. The video preview thumbnail displays, and playing the video starts and stops at the specified time.

Maybe take a look either in "View Page Source" or F12 Elements to see what it's actually rendering as in your failure case, to verify whether something is actually amiss about how the URL or parameters were constructed.
Bruce Banner
Registered User
Posts: 834
Joined: Thu Sep 25, 2014 10:36 am

Re: Trying to create a YouTube code with start time and optional end time

Post by Bruce Banner »

EA117 wrote:
Thu Nov 19, 2020 5:01 pm
Your proposed code...
Thanks! Turns out I was just being stupid. I was putting the end time in the wrong place. lol Instead of

Code: Select all

[YouTubeSE end=100]https://www.youtube.com/watch?v=cMz_sTgoydQ 49[/YouTubeSE]
I was entering

Code: Select all

[YouTubeSE]https://www.youtube.com/watch?v=cMz_sTgoydQ 49 100[/YouTubeSE]
This was the first time I succeeded in creating a BBCode with options, and without actually understanding anything I was doing, so I just ****ed up. Turns out you're right and the code I laid out in the first post does work perfectly, apart from that example at the bottom of the post which I said worked fine if the start and end times are both compulsory. Turns out there's a small typo, a character I failed to delete. Should read like this

Code: Select all

[YouTubeSE]https://www.youtube.com/watch?v={SIMPLETEXT} {NUMBER1} {NUMBER2}[/YouTubeSE]

Code: Select all

<center><div class="container">
 <iframe class="responsive-iframe" src="https://www.youtube.com/embed/{SIMPLETEXT}?start={NUMBER1}&end={NUMBER2}">
</iframe>
</div></center>
Bruce Banner
Registered User
Posts: 834
Joined: Thu Sep 25, 2014 10:36 am

Re: Trying to create a YouTube code with start time and optional end time

Post by Bruce Banner »

I'm having a problem now trying to work out how to deal with the different YouTube URL formats. The embed code is the same no matter what but how do I allow people to enter either https://www.youtube.com/watch?v=MvXQDnTCVC0 or https://youtu.be/MvXQDnTCVC0? The first ya see in a browser's address bar but the second ya see if ya right-click the video and select "Copy video URL". The main thing they have in common is the

Code: Select all

MvXQDnTCVC0
bit, but regardless of whichever version of the URL the user may enter, the embed code always contains https://www.youtube.com/embed/MvXQDnTCVC0, which makes things really difficult. If the embed code just contained the full URL, I could just put {URL} in the BBCode usage box, but my BBCode usage box at the moment reads,

Code: Select all

[YouTubeSE end={NUMBER2?}]https://www.youtube.com/watch?v={SIMPLETEXT} {NUMBER1}[/YouTubeSE]
and in my HTML replacement box, I have https://www.youtube.com/embed/{SIMPLETEXT}. What should I do to allow someone to enter the URL in the form https://youtu.be/{SIMPLETEXT}?
Post Reply

Return to “Custom BBCode Development and Requests”