1

I want to get video duration before upload. How to do this in angular2? There is my input code:

<input type="file" class="form-control" ng2FileSelect [uploader]="uploader" accept="video/*" #video (change)="getVideoInfor(video.value) "/>

I find some comparable example using jquery, but I have no idea to use in angualr2. Is it possible just using angular2 without jquery to do that?

PaulHuang
  • 219
  • 2
  • 3
  • 14

2 Answers2

1

Please find answer here https://stackoverflow.com/a/29285597/7270500 using javascript.

By the way, getVideoInfor is referring to video.value. This can easily be accessed using $event.target.value, saving you the #video reference, unless needed in your component's code.

Community
  • 1
  • 1
Arinaud
  • 155
  • 6
  • I just found it, and it's amazing! I get the video.duration value what I want, but I have to use the value in my component.ts. How to output value of video.duration from video.onloadedmetadata() to component. I try many ways but can't access it. Thank you for your help:) – PaulHuang Feb 23 '17 at 04:46
  • Add a property to your component class and assign it in video.onloadedmetada(). In your template read that value using {{ }} syntax. See [link](https://angular.io/docs/ts/latest/guide/displaying-data.html) for examples – Arinaud Feb 23 '17 at 05:32
  • You save my life, and it's so glad that you response me patiently:) – PaulHuang Feb 23 '17 at 08:08
0

sorry wanted to comment to answered post but stack wont let me.

@PaulHuang would it be possible to see how you have implemented the answer? I have been trying to modify the answer in to my angular 2 but getting some errors that I cant get past.

Robbie
  • 320
  • 2
  • 12
  • Here you are, `getDuration(files) { var myVideos = []; window.URL = window.URL; myVideos.push(files[0]); var a = []; var video = document.createElement('video'); video.preload = 'metadata'; video.onloadedmetadata = function( videoDuration) { window.URL.revokeObjectURL(this.src) a.push(video.duration); }; video.src = URL.createObjectURL(files[0]); this.videoDuration = a; }` but I don't think it's the best way. If you find another better, please share to meXD – PaulHuang Mar 01 '17 at 02:13