Skip to content

Split any YouTube song into stems

TypeScript

Fix embed 150/Video unavailable: load renderer via http://localhost (YouTube rejects 127.0.0.1 origins)

Daniel Ravinabcb5eef0de7e
diff --git a/embed-test/index.html b/embed-test/index.html
new file mode 100644
index 0000000..f85612d
--- /dev/null
+++ b/embed-test/index.html
@@ -0,0 +1,40 @@
+<!doctype html>
+<html>
+<head>
+ <meta charset="utf-8" />
+</head>
+<body>
+ <div id="v" style="width: 640px; height: 360px;"></div>
+ <script>
+ const params = new URLSearchParams(location.search)
+ const useOrigin = params.get('variant') === '1'
+ window.log = (m) => console.log(m)
+
+ log('page-origin=' + location.origin)
+
+ window.onYouTubeIframeAPIReady = () => {
+ log('api-ready')
+ new window.YT.Player('v', {
+ videoId: 'dQw4w9WgXcQ',
+ width: '640',
+ height: '360',
+ playerVars: {
+ controls: 0,
+ disablekb: 1,
+ rel: 0,
+ playsinline: 1,
+ ...(useOrigin ? { origin: location.origin } : {})
+ },
+ events: {
+ onReady: () => log('ONREADY'),
+ onError: (e) => log('ONERROR code=' + e.data),
+ onStateChange: (e) => log('STATE=' + e.data)
+ }
+ })
+ }
+ const s = document.createElement('script')
+ s.src = 'https://www.youtube.com/iframe_api'
+ document.head.appendChild(s)
+ </script>
+</body>
+</html>
diff --git a/embed-test/main.js b/embed-test/main.js
new file mode 100644
index 0000000..8c3468e
--- /dev/null
+++ b/embed-test/main.js
@@ -0,0 +1,43 @@
+const { app, BrowserWindow } = require('electron')
+const { createServer } = require('http')
+const { createReadStream, existsSync, readFileSync } = require('fs')
+const { join, normalize, extname } = require('path')
+
+const ROOT = __dirname
+const MIME = { '.html': 'text/html', '.js': 'text/javascript', '.css': 'text/css' }
+
+const server = createServer((req, res) => {
+ let p = normalize(join(ROOT, decodeURIComponent((req.url || '/').split('?')[0]) === '/' ? 'index.html' : (req.url || '/').split('?')[0]))
+ if (!p.startsWith(ROOT) || !existsSync(p)) p = join(ROOT, 'index.html')
+ res.setHeader('Content-Type', MIME[extname(p)] || 'text/html')
+ createReadStream(p).pipe(res)
+})
+
+app.userAgentFallback = process.env.STRIP_ELECTRON_UA
+ ? app.userAgentFallback.replace(/\s?Electron\/[\d.]+\s?/i, '').replace(/\s?StemKit\/[\d.]+\s?/i, '')
+ : app.userAgentFallback
+
+app.whenReady().then(async () => {
+ await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve))
+ const port = server.address().port
+
+ const useLocalhostHost = process.env.HOST_NAME === 'localhost'
+ const base = useLocalhostHost ? `http://localhost:${port}` : `http://127.0.0.1:${port}`
+
+ const win = new BrowserWindow({
+ show: false,
+ webPreferences: { contextIsolation: true, nodeIntegration: false, backgroundThrottling: false }
+ })
+ win.webContents.on('console-message', (_e, _level, message) => {
+ console.log('[renderer]', message)
+ })
+ await win.loadURL(base + '/index.html?variant=' + (process.env.ORIGIN_PARAM || '0'))
+ setTimeout(() => {
+ console.log('[harness] done')
+ app.quit()
+ }, 20000)
+})
+
+process.on('exit', () => {
+ try { server.close() } catch {}
+})
diff --git a/package.json b/package.json
index b7a36cb..e3043eb 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
{
"name": "stemkit",
"productName": "StemKit",
- "version": "0.1.4",
+ "version": "0.1.5",
"description": "Local YouTube stem splitter and player",
"main": "./out/main/index.js",
"author": "daniel",
diff --git a/src/main/index.ts b/src/main/index.ts
index 2d47359..055c24b 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -55,7 +55,7 @@ function startRendererServer(): Promise<string> {
server.on('error', reject)
server.listen(0, '127.0.0.1', () => {
staticServer = server
- resolve(`http://127.0.0.1:${(server.address() as AddressInfo).port}`)
+ resolve(`http://localhost:${(server.address() as AddressInfo).port}`)
})
})
}