This article describes how to shoot a video from JavaScript using the MediaStream Recording API. The related resources are shown below.
The workflow is shown below.
- Coding preparation
- Coding
- Operation check
Run the following commands in your terminal to prepare for coding.
mkdir javascript-media-video
cd javascript-media-video
touch index.html main.js
Open index.html in your editor and enter the following content.
Open main.js in your editor and enter the following content.
The points are shown below.
- Get the media stream to capture the video.
- Check if recording in video / webm format is possible.
- Create a media recorder.
- Call the mediaRecorder.start method to start recording.
- Call the mediaRecorder.stop method to end recording.
- Add the dataavailable event handler of mediaRecorder to get the video data.
Open index.html in your web browser. The following command are useful for macOS.
open index.html
You will be asked to allow access to the camera and microphone, so click the "Allow" button.
Click the "Allow" button and then the "Start" button to start recording.
Click the "Stop" button to stop recording.
Click the play button of the recorded video to play the video.
The MediaStream Recording API can be used not only for recording video, but also for recording audio. For information on how to record audio using MediaStream Recording, see How to record audio using MediaStream Recording, so if you are interested, please have a look. Thank you for reading!
@voivoikeri
Thank you for your question!
A friend asked me the same question in the past and I wrote an article for him.
The article is written in Japanese, but I hope you find it helpful.
https://zenn.dev/tatsuyasusukida/scraps/7a60786e24fc87
Good luck!