-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an example of frame-by-frame video export #10172
Merged
+138
−13
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Mapbox GL JS debug page</title> | ||
<meta charset='utf-8'> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> | ||
<link rel='stylesheet' href='../dist/mapbox-gl.css' /> | ||
<style> | ||
#map { width: 960px; height: 540px; } | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<div id='map'></div> | ||
|
||
<script src='../dist/mapbox-gl-dev.js'></script> | ||
<script src='../debug/access_token_generated.js'></script> | ||
|
||
<script type="module"> | ||
import loadEncoder from 'https://unpkg.com/[email protected]/build/mp4-encoder.js'; | ||
import {simd} from "https://unpkg.com/wasm-feature-detect?module"; | ||
|
||
const map = window.map = new mapboxgl.Map({ | ||
container: 'map', | ||
center: [7.533634776071096, 45.486077107185565], | ||
zoom: 13.5, | ||
pitch: 61, | ||
bearing: -160, | ||
style: 'mapbox://styles/mapbox/satellite-v9' | ||
}); | ||
|
||
async function animate() { | ||
// do all the animations you need to record here | ||
map.easeTo({ | ||
bearing: map.getBearing() - 20, | ||
duration: 3000, | ||
easing: t => t | ||
}); | ||
// wait for animation to finish | ||
await untilMapEvent('moveend'); | ||
} | ||
|
||
map.on('load', async () => { | ||
map.addSource('dem', {type: 'raster-dem', url: 'mapbox://mapbox.mapbox-terrain-dem-v1'}); | ||
map.setTerrain({source: 'dem', exaggeration: 1.5}); | ||
|
||
// wait until the map settles | ||
await untilMapEvent('idle'); | ||
|
||
// uncomment to fine-tune animation without recording: | ||
// animate(); return; | ||
|
||
// don't forget to enable WebAssembly SIMD in chrome://flags for faster encoding | ||
const supportsSIMD = await simd(); | ||
|
||
// initialize H264 video encoder | ||
const Encoder = await loadEncoder({simd: supportsSIMD}); | ||
|
||
const gl = map.painter.context.gl; | ||
const width = gl.drawingBufferWidth; | ||
const height = gl.drawingBufferHeight; | ||
|
||
const encoder = Encoder.create({ | ||
width, | ||
height, | ||
fps: 60, | ||
kbps: 64000, | ||
rgbFlipY: true | ||
}); | ||
|
||
// stub performance.now for deterministic rendering per-frame (only available in dev build) | ||
let now = performance.now(); | ||
mapboxgl.setNow(now); | ||
|
||
const ptr = encoder.getRGBPointer(); // keep a pointer to encoder WebAssembly heap memory | ||
|
||
function frame() { | ||
// increment stub time by 16.6ms (60 fps) | ||
now += 1000 / 60; | ||
mapboxgl.setNow(now); | ||
|
||
const pixels = encoder.memory().subarray(ptr); // get a view into encoder memory | ||
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels); // read pixels into encoder | ||
encoder.encodeRGBPointer(); // encode the frame | ||
} | ||
|
||
map.on('render', frame); // set up frame-by-frame recording | ||
|
||
await animate(); // run all the animations | ||
|
||
// stop recording | ||
map.off('render', frame); | ||
mapboxgl.restoreNow(); | ||
|
||
// download the encoded video file | ||
const mp4 = encoder.end(); | ||
const anchor = document.createElement("a"); | ||
anchor.href = URL.createObjectURL(new Blob([mp4], {type: "video/mp4"})); | ||
anchor.download = "mapbox-gl"; | ||
anchor.click(); | ||
|
||
// make sure to run `ffmpeg -i mapbox-gl.mp4 mapbox-gl-optimized.mp4` to compress the video | ||
}); | ||
|
||
function untilMapEvent(type) { | ||
return new Promise(resolve => map.once(type, resolve)); | ||
} | ||
|
||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is only available with dev builds, I believe. This example isn't directly usable with the built version of GL JS on CDN or NPM. That's fine for this debug page, but it's good to note.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ryanhamley yes, I mentioned this in the PR description and the code comment above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added another commit that documents & exposes these methods in production build, so we can add the official example after this gets into a release. Note that it no longer falls back to
Date.now
because it should be universally supported now.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooops I missed that in the PR description.
👍 awesome