scratch – Blame information for rev
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
115 | office | 1 | <?php |
2 | |||
3 | /* |
||
4 | * This file is part of PHP-FFmpeg. |
||
5 | * |
||
6 | * (c) Alchemy <info@alchemy.fr> |
||
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\Media; |
||
13 | |||
14 | use Alchemy\BinaryDriver\Exception\ExecutionFailureException; |
||
15 | use FFMpeg\Filters\Audio\AudioFilters; |
||
16 | use FFMpeg\Format\FormatInterface; |
||
17 | use FFMpeg\Filters\Audio\SimpleFilter; |
||
18 | use FFMpeg\Exception\RuntimeException; |
||
19 | use FFMpeg\Exception\InvalidArgumentException; |
||
20 | use FFMpeg\Filters\Audio\AudioFilterInterface; |
||
21 | use FFMpeg\Filters\FilterInterface; |
||
22 | use FFMpeg\Format\ProgressableInterface; |
||
23 | |||
24 | class Audio extends AbstractStreamableMedia |
||
25 | { |
||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | * |
||
29 | * @return AudioFilters |
||
30 | */ |
||
31 | public function filters() |
||
32 | { |
||
33 | return new AudioFilters($this); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | * |
||
39 | * @return Audio |
||
40 | */ |
||
41 | public function addFilter(FilterInterface $filter) |
||
42 | { |
||
43 | if (!$filter instanceof AudioFilterInterface) { |
||
44 | throw new InvalidArgumentException('Audio only accepts AudioFilterInterface filters'); |
||
45 | } |
||
46 | |||
47 | $this->filters->add($filter); |
||
48 | |||
49 | return $this; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Exports the audio in the desired format, applies registered filters. |
||
54 | * |
||
55 | * @param FormatInterface $format |
||
56 | * @param string $outputPathfile |
||
57 | * |
||
58 | * @return Audio |
||
59 | * |
||
60 | * @throws RuntimeException |
||
61 | */ |
||
62 | public function save(FormatInterface $format, $outputPathfile) |
||
63 | { |
||
64 | $listeners = null; |
||
65 | |||
66 | if ($format instanceof ProgressableInterface) { |
||
67 | $listeners = $format->createProgressListener($this, $this->ffprobe, 1, 1); |
||
68 | } |
||
69 | |||
70 | $commands = array('-y', '-i', $this->pathfile); |
||
71 | |||
72 | $filters = clone $this->filters; |
||
73 | $filters->add(new SimpleFilter($format->getExtraParams(), 10)); |
||
74 | |||
75 | if ($this->driver->getConfiguration()->has('ffmpeg.threads')) { |
||
76 | $filters->add(new SimpleFilter(array('-threads', $this->driver->getConfiguration()->get('ffmpeg.threads')))); |
||
77 | } |
||
78 | if (null !== $format->getAudioCodec()) { |
||
79 | $filters->add(new SimpleFilter(array('-acodec', $format->getAudioCodec()))); |
||
80 | } |
||
81 | |||
82 | foreach ($filters as $filter) { |
||
83 | $commands = array_merge($commands, $filter->apply($this, $format)); |
||
84 | } |
||
85 | |||
86 | if (null !== $format->getAudioKiloBitrate()) { |
||
87 | $commands[] = '-b:a'; |
||
88 | $commands[] = $format->getAudioKiloBitrate() . 'k'; |
||
89 | } |
||
90 | if (null !== $format->getAudioChannels()) { |
||
91 | $commands[] = '-ac'; |
||
92 | $commands[] = $format->getAudioChannels(); |
||
93 | } |
||
94 | $commands[] = $outputPathfile; |
||
95 | |||
96 | try { |
||
97 | $this->driver->command($commands, false, $listeners); |
||
98 | } catch (ExecutionFailureException $e) { |
||
99 | $this->cleanupTemporaryFile($outputPathfile); |
||
100 | throw new RuntimeException('Encoding failed', $e->getCode(), $e); |
||
101 | } |
||
102 | |||
103 | return $this; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Gets the waveform of the video. |
||
108 | * |
||
109 | * @param integer $width |
||
110 | * @param integer $height |
||
111 | * @return Waveform |
||
112 | */ |
||
113 | public function waveform($width = 640, $height = 120) |
||
114 | { |
||
115 | return new Waveform($this, $this->driver, $this->ffprobe, $width, $height); |
||
116 | } |
||
117 | } |