electron: [Bug]: video files not seekable with protocol.handle
Preflight Checklist
- I have read the Contributing Guidelines for this project.
- I agree to follow the Code of Conduct that this project adheres to.
- I have searched the issue tracker for a bug report that matches the one I want to file, without success.
Electron Version
25.1.0
What operating system are you using?
Other Linux
Operating System Version
Linux Mint 21.1
What arch are you using?
x64
Last Known Working Electron version
No response
Expected Behavior
I’m using a custom protocol in place of file:// URLs for a video player. This works:
protocol.registerFileProtocol("video", (request, callback) => {
callback(request.url.slice("video://".length)) // simplified path for this example
})
Since protocol.registerFileProtocol got deprecated in version 25, I tried the suggested alternative protocol.handle as per the update notes:
protocol.handle("video", (request) => {
return net.fetch(request.url.replace(/^video:/, "file:")) // simplified path
})
This allows me to play videos. However now when I set .currentTimeon a <video> element with a video://… source, it just jumps back to zero. video.seekable.end() is always 0. This breaks seeking in HTML5 videos with custom protocol URLs.
I’ve also tried this, with the same result:
protocol.handle("video", (request) => {
return new Response(fs.readFileSync(request.url.replace(/^video:/, "file:")))
})
Not sure if it matters, the scheme is registered like this:
protocol.registerSchemesAsPrivileged([
{
scheme: "video",
privileges: {
secure: true,
bypassCSP: true,
stream: true,
},
},
])
Expected behavior: The documented alternative to protocol.registerFileProtocol should allow serving seekable video files.
Actual Behavior
See above.
Testcase Gist URL
No response
Additional Information
No response
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 16
- Comments: 63 (3 by maintainers)
Commits related to this issue
- Prune Refs: - https://www.electronjs.org/docs/latest/api/protocol - https://github.com/electron/electron/issues/39031 - https://github.com/electron/electron/issues/38749 — committed to ente-io/ente by mnvr 3 years ago
- Desktop: Resolves #10424: Move the note viewer to a separate process This commit moves Joplin's markdown note viewer to a separate process, preventing Electron's rerendering from blocking the UI. Be... — committed to personalizedrefrigerator/joplin by personalizedrefrigerator 2 years ago
Be sure that the
Content-LengthandContent-Rangeheaders in the response are correct. If I recall correctly, our implementation (available here under the AGPLv3), originally had an off-by-one error.Here is my solution. It works fine.
In older versions of Electron, it was possible to seek by specifying a local file path directly to an HTMLMediaElement.
I understand that
protocol.registerSchemesAsPrivilegedis for security improvement. If Electron wants to promote this feature, it would be good to have an official sample that is equivalent to the version where the local file path was specified directly.Or, when
request.headersis specified as an option fornet.fetch, I would like it to generate a response that takesRangeinto account if there is one.@jonmajorc are you able to SEEK the video?
So, before v34, this kind of wasn’t an issue, assuming
registerFileProtocolhad a different implementation behind the scenes thanprotocol.handle. Now with v34, that seems to have changed. So that workaround of still usingregisterFileProtocoldoesn’t work anymore, so it’s actually irrelevant to still using it and we can rather try to solve it withprotocol.handle.Now we tried the solution from https://github.com/electron/electron/issues/38749#issuecomment-2585189055 but we don’t really get any difference. Basically, we want to let videos autoplay and loop while Electron is serving the file via a protocol handler. We currently don’t see how we make this possible in Electron v34+. At least, on specific Linux based systems that doesn’t work. On a macOS build of the application, using the cache-control header to disabale the cache too for the video stream, seems to kind of work. But only for macOS so far.
Might somebody be able to provide a fully tested solution that works for them, including any workarounds with converting streams?
In testing further, I’ve found that my example does work on v36.7.2, but it does not work on v37.0.0.
I’ve also confirmed it does not work on v38.0.0-alpha.7 or v39.0.0-nightly.20250718 .
I’ve narrowed this further to v37.0.0-nightly.20250422 working and v37.0.0-nightly.20250423 not working, so I believe this issue started in one of these two commits
dd03cceda0384f62540b0cea650e73b091eab34b (more likely this one as it bumps Chromium) 686ae476963ed138135bc5f2934addcd1cc1e4ac
I’m struggling to figure out how I could bisect Electron and/or Chromium to further narrow this. What I’d like to be able to do is compile electron at commit dd03cceda0384f62540b0cea650e73b091eab34b and see if it works, and if it doesn’t, start stepping through the Chromium commits between when there were previously bumped and this commit. I’ve compiled Electron at it’s latest commit but haven’t gotten past that yet.
Edit: Someone else actually already found these commits here https://github.com/electron/electron/issues/47661 and there’s an open PR to fix it here https://github.com/electron/electron/pull/47703
Sorry, but this idea has been proposed before. It works well with electron 35, but not with electron 36 or electron 37.
It might be necessary to convert the
fsread stream to a web-compatible stream (see the implementation linked in https://github.com/electron/electron/issues/38749#issuecomment-2313762452).@cxw2299 When serving large files like videos or audio, browsers or media players often request partial data segments for:
This code handles the Range header from the client to send only the required portion of the file.
what is confusing to me – why would
registerFileProtocolwork for streaming large files? Shouldn’t we be usingregisterStreamProtocol? Also, @personalizedrefrigerator has so much custom code, e.g. request with Range header handling. DoesregisterFileProtocolinternally also handle requests with Range headers? If that’s the case, could that be in an utility mehtod so that we could use inprotocol.handle?Hello! I have the same problem on Windows. Video files prepared using
protocol.handlecannot be seeked.Electron: 25.1.0 OS: Windows 11 Pro x64 10.0.22621 Build 22621
Fiddle: https://gist.github.com/takumus/7b6d73df884df009f36bc6ff1979d5ff
This text was translated by Deepl.
Thank you for investigating 😃 Great findings. I’m hoping hard that the proposed fix (in the PR you linked to) will be merged asap to address the regression, otherwise we’ll have to rollback our app to Electron v36 (or modify our architecture to serve audio/video media resources via a local HTTP server, which comes with significant tradeoffs in our case).
@blurymind Here’s my minimal project https://github.com/agsimmons/electron-video-playback-example
Clone the repo, cd into it, run
npm install, thennpm run dev. It plays back the example_video.mp4 video in the root of the project.I was finally able to get this working with protocol.handle . Autoplay, seeking, and looping are all working for me on both Linux and Windows.
standardprivilege; I was having issues without itstreamprivilege, but the docs seem to suggest to use itRangeheader, and return the correct range of the file from diskContent-Lengthheader for the given rangeAccept-Ranges: bytesto state that the handler understands range requestsI can’t share my code directly, but I hope this is helpful
@blurymind on my front its just
Ok none of these examples here make it seekable for me. I was thinking its because its not piping the stream like theexpress example (which btw works) https://github.com/bootstrapping-microservices/video-streaming-example/blob/master/index.js I tried to do
fs.createReadStream(path).pipe(res);but the response from the example which nodejs uses is not compatible with that code at all and couldnt find any clear example of how to pipe the video stream with nodeYou are loading the entire file to a Blob, which might bloat the memory, not recommended for large files
Brothers!i find the loadURL on electron must take a long time to show a win,have someting to deal this?
in the renderer
@renestalder I have the exact same behavior. Intermittent playing on Mac &
registerFileProtocoldoesn’t work either. Difficult to debug for sure. Hoping to get some time Sunday or early next week to give it another go. Frustrating.Will post here if I get it figured out before someone else.
Thanks, that is what I have now and still having issues. Something along the lines of that
nodeStreamToWebis still required as the node double controller close interaction still exists, for those following along.Struggling to get this work in Electron v34.2.0.
<video />elements load the src intermittently, but then cannot be played. For some reason, toggling on ‘autoPlay’ seems to make it more likely that the video will load.@dev-techfago how were you able to force a
Responseto accept a rawcreateReadStream?agree with @6zz seem like a bug in electron, maybe there is a way to fallback to some Electron.ProtocolResponse kinda like registerFileProtocol does.
+1, I’m also switching back to
registerFileProtocolThings I tried:
protocol.handlewithnet.fetch: Loading a 2GB local video takes 10+ secondsprotocol.handlewithcreateReadStream(filePath): same resultprotocol.handlewithcreateReadStream(filePath)+ range headers: completely broke it, the video never loadsThese were all with
stream: truein the scheme privileges. I wonder if this is related to #41032 or #40447.On the other hand
registerFileProtocolloads instantly, matching the same performance as using an<input type="file">withcreateObjectURL(). Even the largest videos come through at disk read speed.Any update on this? Seems to have affected a lot of video & music players built on electron. Have been using the depreciated handlers but as @vincaslt said it triggers a false-positive virus warning in windows.
The old handler is now also throwing these errors:
[50325:0701/153404.655428:ERROR:ffmpeg_common.cc(959)] Unsupported pixel format: -1This still seems to be an issue with Electron 31.0.0.
Still an issue. Also, similar as to this comment from 2021t, the issue only affects the first load of a video item. I.e. if we close and re-open the video, then suddenly seeking does work.
Affects plain audio files, as well. Present on Mac and Windows (haven’t tested Linux yet, but if someone wants, just ask)
Workaround is staying on the deprecated API for the time being.
@philer the fiddle by @takumus is what i was looking for! i can try to look into this more but
@nornagon(who is off work at the moment - back midsummer) may be a better person to investigate depending on how far i get.