|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ProtoneMedia\LaravelFFMpeg\Drivers; |
|
4
|
|
|
|
|
5
|
|
|
use FFMpeg\FFProbe\DataMapping\Stream; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use ProtoneMedia\LaravelFFMpeg\Filesystem\Media; |
|
8
|
|
|
use ProtoneMedia\LaravelFFMpeg\Filesystem\MediaCollection; |
|
9
|
|
|
|
|
10
|
|
|
trait InteractsWithMediaStreams |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Returns an array with all streams. |
|
14
|
|
|
* |
|
15
|
|
|
* @return array |
|
16
|
|
|
*/ |
|
17
|
|
|
public function getStreams(): array |
|
18
|
|
|
{ |
|
19
|
|
|
if (!$this->isAdvancedMedia()) { |
|
|
|
|
|
|
20
|
|
|
return iterator_to_array($this->media->getStreams()); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
return $this->mediaCollection->map(function (Media $media) { |
|
24
|
|
|
return $this->fresh()->open(MediaCollection::make([$media]))->getStreams(); |
|
|
|
|
|
|
25
|
|
|
})->collapse()->all(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Gets the duration of the media from the first stream or from the format. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getDurationInMiliseconds(): int |
|
32
|
|
|
{ |
|
33
|
|
|
$stream = Arr::first($this->getStreams()); |
|
34
|
|
|
|
|
35
|
|
|
if ($stream->has('duration')) { |
|
36
|
|
|
return $stream->get('duration') * 1000; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$format = $this->getFormat(); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
if ($format->has('duration')) { |
|
42
|
|
|
return $format->get('duration') * 1000; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getDurationInSeconds(): int |
|
47
|
|
|
{ |
|
48
|
|
|
return round($this->getDurationInMiliseconds() / 1000); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Gets the first audio streams of the media. |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getAudioStream(): ?Stream |
|
55
|
|
|
{ |
|
56
|
|
|
return Arr::first($this->getStreams(), function (Stream $stream) { |
|
57
|
|
|
return $stream->isAudio(); |
|
58
|
|
|
}); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Gets the first video streams of the media. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getVideoStream(): ?Stream |
|
65
|
|
|
{ |
|
66
|
|
|
return Arr::first($this->getStreams(), function (Stream $stream) { |
|
67
|
|
|
return $stream->isVideo(); |
|
68
|
|
|
}); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|