Skip to content Skip to sidebar Skip to footer

Check Video Length On Upload - Angular

I am trying to do a check to see if the video I uploaded is longer than 30s, but cannot get the example I need. This is the code I have so far where I check the size and some other

Solution 1:

I'd add an invisible video player and set its source then get the duration from that:

HTML:

<inputtype="file" (change)="readVideoUrl($event)"><p *ngIf="videoSizeError">Too big</p><video #videostyle="display: none;" *ngIf="videoUrl"width="320"height="240"controls [attr.src]="videoUrl" (loadedmetadata)="getDuration($event)"></video>

TS:

import { Component } from'@angular/core';
import { DomSanitizer } from'@angular/platform-browser';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
exportclassAppComponent {
  videoUrl;
  videoSizeError;

  constructor(private sanitizer: DomSanitizer) { }

  readVideoUrl(event: any) {
    const files = event.target.files;
    if (files && files[0]) {
      this.videoUrl = this.sanitizer.bypassSecurityTrustUrl(URL.createObjectURL(files[0]));
    }
  }

  getDuration(e) {
    const duration = e.target.duration;
    this.videoSizeError = duration > 30;
  }
}

working code's link

Post a Comment for "Check Video Length On Upload - Angular"