Passed
Push — master ( 67f2c8...68e9c9 )
by Amin
01:53
created

Export::getPathInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2019 Amin Yazdanpanah<http://www.aminyazdanpanah.com>.
5
 *
6
 * Licensed under the MIT License;
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *      https://opensource.org/licenses/MIT
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace AYazdanpanah\FFMpegStreaming;
20
21
use AYazdanpanah\FFMpegStreaming\Filters\Filter;
22
use AYazdanpanah\FFMpegStreaming\Traits\Formats;
23
24
abstract class Export
25
{
26
    use Formats;
27
28
    /** @var object */
29
    protected $media;
30
31
    /** @var Filter */
32
    protected $filter;
33
34
    /** @var array */
35
    protected $path_info;
36
37
    /**
38
     * Export constructor.
39
     * @param Media $media
40
     */
41
    public function __construct(Media $media)
42
    {
43
        $this->media = $media;
44
    }
45
46
    /**
47
     * @param string $path
48
     * @return Export
49
     */
50
    public function save(string $path = null): Export
51
    {
52
        $path = $this->getPath($path);
53
54
        $this->setFilter();
55
56
        $this->media->addFilter(
57
            $this->getFilter()
58
        );
59
60
        $this->media->save(
61
            $this->getFormat(),
62
            $path
63
        );
64
65
        return $this;
66
    }
67
68
    /**
69
     * @return Filter
70
     */
71
    abstract protected function getFilter(): Filter;
72
73
    /**
74
     * @return mixed
75
     */
76
    abstract protected function setFilter();
77
78
    private function getPath($path): string
79
    {
80
        $this->path_info = $path_parts = (null === $path) ? $this->media->getPathInfo() : pathinfo($path);
81
        $dirname = str_replace("\\", "/", $path_parts["dirname"]);
82
        Helper::makeDir($dirname);
83
        $filename = substr($path_parts["filename"], -50);
84
85
        if ($this instanceof DASH) {
86
            $path = $dirname . "/" . $filename . ".mpd";
87
        } elseif ($this instanceof HLS) {
88
            $path = $dirname . "/" . $filename . "_" . end($this->getRepresentations())->getHeight() . "p.m3u8";
89
            ExportHLSPlaylist::savePlayList($dirname . "/" . $filename . ".m3u8", $this->getRepresentations(), $filename);
90
        }
91
92
        return $path;
93
    }
94
95
    /**
96
     * @return array
97
     */
98
    public function getPathInfo(): array
99
    {
100
        return $this->path_info;
101
    }
102
}
103