| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the PHP-FFmpeg-video-streaming package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Amin Yazdanpanah <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 Streaming\Filters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Streaming\File; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Streaming\Representation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Streaming\Utiles; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class HLSFilter extends Filter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /**  @var \Streaming\HLS */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $hls; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $dirname; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $filename; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $seg_sub_dir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $base_url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     private $seg_filename; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     private function getFormats(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $format = ['-c:v', $this->hls->getFormat()->getVideoCodec()]; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $audio_format = $this->hls->getFormat()->getAudioCodec(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         return $audio_format ? array_merge($format, ['-c:a', $audio_format]) : $format; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param Representation $rep | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param bool $not_last | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     private function playlistPath(Representation $rep, bool $not_last): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         return $not_last ? [$this->dirname . "/" . $this->filename . "_" . $rep->getHeight() . "p.m3u8"] : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param Representation $rep | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     private function getAudioBitrate(Representation $rep): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return $rep->getAudioKiloBitrate() ? ["-b:a", $rep->getAudioKiloBitrate() . "k"] : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     private function getBaseURL(): array | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         return $this->base_url ? ["-hls_base_url", $this->base_url] : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     private function getKeyInfo(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return $this->hls->getHlsKeyInfoFile() ? ["-hls_key_info_file", $this->hls->getHlsKeyInfoFile()] : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     private function getInitFilename(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         return $this->seg_sub_dir . $this->filename . "_" . $this->hls->getHlsFmp4InitFilename(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @param Representation $rep | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     private function getSegmentFilename(Representation $rep): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $ext = ($this->hls->getHlsFmp4InitFilename() === "fmp4") ? "m4s" : "ts"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         return $this->seg_filename . "_" . $rep->getHeight() . "p_%04d." . $ext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @param Representation $rep | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     private function initArgs(Representation $rep): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             "-s:v", $rep->getResize(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             "-crf", "20", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             "-sc_threshold", "0", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             "-g", "48", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             "-keyint_min", "48", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             "-hls_list_size", $this->hls->getHlsListSize(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             "-hls_time", $this->hls->getHlsTime(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             "-hls_allow_cache", (int)$this->hls->isHlsAllowCache(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             "-b:v", $rep->getKiloBitrate() . "k", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             "-maxrate", intval($rep->getKiloBitrate() * 1.2) . "k", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             "-hls_segment_type", $this->hls->getHlsSegmentType(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             "-hls_fmp4_init_filename", $this->getInitFilename(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             "-hls_segment_filename", $this->getSegmentFilename($rep) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @param Representation $rep | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * @param bool $not_last | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     private function getArgs(Representation $rep, bool $not_last): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         $this->filter = array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             $this->filter, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             $this->initArgs($rep), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             $this->getAudioBitrate($rep), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             $this->getBaseURL(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             $this->getKeyInfo(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             $this->hls->getAdditionalParams(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             ["-strict", $this->hls->getStrict()], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             $this->playlistPath($rep, $not_last) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * set segments paths | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     private function segmentPaths() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         if ($this->hls->getTsSubDirectory()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             File::makeDir($this->dirname . "/" . $this->hls->getTsSubDirectory() . "/"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         $base = Utiles::appendSlash($this->hls->getHlsBaseUrl()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $this->seg_sub_dir = Utiles::appendSlash($this->hls->getTsSubDirectory()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $this->seg_filename = $this->dirname . "/" . $this->seg_sub_dir . $this->filename; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         $this->base_url = $base . $this->seg_sub_dir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * set paths | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     private function setPaths(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         $this->dirname = str_replace("\\", "/", $this->hls->getPathInfo(PATHINFO_DIRNAME)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         $this->filename = $this->hls->getPathInfo(PATHINFO_FILENAME); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         $this->segmentPaths(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      * @param $media | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     public function setFilter($media): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         $this->hls = $media; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         $this->setPaths(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         $reps = $this->hls->getRepresentations(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         foreach ($reps as $key => $rep) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             if ($key) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |                 $this->filter = array_merge($this->filter, $this->getFormats()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |             $this->getArgs($rep, end($reps) !== $rep); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 189 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 190 |  |  | } |