I'm trying to set up a Siri Shortcut on iOS to make AirPlaying a YouTube video to an Apple TV easy. As part of that process, I want to select the quality of the video (presumably using Javascript), but I'm having trouble getting that going. Here's what I've got so far (imgur link to view on desktop/non-iOS devices). That successfully uses JavaScript to pause the video, rewind it back to the beginning since YouTube autoplays, then connects to the Apple TV with AirPlay, waits for the TV to turn on, and clicks on the video to unmute it. However, everything I've tried to set the video quality hasn't worked, and I'm tearing my hair out over here. Anyone have any idea if that's possible? Is it a parameter of the video that can be set? Or can I use JavaScript to click on the settings button and pick the quality? My Javascript is very weak, so any help pointing me in the right direction would be very much appreciated.
Here's the Javascript I've got so far:
ytplayer = document.querySelector('video');
ytplayer.pause();
ytplayer.currentTime = 0;
ytplayer.click();
ytplayer.play();
pletion();
I'm trying to set up a Siri Shortcut on iOS to make AirPlaying a YouTube video to an Apple TV easy. As part of that process, I want to select the quality of the video (presumably using Javascript), but I'm having trouble getting that going. Here's what I've got so far (imgur link to view on desktop/non-iOS devices). That successfully uses JavaScript to pause the video, rewind it back to the beginning since YouTube autoplays, then connects to the Apple TV with AirPlay, waits for the TV to turn on, and clicks on the video to unmute it. However, everything I've tried to set the video quality hasn't worked, and I'm tearing my hair out over here. Anyone have any idea if that's possible? Is it a parameter of the video that can be set? Or can I use JavaScript to click on the settings button and pick the quality? My Javascript is very weak, so any help pointing me in the right direction would be very much appreciated.
Here's the Javascript I've got so far:
ytplayer = document.querySelector('video');
ytplayer.pause();
ytplayer.currentTime = 0;
ytplayer.click();
ytplayer.play();
pletion();
Share
Improve this question
asked Nov 29, 2019 at 3:38
LennonLennon
211 silver badge3 bronze badges
2 Answers
Reset to default 9I'm not sure how the Siri Shortcuts with Apple TV work, so this might not apply, but if it's a similar layout as a puter browser, this code works for me for clicking the settings button and selecting a resolution in a web browser
let sleep = ms => new Promise(r => setTimeout(r, ms));
async function waitForVideo() {
let video = document.querySelector('video');
while (!video) {
console.log('waiting for video');
await sleep(200);
video = document.querySelector('video');
}
}
/**
* Sets the quality
* options are: "Highest" and the options available in the menu ("720p", "480p", etc.)
*/
async function setQuality(quality) {
await waitForVideo();
await sleep(1000);
let settingsButton = document.querySelector('.ytp-settings-button');
settingsButton.click();
await sleep(500);
let qualityMenu = document.querySelector('.ytp-panel-menu').lastElementChild;
qualityMenu.click();
await sleep(500);
let qualityOptions = [...document.querySelectorAll('.ytp-menuitem')];
let selection;
if (quality === 'Highest') selection = qualityOptions[0];
else selection = qualityOptions.filter((el) => el.innerText == quality)[0];
if (!selection) {
let qualityTexts = qualityOptions.map((el) => el.innerText).join('\n');
console.log('"' + quality + '" not found. Options are: \n\nHighest\n' + qualityTexts);
settingsButton.click(); // click the menu button to close it
return;
}
selection.click();
}
setQuality('Highest');
You will need to use puppeteer:
This an example that you can use:
`
const puppeteer = require('puppeteer')
async function start() {
const site1 = "https://ampdemo.azureedge/azuremediaplayer.html?url=%2F%2Famssamples.streaming.mediaservices.windows%2F49b57c87-f5f3-48b3-ba22-c55cfdffa9cb%2FSintel.ism%2Fmanifest&muted=true&aes=true"
const tabs_no = 4
const time_interval = 30 * 1000 // in ms, time between 2 consequetive tabs opening
const vid_length = 5 * 60 * 1000 // in ms
const path_chromium76 = '/usr/bin/chromium-browser' // recent install, v. 76.0.3809.100 (official Build) Built on Ubuntu, running on LinuxMint 18.1
const launch_args = {
headless:false,
executablePath:path_chromium76,
devtools:false,
timeout: 0,
}
const selector1 = "#azuremediaplayer > div > div.vjs-control-bar > div.amp-controlbaricons-right > div.amp-quality-control.vjs-menu-button.vjs-menu-button-popup.vjs-control.vjs-button.outline-enabled-control.quality-4 > div > ul > li:nth-child(3)"
const selector2 = 'li[aria-label="816p-6Mbps"]'
const selector3 = "li[aria-label='816p-6Mbps']"
const selector4 = "li.vjs-menu-item.amp-menu-item[aria-label='816p-6Mbps']"
const selector5 = "li.vjs-menu-item.amp-menu-item"
const selector10 = 'li.vjs-menu-item.amp-menu-item'
const sel = selector3
const browser = await puppeteer.launch(launch_args)
// const version = await browser.version() // chrome/77.0.3844.0 (shipped with puppeteer)
const incognito_context = await browser.createIncognitoBrowserContext()
for (i=0; i<tabs_no; i++) {
let page = await incognito_context.newPage()
await page.goto(site1)
await page.waitFor(3000)
page.evaluate((sel) => {
const el = document.querySelector(sel)
el.style.color = 'red'
el.click()
//return el
}, sel)
// Wait before going to the next tab (i.e. for continuing with the for loop)
await page.waitFor(time_interval)
//------------WORKS
setTimeout( ()=> page.close(),vid_length)
//----------*/
if (i == tabs_no-1) {
setTimeout( ()=> browser.close(), vid_length+3000)
//setTimeout( ()=> incognito_context.close(), vid_length+3000)
}
} // End of for loop for tabs
}
async function main() {
const browsers_no = 2
for (b=0; b<browsers_no; b++) {
setTimeout( start, 1000)
}
}
main()'