Page 1 of 1

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

Posted: Thu Nov 19, 2020 2:18 am
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.

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

Posted: Thu Nov 19, 2020 2:41 pm
by Lumpy Burgertushie
make sure that your url actually works in the browser the way you are using it in the bbcode.

robert

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

Posted: Thu Nov 19, 2020 5:01 pm
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.

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

Posted: Fri Nov 20, 2020 12:00 pm
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>

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

Posted: Sat Nov 21, 2020 4:13 pm
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}?

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

Posted: Tue Dec 01, 2020 2:13 pm
by EA117
Indeed, the default way BBCodes work can make even the difference between "https://youtube.com" and "http://youtube.com" get declared as "not the URL this BBCode was expecting to match." Making the BBCode user-unfriendly and making them paste just the video ID (rather than pasting the entire URL) would be one solution, but presumably not a good solution.

I'm sure there is some conditional parsing that might be possible here, but I don't know it. We can see modules like /vendor/s9e/text-formatter/src/Plugins/MediaEmbed/ dealing with all the various YouTube URL domains and parameter formations, but I'm not sure how to translate that into "the best way to setup your own BBCode that could handle this automagically."

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

Posted: Tue Dec 01, 2020 2:25 pm
by Bruce Banner
EA117 wrote:
Tue Dec 01, 2020 2:13 pm
Indeed, the default way BBCodes work can make even the difference between "https://youtube.com" and "http://youtube.com" get declared as "not the URL this BBCode was expecting to match." Making the BBCode user-unfriendly and making them paste just the video ID (rather than pasting the entire URL) would be one solution, but presumably not a good solution.

I'm sure there is some conditional parsing that might be possible here, but I don't know it. We can see modules like /vendor/s9e/text-formatter/src/Plugins/MediaEmbed/ dealing with all the various YouTube URL domains and parameter formations, but I'm not sure how to translate that into "the best way to setup your own BBCode that could handle this automagically."
Thanks. :) It's looking like I'll probably have to settle for making people enter just the video ID then. It'd probably just be way too complicated to allow them to just paste an entire URL in different forms. Pity but c'est la vie.