Skip to content

armgit / my-yt

Public Imported from GitHub
0

A clean and minimal youtube frontend, without all the ads and whistles

JavaScript

feat: enable/disable transcoding of videos in the settings page

Christian Feic19c5250297b
diff --git a/README.md b/README.md
index 137c1c5..9db34a7 100644
--- a/README.md
+++ b/README.md
@@ -116,13 +116,13 @@ docker run -e AI_MODEL=claude-xyz -e AI_HOST=https://api.anthropic.com -e AI_END
-## Environment variable to skip transcoding to h264
+## Skip transcoding to h264
If your system is under stress when downloading a video, that could be because of the automatic transcoding that's happening behind the scenes.
This is a feature to make the video compatible with most devices (especially iOS)
-If you don't have the need, you can skip the transcoding process by setting `SKIP_TRANSCODING=true`.
+If you don't have the need, you can skip the transcoding process in the settings page.
diff --git a/client/components/transcode-videos-form.js b/client/components/transcode-videos-form.js
new file mode 100644
index 0000000..61349bf
--- /dev/null
+++ b/client/components/transcode-videos-form.js
@@ -0,0 +1,49 @@
+class TranscodeVideosForm extends HTMLElement {
+ constructor () {
+ super()
+ }
+ connectedCallback () {
+ this.render()
+ this.registerEvents()
+
+ fetch('/api/transcode-videos')
+ .then(response => response.json())
+ .then((transcodeVideos) => {
+ this.querySelector('#transcode-videos').checked = transcodeVideos
+ })
+ .catch(error => console.error('Error:', error))
+ }
+ disconnectedCallback () {
+ this.unregisterEvents()
+ }
+ registerEvents () {
+ this.querySelector('#transcode-videos').addEventListener('change', this.setTranscodeVideosHandler.bind(this))
+ }
+ unregisterEvents () {
+ this.querySelector('#transcode-videos').removeEventListener('change', this.setTranscodeVideosHandler.bind(this))
+ }
+ render () {
+ this.innerHTML = /*html*/`
+ <form>
+ <div class="flex space-between">
+ <div>
+ <label for="transcode-videos">Transcode videos to h264</label>
+ </div>
+ <input type="checkbox" id="transcode-videos"/>
+ </div>
+ <div><small>If you're having issues playing back videos, this can help to maximize compatibility across devices</small></div>
+ <div><small>The download of videos will be slower, because transcoding is quite resource intensive</small></div>
+ </form>
+ `
+ }
+
+ setTranscodeVideosHandler (event) {
+ event.preventDefault()
+ fetch('/api/transcode-videos', {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify(event.target.checked)
+ })
+ }
+}
+customElements.define('transcode-videos-form', TranscodeVideosForm)
diff --git a/client/index.html b/client/index.html
index 32187fb..4d56300 100644
--- a/client/index.html
+++ b/client/index.html
@@ -56,6 +56,8 @@
<br>
<manage-disk-space-form></manage-disk-space-form>
<br>
+ <transcode-videos-form></transcode-videos-form>
+ <br>
<form>
<label for="show-thumbnails">
Show video thumbnails
@@ -114,6 +116,7 @@
<script type="module" src="/components/manage-channels-form.js"></script>
<script type="module" src="/components/video-quality-form.js"></script>
<script type="module" src="/components/manage-disk-space-form.js"></script>
+ <script type="module" src="/components/transcode-videos-form.js"></script>
<script type="module" src="/main.js"></script>
<script type="module" src="/lib/router.js"></script>
</body>
diff --git a/lib/repository.js b/lib/repository.js
index fda88f5..f14ffb1 100644
--- a/lib/repository.js
+++ b/lib/repository.js
@@ -19,9 +19,12 @@ export default class Repository {
fs.writeFileSync(this.paths.channels, '[]')
}
if (!fs.existsSync(this.paths.settings)) {
- fs.writeFileSync(this.paths.settings, JSON.stringify({"videoQuality":720}, null, 2))
+ fs.writeFileSync(this.paths.settings, JSON.stringify({
+ videoQuality: 720,
+ transcodeVideos: true
+ }, null, 2))
}
-
+
try {
this.videos = JSON.parse(fs.readFileSync(this.paths.videos))
this.channels = JSON.parse(fs.readFileSync(this.paths.channels))
@@ -29,6 +32,7 @@ export default class Repository {
} catch (err) {
console.error(err)
}
+ if (this.settings.transcodeVideos === undefined) { this.setTranscodeVideos(true) }
fs.readdirSync(`${this.basePath}/videos`).forEach(file => {
const format = file.substring(file.lastIndexOf('.') + 1)
@@ -166,4 +170,12 @@ export default class Repository {
}
return this.settings.videoQuality
}
+ getTranscodeVideos() {
+ return this.settings.transcodeVideos
+ }
+ setTranscodeVideos(value) {
+ this.settings.transcodeVideos = !!value
+ this.saveSettings()
+ return this.settings.transcodeVideos
+ }
}
\ No newline at end of file
diff --git a/lib/router/api.js b/lib/router/api.js
index 86a979c..a8c8b7e 100644
--- a/lib/router/api.js
+++ b/lib/router/api.js
@@ -49,6 +49,10 @@ export default function apiHandler(req, res, repo, connections = [], state = {})
return diskUsageHandler(req, res, repo, connections)
if (url.pathname === '/api/reclaim-disk-space' && req.method === 'POST')
return reclaimDiskSpaceHandler(req, res, repo, connections)
+ if (url.pathname === '/api/transcode-videos' && req.method === 'GET')
+ return getTranscodeVideosHandler(req, res, repo, connections)
+ if (url.pathname === '/api/transcode-videos' && req.method === 'POST')
+ return setTranscodeVideosHandler(req, res, repo, connections)
if (url.pathname.match(/\/api\/videos\/.*/) && req.method === 'GET')
return watchVideoHandler(req, res, repo)
if (url.pathname.match(/\/api\/captions\/.*/) && req.method === 'GET')
@@ -252,6 +256,20 @@ async function reclaimDiskSpaceHandler(req, res, repo) {
res.end()
}
+async function getTranscodeVideosHandler(req, res, repo) {
+ let transcodeVideos = repo.getTranscodeVideos()
+ res.writeHead(200, { 'Content-Type': 'application/json' })
+ res.end(JSON.stringify(transcodeVideos))
+}
+async function setTranscodeVideosHandler(req, res, repo) {
+ const body = await getBody(req)
+ const transcodeVideos = JSON.parse(body)
+
+ repo.setTranscodeVideos(transcodeVideos)
+ res.writeHead(200)
+ res.end()
+}
+
function watchVideoHandler(req, res, repo) {
const id = req.url.replace('/api/videos/', '').replace(/\.(webm|mp4)$/, '')
const video = repo.getVideo(id)
diff --git a/lib/router/app.js b/lib/router/app.js
index 57cc0db..eebb5b1 100644
--- a/lib/router/app.js
+++ b/lib/router/app.js
@@ -26,6 +26,8 @@ export default function apiHandler(req, res) {
return fileHandler('client/components/video-quality-form.js', 'application/javascript')(req, res)
if (url.pathname === '/components/manage-disk-space-form.js')
return fileHandler('client/components/manage-disk-space-form.js', 'application/javascript')(req, res)
+ if (url.pathname === '/components/transcode-videos-form.js')
+ return fileHandler('client/components/transcode-videos-form.js', 'application/javascript')(req, res)
if (url.pathname === '/components/search-videos.js')
return fileHandler('client/components/search-videos.js', 'application/javascript')(req, res)
if (url.pathname === '/components/channels-list.js')
diff --git a/lib/youtube.js b/lib/youtube.js
index d0535f1..a4bb273 100644
--- a/lib/youtube.js
+++ b/lib/youtube.js
@@ -35,7 +35,7 @@ export async function downloadVideo(id, repo, callback = () => {}) {
}
const video = repo.updateVideo(id, {location, format})
- if (process.env.SKIP_TRANSCODING !== 'true' && video.location && video.format) {
+ if (repo.getTranscodeVideos() && video.location && video.format) {
for await (const line of ffmpeg.transcode(video.location, video.format)) {
console.log(line)
callback(line)
diff --git a/test/repository.test.js b/test/repository.test.js
index 900df43..51f0185 100644
--- a/test/repository.test.js
+++ b/test/repository.test.js
@@ -114,6 +114,15 @@ describe('repository', () => {
assert.ok(repo.setVideoQuality(1080))
assert.equal(repo.getVideoQuality(), 1080)
})
+ test('gets transcode videos setting', () => {
+ assert.equal(repo.getTranscodeVideos(), true)
+ })
+ test('sets transcode videos setting', () => {
+ repo.setTranscodeVideos(false)
+ assert.equal(repo.getTranscodeVideos(), false)
+ repo.setTranscodeVideos(true)
+ assert.equal(repo.getTranscodeVideos(), true)
+ })
})
describe('search', () => {