Skip to content

Commit 75b2c63

Browse files
webcodecs cleanup for codepen
1 parent b26e9d7 commit 75b2c63

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

WebCodecs/decodeToVideo.html

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,47 @@
77
body {
88
background-color: #333;
99
color: #fff;
10+
font-size: large;
1011
}
12+
13+
a {
14+
color: #0099ff; /* Light blue */
15+
}
16+
17+
a:hover {
18+
color: #00ccff; /* Slightly lighter blue */
19+
}
20+
21+
ul, p {
22+
margin-top: 6px;
23+
margin-bottom: 6px;
24+
}
25+
1126
</style>
1227
</head>
1328
<body>
29+
<p>
30+
Proof of concept that loads a video file and uses WebCodecs to convert it to a
31+
<code><a href="https://www.w3.org/TR/mediacapture-streams/#mediastream">MediaStream</a></code> where it can be set as a
32+
VideoElement <code>srcObject</code> or sent to a WebRTC <code>RTCPeerConnection</code>
33+
</p>
34+
Notes:
35+
<ul>
36+
<li><a href="https://github.com/gpac/mp4box.js">mp4box.js</a> is used for demuxing.</li>
37+
<li>Doesn't include a buffer management system to keep audio & video in sync</li>
38+
<li>Only works if the source file contains a single audio and video track</li>
39+
</ul>
40+
</p>
41+
<p>
42+
See <a href="https://webrtcHacks.com">webrtcHacks</a> for more details and commentary.
43+
</p>
44+
<p>
45+
Heavily inspired by the <a href="https://github.com/w3c/webcodecs">W3C WebCodecs</a> work and their <a
46+
href="https://w3c.github.io/webcodecs/samples/audio-video-player/audio_video_player.html">Audio and Video Player
47+
sample</a>.
48+
Video "<a href="https://peach.blender.org/">Big Buck Bunny</a>" by Blender Foundation, used under <a
49+
href="https://creativecommons.org/licenses/by/3.0/">CC BY 3.0</a>.
50+
</p>
1451
<button>Play Big Buck Bunny</button>
1552
<input type="file" id="videoFile" accept="video/*">
1653
<br/>
@@ -21,13 +58,13 @@
2158
const video = document.querySelector('video');
2259

2360
// Assumes file has both audio and video tracks
24-
const videoTrackGenerator = new MediaStreamTrackGenerator({ kind: 'video' });
25-
const audioTrackGenerator = new MediaStreamTrackGenerator({ kind: 'audio' });
61+
const videoTrackGenerator = new MediaStreamTrackGenerator({kind: 'video'});
62+
const audioTrackGenerator = new MediaStreamTrackGenerator({kind: 'audio'});
2663

2764
const stream = new MediaStream([videoTrackGenerator, audioTrackGenerator]);
2865
video.srcObject = stream;
2966

30-
function start(file){
67+
function start(file) {
3168
bbbButton.disabled = true;
3269
fileButton.disabled = true;
3370

@@ -39,8 +76,10 @@
3976
worker.postMessage({file, videoWriter, audioWriter}, [videoWriter, audioWriter]);
4077
}
4178

42-
bbbButton.onclick = ()=> start('../BigBuckBunny_360p30.mp4');
43-
fileButton.onchange = ()=> start(fileButton.files[0]);
79+
const videoUrl = '../BigBuckBunny_360p30.mp4';
80+
// const videoUrl = "https://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4"; // works
81+
bbbButton.onclick = () => start(videoUrl);
82+
fileButton.onchange = () => start(fileButton.files[0]);
4483

4584
</script>
4685
</body>

WebCodecs/worker.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
importScripts("demuxer_mp4.js");
1+
importScripts("https://gpac.github.io/mp4box.js/dist/mp4box.all.js"); // mp4box.js - https://github.com/gpac/mp4box.js
22

33
// Get the appropriate `description` for a specific track. Assumes that the
44
// track is H.264, H.265, VP8, VP9, or AV1.
@@ -69,7 +69,7 @@ async function start(data) {
6969

7070

7171
// Decode and loop, waiting for each sample's duration before decoding the next.
72-
// NOTE: at 1000 samples/second, the audio one has a hard time keeping up
72+
// NOTE: the audio one has a hard time keeping up
7373
function renderVideoLoop() {
7474
if (videoSamples.length > 0) {
7575
const duration = decodeVideoSample(videoSamples.shift());

0 commit comments

Comments
 (0)