DEV Community

Cover image for Save your web camera recording locally in your browser
EneasLari
EneasLari

Posted on

Save your web camera recording locally in your browser

This post is the sequel of How to record your web camera only with JavaScript.

Today we will store the video after recording into browser's indexedDB.

So lets start with the already existing code of web camera recording Source code. We will make a new branch for this project:

Image description

I know I know I am good at naming!

Next let's create a new file named idb-file-storage.js. This is a wrapper of indexedDB API

Now lets create a new file named indexedDB.js:

In this file we define three functions:

  • saveRecordedVideoInDB(recordedpieces, stored_name, blob_name)
    function that saves the array of recorded pieces

  • retreiveVideoFromDB(stored_name)
    returns an array of stored videos in indexedDB

  • removeStoredVideo(bloburl, videoname, stored_name)
    removes a specific video from indexedDB

Now add a script tag with indexedDB script at index.html:

<script type="module" src="js/indexedDB.js"></script>

And finally edit the index.js. Call saveRecordedVideoInDB function when the recording stops.



    mediarecorder.onstop = ()=>{ 
     window.saveRecordedVideoInDB([recordedChunks],"storedvideos", "videoname" + datetime).then(() => {
            download();
          })
    }


Enter fullscreen mode Exit fullscreen mode

The final index.html and index.js:

index.html:

index.js

If you press f12 and you navigate to Application > Storage > IndexedDb in console panel you will see that a new entry added.

If you want to retrieve it or delete it from html, simply call the retreiveVideoFromDB and removeStoredVideo.

Source code provided below:

Source Code

Thanks for your time.
Leave a question or comment below.

Top comments (2)

Collapse
 
gladyslowe profile image
GladysLowe

These info-packed moving pictures are a fantastic way to share information and tell the world about your brand. Spell to end a relationship

Collapse
 
thien711 profile image
Thien Nguyen • Edited

Thank for your post.
Regarding to dowload the recorded, I am facing an issue that cannot waiting for the recorded to dowload done (in my case: dowload done then close tab).
Is there any way to wait the video for dowloaded?

I am Vietnammes. Sorry for my english not well.