scratch – Blame information for rev 115
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
115 | office | 1 | <?php |
2 | |||
3 | /* |
||
4 | * This file is part of PHP-FFmpeg. |
||
5 | * |
||
6 | * (c) Strime <romain@strime.io> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace FFMpeg\Filters\Video; |
||
13 | |||
14 | use FFMpeg\Exception\InvalidArgumentException; |
||
15 | use FFMpeg\Exception\RuntimeException; |
||
16 | use FFMpeg\Media\Video; |
||
17 | use FFMpeg\Format\VideoInterface; |
||
18 | |||
19 | class ExtractMultipleFramesFilter implements VideoFilterInterface |
||
20 | { |
||
21 | /** will extract a frame every second */ |
||
22 | const FRAMERATE_EVERY_SEC = '1/1'; |
||
23 | /** will extract a frame every 2 seconds */ |
||
24 | const FRAMERATE_EVERY_2SEC = '1/2'; |
||
25 | /** will extract a frame every 5 seconds */ |
||
26 | const FRAMERATE_EVERY_5SEC = '1/5'; |
||
27 | /** will extract a frame every 10 seconds */ |
||
28 | const FRAMERATE_EVERY_10SEC = '1/10'; |
||
29 | /** will extract a frame every 30 seconds */ |
||
30 | const FRAMERATE_EVERY_30SEC = '1/30'; |
||
31 | /** will extract a frame every minute */ |
||
32 | const FRAMERATE_EVERY_60SEC = '1/60'; |
||
33 | |||
34 | /** @var integer */ |
||
35 | private $priority; |
||
36 | private $frameRate; |
||
37 | private $destinationFolder; |
||
38 | |||
39 | public function __construct($frameRate = self::FRAMERATE_EVERY_SEC, $destinationFolder = __DIR__, $priority = 0) |
||
40 | { |
||
41 | $this->priority = $priority; |
||
42 | $this->frameRate = $frameRate; |
||
43 | |||
44 | // Make sure that the destination folder has a trailing slash |
||
45 | if(strcmp( substr($destinationFolder, -1), "/") != 0) |
||
46 | $destinationFolder .= "/"; |
||
47 | |||
48 | // Set the destination folder |
||
49 | $this->destinationFolder = $destinationFolder; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | public function getPriority() |
||
56 | { |
||
57 | return $this->priority; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | public function getFrameRate() |
||
64 | { |
||
65 | return $this->frameRate; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | public function getDestinationFolder() |
||
72 | { |
||
73 | return $this->destinationFolder; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | public function apply(Video $video, VideoInterface $format) |
||
80 | { |
||
81 | $commands = array(); |
||
82 | $duration = 0; |
||
83 | |||
84 | try { |
||
85 | // Get the duration of the video |
||
86 | foreach ($video->getStreams()->videos() as $stream) { |
||
87 | if ($stream->has('duration')) { |
||
88 | $duration = $stream->get('duration'); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | // Get the number of frames per second we have to extract. |
||
93 | if(preg_match('/(\d+)(?:\s*)([\+\-\*\/])(?:\s*)(\d+)/', $this->frameRate, $matches) !== FALSE){ |
||
94 | $operator = $matches[2]; |
||
95 | |||
96 | switch($operator){ |
||
97 | case '/': |
||
98 | $nbFramesPerSecond = $matches[1] / $matches[3]; |
||
99 | break; |
||
100 | |||
101 | default: |
||
102 | throw new InvalidArgumentException('The frame rate is not a proper division: ' . $this->frameRate); |
||
103 | break; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | // Set the number of digits to use in the exported filenames |
||
108 | $nbImages = ceil( $duration * $nbFramesPerSecond ); |
||
109 | |||
110 | if($nbImages < 100) |
||
111 | $nbDigitsInFileNames = "02"; |
||
112 | elseif($nbImages < 1000) |
||
113 | $nbDigitsInFileNames = "03"; |
||
114 | else |
||
115 | $nbDigitsInFileNames = "06"; |
||
116 | |||
117 | // Set the parameters |
||
118 | $commands[] = '-vf'; |
||
119 | $commands[] = 'fps=' . $this->frameRate; |
||
120 | $commands[] = $this->destinationFolder . 'frame-%'.$nbDigitsInFileNames.'d.jpg'; |
||
121 | } |
||
122 | catch (RuntimeException $e) { |
||
123 | throw new RuntimeException('An error occured while extracting the frames: ' . $e->getMessage() . '. The code: ' . $e->getCode()); |
||
124 | } |
||
125 | |||
126 | return $commands; |
||
127 | } |
||
128 | } |