Search code examples
angularvideowebcam

How to record a video in Angular with the use of ngx-webcam- capturing photos works


I am currently working on a camera that would record videos in my angular app. Currently, with the below code, I can access my camera and take snapshots.

Can someone please advise what I should amend in my code in order to have the ability to take a video?

App.module.ts file:

import {WebcamModule} from 'ngx-webcam';
...
imports: [
    ...
    WebcamModule
]

app.component.ts file:

import {WebcamImage} from 'ngx-webcam';
import {Subject, Observable} from 'rxjs';
...
export class AppComponent {
...
   // latest snapshot
   public webcamImage: WebcamImage = null;
   // webcam snapshot trigger
   private trigger: Subject<void> = new Subject<void>();
   triggerSnapshot(): void {
    this.trigger.next();
   }
   handleImage(webcamImage: WebcamImage): void {
    console.info('received webcam image', webcamImage);
    this.webcamImage = webcamImage;
   }
  
   public get triggerObservable(): Observable<void> {
    return this.trigger.asObservable();
   }
}

app.component.html file:

<webcam [height]="500" [width]="500" [trigger]="triggerObservable" (imageCapture)="handleImage($event)"></webcam>
<!-- Button Takes Photo -->
<button class="actionBtn" (click)="triggerSnapshot();">Take A Snapshot</button>
<!-- Snapshot Div where image will be shown -->
<div class="snapshot" *ngIf="webcamImage">
  <h2>Take your image or get another</h2>
  <img [src]="webcamImage.imageAsDataUrl"/>
</div>

Solution

  • ngx-webcam doesnt support out of the box recording, yet. https://github.com/basst314/ngx-webcam/issues/38   Its built in feature  

    • Webcam live view
    • Photo capturing
    • Smartphone compatibility for modern OS's (OS must support WebRTC/UserMedia API)
    • Access to front- and back-camera, if multiple cameras exist
    • Portrait & Landscape mode on smartphones
    • Mirrored live-view for user-facing cameras - "selfie view" on phones
    • Capturing of lossless pixel image data for better post-processing.   I suggest looking into different packages or approaches.  
    • how to record the video through webcam using javascript   You may have to end up getting a little lower level and building your own solution.  
    • https://developers.google.com/web/fundamentals/media/recording-video