Can We Loop a YouTube Video Between Time Frames Using Keyboard Maestro?

Can we use a macro to loop between time A and time B on a YouTube video webpage? For example, Loop between 5s and 13s of this YouTube video, which video id is 5AwdkGKmZ0I.

I've found a javascript snippet that allows us to set up the starting time and the ending time to loop on this video. It only needs us to fill in 3 things: starting seconds, ending seconds, and a YouTube video ID👇🏽

I wonder if we can use a macro starting like this to achieve the looping function on a YouTube page. A macro may look like this :point_down:t4:
Loop AB for a YouTube video.kmmacros (v10.2) (4.6 KB)

Here's a start, which plays it in a Custom HTML Prompt:

YouTube Video Loop Test.kmmacros (3.3 KB)

For the JavaScript, I just pasted the code in your link into this HTML. The only thing I changed was this

var section = {
  start: 0,
  end: 64

to this, which gets the values from KM variables:

var section = {
	start: document.kmvar.Local_start,
	end: document.kmvar.Local_end

I don't have a lot of time to spend fleshing this out, so you're pretty much on your own. But if you have questions, go ahead and ask. I just might say "I don't have the time".


Hi Dan! Thank you for your time and kind help! :grinning: I've run your macro on my Mac. It popped up a player and looped the video well. But before playing, it always pops up two windows like this :point_right:
And it wouldn't go away when choosing an app like Google Chrome, IINA video player or QuickTime Player to open the video. PS: My mac version is Monterey 12.6; Keyboard Maestro V10.2.

Besides, can we achieve an AB loop on a real YouTube page? Like a webpage opened on a Google Chrome browser? That's why I uploaded this macro :point_down:t4:

Hi @Alice_Shi - this problem is quite well known. To get rid of the pop up visit GitHub and get the solution!

It works for me but I guess YMMV. Let us know how you get on.


Thanks - I thought I remembered something like that, except... I couldn't remember what it was, if that makes sense.

No idea. Sorry.

1 Like

It works on my computer. The pop-up window goes away. Thank you tiffle! :grinning:


Hi Dan! This is a macro based on your code before.▼
YouTube Video Loop Test 2023-01-26.kmmacros (v10.2) (4.3 KB)

It allowed me to change the start_seconds and end_seconds through Keyboard Maestro to loop the video. But the video_id didn't work. So what's the problem here, may I ask? It looks like something needs to be fixed. The player doesn't recognize the video_id that I assigned to it on line 29▼

videoId: 'document.kmvar.video_id',

Capitalization matters, in Javascript. "Video_id" is different than "video_id".

Sorry, that was a typo in the reply. It doesn't have this case issue in the real macro here. :point_down:t4:

If you actually put it in quotes, then that's your problem. It should be:

videoId: document.kmvar.video_id,

When it's in quotes, it's a string, not a variable. Sort of like when you paste in the name of a variable in KM, when you meant %Variable%MyVariable%

I hope that makes sense.

1 Like

It works now! Thank you for your advice and explanation. :grin:

1 Like