Repository navigation
Client-side media: Add video transcoding to web-safe formats - #79375
adamsilverstein wants to merge 44 commits into
Conversation
|
Size Change: +130 kB (+1.65%) Total Size: 8.05 MB 📦 View Changed
|
|
Flaky tests detected in 34667e1. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/33114972868 three users concurrently edit a large post with diverse blocks in
|
Detect uploaded videos that are not web-safe (non-MP4/WebM container, non-web-safe codec, oversized, or over a bitrate budget) and transcode them to MP4/H.264 (or WebM/VP9) in the browser before they are served, mirroring the GIF-to-video flow. The original upload is preserved as the attachment (consistent with how WordPress keeps the `-scaled` and HEIC originals); the transcoded, web-safe version is sideloaded as a companion file and is what plays. A `videoKeepOriginal` setting flips the pipeline to replace-primary so only the optimized file is stored. Transcoding runs off the main thread in the existing @wordpress/video-conversion worker via mediabunny's Conversion API, with hardware acceleration where available and graceful fallback to the original when the browser cannot encode. Video encoding shares the single-concurrency WebCodecs gate with GIF conversion. Part of #76756. Implements #79363.
Add server-side support for the `optimized-video` sideload size: the transcoded, web-safe video is stored as a companion file recorded in attachment metadata under `optimized_video` (surfaced to the editor via media_details), the original stays the attachment, and the companion is cleaned up on delete_attachment. Add the `gutenberg_video_transcoding_keep_original` filter (default true). When a developer returns false, the resolved value is exposed as window.__videoTranscodingKeepOriginal and read by the block editor's media upload settings, flipping the pipeline to transcode-before-upload so only the optimized file is stored. Part of #76756. Implements #79363.
The setting is read from the REST index into the block editor settings, but the base entity only requests an explicit _fields list, so it was never returned. Add it to the entity fields and the matching preload path.
…ideo upload. Add server-side support for the client-side video transcoding flow. When client-side media processing is enabled, the editor transcodes an uploaded video that is not already web-safe to MP4/WebM in the browser and sideloads the result as a companion of the original video attachment, which remains the attachment itself. - Accept a video attachment as the sideload parent for the new `optimized_video` companion size, and only for that size; every other size still requires an image or PDF parent. - Record the companion under the `optimized_video` attachment metadata key on finalize, and treat it as a name the request is allowed to store. - Delete the companion file alongside the attachment in `wp_delete_attachment_files()`. - Expose the new `wp_video_transcoding_keep_original` filter (default `true`) on the REST API index as `video_keep_original`, so the editor knows whether to keep the original upload or transcode before uploading, and preload it with the other media processing settings. See WordPress/gutenberg#79375.
|
Core backport: WordPress/wordpress-develop#13323 (Trac https://core.trac.wordpress.org/ticket/65998) Claude prepared the backport and pushed two small fixes here along the way:
|
|
Thanks for summarizing, Adam! It's been a while since I've looked at this, so just sharing some thoughts after having not looked at it for a while. Please know that I think as a long-term goal being able to support video conversion is a great idea, so my thoughts here are in the spirit of supporting the effort! Now, onto the approach. I think there are a lot of assumptions here, and this problem space is more complex than a single PR can capture succinctly. Transcoding can take a lot of time, and depending on the computer, is computationally expensive. Will it slow down the editor for some users while it's happening? What if a user saves their post and closes the window, or closes their laptop lid. Where will they be in the transcoding process? What if they're in the site editor and switch to a different view from the post they were working on? What is the baseline configuration that we should use for a single video? How can a user preview what it looks like and make choices about how they want it to look? What if the video is too compressed, or not compressed enough? The differences for an individual image can be marginal, but on a bigger video can be significant. And what about audio codecs? What if the video looks fine, but an indie artist's music video doesn't sound right? If we're using the current image pipeline as an example, users can currently choose from a range of sub-sizes or select "full" to show the original, after the conversion has happened. This is easy to do because generating sub-sizes for images is quick, so the UI to make that selection can kind of happen after the conversion has happened. Whereas transcoding video is typically slow. All of this is why I'm skeptical of transcoding behind the scenes being the right path for this feature. Especially if one of the goals is to ultimately not upload the original file for the case of very large media. It isn't clear to me in the linked issue (#79363) if the goal is to solve the storage problem or not, as it's used as one of the reasons we should do this feature, but it's also mentioned that we're preserving the file that the user uploaded. There are good arguments either way, but in its current form, I wasn't sure where we're trying to land. So, my recommendation here would be to consider (and maybe this is more a discussion for the linked issue): what's the best UX we'd like to provide a user for this feature? Unlike client-side image processing which can fall back to a server-side approach, this is a new feature with no fallback. Once it's announced "WordPress can automatically convert your videos", we're committed to supporting it, so there's a bit less wriggle room IMO than for the still image processing. Alrighty, those are my first impressions. I think overall it's a good feature idea, but also a project in and of itself to make it feel seamless and polished. Would it be worth introducing it behind an experiment to begin with so that many of these questions can be explored after the initial foundations have landed? I like asking all these questions, but I'm sure there's much of this PR that is infrastructural that doesn't needed to be blocked by thinking through all the particulars. (I.e. I'm trying to figure out how I can best be supportive here without needlessly blocking good work!) |
…g-mediabunny # Conflicts: # packages/core-data/CHANGELOG.md # packages/editor/CHANGELOG.md # packages/upload-media/CHANGELOG.md # packages/upload-media/src/store/test/actions.ts # packages/upload-media/src/store/test/private-actions.js # packages/video-conversion/CHANGELOG.md
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the 📦 Bundle sizeSize Change: +129 kB (+1.48%) Total Size: 8.86 MB 📦 View Changed
⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
🏁 Flaky testsSome tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information. As a user I want to be able to add multiple close buttons so that users can close the overlay from different positions in
|
Vitest runs with `globals: false`, so the test helpers have to be imported rather than picked up from the environment as they were under Jest. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RtZdDWdC33cNHfY4U238A5
…g-mediabunny # Conflicts: # packages/core-data/CHANGELOG.md
`cancelItem` reports through the console when the item carries no onError handler, so the concurrency-gate test has to expect that rather than let it surface as an unexpected console call. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RtZdDWdC33cNHfY4U238A5
…ss/gutenberg into add/video-transcoding-mediabunny
|
@andrewserong you raise some good points about the challenges of video transcoding. The overall goal would be to help users server video successfully on their websites. The idea is generally to detect an uploaded video that is in a format web browsers may not support. This would work similar to uploading a HEIC image where we automatically convert it to JPEG because HEICs aren't very well supported.
it shouldn't slow does the editor because it would happen in a worker thread. Closing a window would leave the transcoding incomplete, but it could resume the next time the editor loaded.
The configuration for the output video would match that of the uploaded video. For example lets say I upload an old 640x480 30fps AVI or file. My intent is to insert a video on my website, but unfortunately I don't know that browsers don't support the AVI format. WordPress automatically converts my upload into a 640x480 30fps mp4 file so my video plays fine. I realize this is easier said than done, I'm mostly trying to describe the overall goal. Audio only uploads may deserve a similar transformation if we know some formats aren't web safe. We might also need guardrails about what we try to convert, or offer more user control over the conversion, or a more explicit conversion interface similar to what was proposed for GIF->Video conversion - for example a modal that pops up offering to convert an uploaded file to a web safe format, then a progress bar showing conversion progress. Good point on the storage issue - videos are huge, so maybe if the user accepts the conversion we would only upload the transcoded version. In summary: I hear your concerns and understand this PR isn't ready for prime time. We need more thinking about UX and the scope of the feature. I'll leave this open for now to keep iterating. |
The Vitest conventions validator requires TypeScript tests to pass a dynamic import to vi.mock(). The partial factory needs a cast because it only stands in for the exports transcodeVideo touches. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JhfpWLne9QuiM4SzMYLLTg
transcodeVideo now resolves to a File, so the never-settling mock promise has to be typed or the vi.mock factory no longer matches the module surface. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JhfpWLne9QuiM4SzMYLLTg
…g-mediabunny # Conflicts: # packages/block-editor/CHANGELOG.md # packages/core-data/CHANGELOG.md # packages/upload-media/CHANGELOG.md
The trunk merge brought Prettier 3.9.6, which adds wp-prettier paren spacing inside `import()` type positions. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AGarM6qGwHbEknVuo9tTHS
Trunk's docgen emits `- ` list markers; the sections this branch did not touch still carried the old `- ` form, so check-local-changes saw a diff. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AGarM6qGwHbEknVuo9tTHS
Type the transcoding tests merged into the TypeScript private actions tests and use the console matchers for expected errors. Swap a Media Library pick to its companion from the select handler instead of an effect, which trunk no longer allows in blocks, and convert the new e2e spec to TypeScript.
What?
Adds client-side video transcoding to web-safe formats for the client-side media feature, implementing #79363. When a video that is not already web-safe is uploaded, it is transcoded in the browser (via mediabunny / WebCodecs) to MP4/H.264 (or WebM/VP9) before it is served, mirroring what we already do for images and the GIF-to-video conversion in #78410.
Fixes #79363.
Note
This PR is stacked on #78410 (
add/gif-to-video-mediabunny) and reuses its@wordpress/video-conversionpackage, worker boundary, and companion-file infrastructure. It targets that branch and should be rebased ontotrunkonce #78410 lands. Review the last 6 commits.Why?
WordPress accepts video uploads in any format (
.mov,.mkv, high-bitrate.mp4) and never transcodes them, because server-side transcoding needs FFmpeg, which most hosts lack. The result is slow page loads, high bandwidth/storage cost, and clips that won't play in some browsers. This is one of the capabilities called out for the 7.1 cycle and was only a roadmap bullet on #76756.How?
Keep the original, serve a web-safe companion. WordPress always preserves the original upload (so it can be linked to or used to regenerate later, like the
-scaledand HEIC originals). So the original video is stored as the attachment and the transcoded, web-safe version is sideloaded as a companion file (recorded in attachment metadata underoptimized_video). Thecore/videoblock points its playbacksrcat the companion; a toolbar control lets authors switch back to the original.Pipeline. Detection and transcoding reuse the
@wordpress/upload-mediaqueue:prepareItemprobes the video metadata (getVideoMetadata, a cheap header read) andneedsVideoTranscodedecides eligibility (non-web-safe container/codec, oversized past the 1920px threshold, or over an optional bitrate budget) — already-optimized small files are skipped.Upload → GenerateVideoCompanion → Finalize; the companion is transcoded off the main thread via mediabunny's high-levelConversionAPI withhardwareAcceleration: 'prefer-hardware', then sideloaded.Developer opt-out. The
gutenberg_video_transcoding_keep_originalPHP filter (defaulttrue) flips the pipeline to transcode-before-upload, storing only the optimized file. Since video files can be very large, this affords the ability to reduce storage requirements with the tradeoff that the original video is no longer available for download or regeneration.Testing Instructions
.mov, an.mkv, or a large/high-bitrate.mp4)..mp4once the upload finishes, while the Media Library still holds the original upload..mp4and confirm it is left untouched.add_filter( 'gutenberg_video_transcoding_keep_original', '__return_false' );and confirm only the optimized file is stored.Automated tests
npm run test:unit packages/upload-media packages/video-conversionvendor/bin/phpunit phpunit/media/video-transcoding-test.phpnpm run test:e2e -- test/e2e/specs/editor/various/video-transcoding.spec.jsPart of #76756.
AI use
This PR was written by Claude after careful prompting, planning and review from me. I also plan to manually review the code and test the feature directly.