@Deletron247

Как избавится от goto в этом примере кода?

Собственно код
spoiler
static function startTranscoding($streamId) {
        $streamInfo = array();
        self::$ipTV_db->query("SELECT * FROM `streams` t1 INNER JOIN `streams_types` t2 ON t2.type_id = t1.type AND t2.live = 0 LEFT JOIN `transcoding_profiles` t4 ON t1.transcode_profile_id = t4.profile_id WHERE t1.direct_source = 0 AND t1.id = '%d'", $streamId);
        if (!(self::$ipTV_db->d1E5CE3B87bB868b9e6Efd39AA355A4F() <= 0)) {
            $streamInfo["stream_info"] = self::$ipTV_db->f1ed191d78470660eDfF4A007696bc1f();
            $targetContainer = json_decode($streamInfo["stream_info"]["target_container"], true);
            if (json_last_error() === JSON_ERROR_NONE) {
                $streamInfo["stream_info"]["target_container"] = $targetContainer;
            } else {
                $streamInfo["stream_info"]["target_container"] = array($streamInfo["stream_info"]["target_container"]);
            }
            self::$ipTV_db->query("SELECT * FROM `streams_sys` WHERE stream_id  = '%d' AND `server_id` = '%d'", $streamId, SERVER_ID);
            if (!(self::$ipTV_db->D1E5cE3B87bb868B9E6EfD39Aa355a4F() <= 0)) {
                $streamInfo["server_info"] = self::$ipTV_db->f1ed191D78470660EDFF4a007696BC1f();
                self::$ipTV_db->query("SELECT t1.*, t2.* FROM `streams_options` t1, `streams_arguments` t2 WHERE t1.stream_id = '%d' AND t1.argument_id = t2.id", $streamId);
                $streamInfo["stream_arguments"] = self::$ipTV_db->c126FD559932F625CDf6098d86C63880();
                $streamSource = urldecode(json_decode($streamInfo["stream_info"]["stream_source"], true)[0]);
                if (substr($streamSource, 0, 2) == "s:") {
                    $streamSourceParts = explode(":", $streamSource, 3);
                    $serverId = $streamSourceParts[1];
                    if ($serverId != SERVER_ID) {
                        $filePath = a78Bf8d35765BE2408c50712ce7A43aD::$StreamingServers[$serverId]["api_url"] . "&action=getFile&filename=" . urlencode($streamSourceParts[2]);
                    } else {
                        $filePath = $streamSourceParts[2];
                    }

                    $streamProtocol = null;
                } else {
                    $streamProtocol = substr($streamSource, 0, strpos($streamSource, "://"));
                    $filePath = str_replace(" ", "%20", $streamSource);
                }
                $streamArguments = implode(" ", self::eA860C1D3851c46D06e64911E3602768($streamInfo["stream_arguments"], $streamProtocol, "fetch"));
                if (isset($serverId) && $serverId == SERVER_ID && $streamInfo["stream_info"]["movie_symlink"] == 1) {
                    $symlinkCommand = "ln -s \"{$filePath}\" " . MOVIES_PATH . $streamId . "." . pathinfo($filePath, PATHINFO_EXTENSION) . " >/dev/null 2>/dev/null & echo \$! > " . MOVIES_PATH . $streamId . "_.pid";
                    goto symlinkLabel;
                }
                $subtitles = json_decode($streamInfo["stream_info"]["movie_subtitles"], true);
                $subtitlesString = '';
                $index = 0;
                subtitlesLoop:
                if (!($index < count($subtitles["files"]))) {
                    $subtitlesOptions = '';
                    $index = 0;
                    optionsLoop:
                    if (!($index < count($subtitles["files"]))) {
                        $symlinkCommand = FFMPEG_PATH . " -y -nostdin -hide_banner -loglevel warning -err_detect ignore_err {FETCH_OPTIONS} -fflags +genpts -async 1 {READ_NATIVE} -i \"{STREAM_SOURCE}\" {$subtitlesString}";
                        $readNativeOption = '';
                        if (!($streamInfo["stream_info"]["read_native"] == 1)) {
                            $readNativeOption = "-re";
                        }
                        if ($streamInfo["stream_info"]["enable_transcode"] == 1) {
                            if ($streamInfo["stream_info"]["transcode_profile_id"] == -1) {
                                $streamInfo["stream_info"]["transcode_attributes"] = array_merge(self::ea860c1d3851c46d06E64911e3602768($streamInfo["stream_arguments"], $streamProtocol, "transcode"), json_decode($streamInfo["stream_info"]["transcode_attributes"], true));
                            } else {
                                $streamInfo["stream_info"]["transcode_attributes"] = json_decode($streamInfo["stream_info"]["profile_options"], true);
                            }
                        } else {
                            $streamInfo["stream_info"]["transcode_attributes"] = array();
                        }
                        $copyUnknownOption = "-map 0 -copy_unknown ";
                        if (!empty($streamInfo["stream_info"]["custom_map"])) {
                            $copyUnknownOption = $streamInfo["stream_info"]["custom_map"] . " -copy_unknown ";
                        } elseif ($streamInfo["stream_info"]["remove_subtitles"] == 1) {
                            $copyUnknownOption = "-map 0:a -map 0:v";
                        }
                        if (array_key_exists("-acodec", $streamInfo["stream_info"]["transcode_attributes"])) {
                            $streamInfo["stream_info"]["transcode_attributes"]["-acodec"] = "copy";
                        }
                        if (array_key_exists("-vcodec", $streamInfo["stream_info"]["transcode_attributes"])) {
                            $streamInfo["stream_info"]["transcode_attributes"]["-vcodec"] = "copy";
                        }

                        $fileExtensions = array();
                        foreach ($streamInfo["stream_info"]["target_container"] as $extension) {
                            $fileExtensions[$extension] = "-movflags +faststart -dn {$copyUnknownOption} -ignore_unknown {$subtitlesOptions} " . MOVIES_PATH . $streamId . "." . $extension . " ";
                        }

                        foreach ($fileExtensions as $extension => $codec) {
                            if ($extension == "mp4") {
                                $streamInfo["stream_info"]["transcode_attributes"]["-scodec"] = "mov_text";
                            } elseif ($extension == "mkv") {
                                $streamInfo["stream_info"]["transcode_attributes"]["-scodec"] = "srt";
                            } else {
                                $streamInfo["stream_info"]["transcode_attributes"]["-scodec"] = "copy";
                            }
                            $symlinkCommand .= implode(" ", self::F6664c80BDe3e9bbe2c12CEb906D5A11($streamInfo["stream_info"]["transcode_attributes"])) . " ";
                            $symlinkCommand .= $codec;
                        }
                        $symlinkCommand .= " >/dev/null 2>MOVIES_PATH" . $streamId . ".errors & echo \$! > " . MOVIES_PATH . $streamId . "_.pid";
                        $symlinkCommand = str_replace(array("{FETCH_OPTIONS}", "{STREAM_SOURCE}", "{READ_NATIVE}"), array(empty($streamArguments) ? '' : $streamArguments, $filePath, empty($streamInfo["stream_info"]["custom_ffmpeg"]) ? $readNativeOption : ''), $symlinkCommand);
                        symlinkLabel:
                        shell_exec($symlinkCommand);
                        file_put_contents("/tmp/commands", $symlinkCommand . "\n", FILE_APPEND);
                        $pid = intval(file_get_contents(MOVIES_PATH . $streamId . "_.pid"));
                        self::$ipTV_db->query("UPDATE `streams_sys` SET `to_analyze` = 1,`stream_started` = '%d',`stream_status` = 0,`pid` = '%d' WHERE `stream_id` = '%d' AND `server_id` = '%d'", time(), $pid, $streamId, SERVER_ID);
                        return $pid;
                    }

                    $subtitlesOptions .= "-map " . ($index + 1) . " -metadata:s:s:{$index} title={$subtitles["names"][$index]} -metadata:s:s:{$index} language={$subtitles["names"][$index]} ";
                    $index++;
                    goto optionsLoop;
                }

                $subtitleFile = urldecode($subtitles["files"][$index]);
                $subtitleCharset = $subtitles["charset"][$index];

                if ($subtitles["location"] == SERVER_ID) {
                    $subtitlesString .= "-sub_charenc \"{$subtitleCharset}\" -i \"{$subtitleFile}\" ";
                } else {
                    $subtitlesString .= "-sub_charenc \"{$subtitleCharset}\" -i \"" . a78BF8D35765be2408c50712Ce7a43aD::$StreamingServers[$subtitles["location"]]["api_url"] . "&action=getFile&filename=" . urlencode($subtitleFile) . "\" ";
                }

                $index++;
                goto subtitlesLoop;
            }
            return false;
        }
        return false;
    }
  • Вопрос задан
  • 169 просмотров
Решения вопроса 1
sharp97
@sharp97
не фонтан но брызги есть
Для избавления от оператора goto в этом коде, вы можете использовать структуры данных, такие как циклы for и foreach, а также условные операторы if. Вот как вы можете переписать код без использования goto:
тык

static function startTranscoding($streamId) {
    $streamInfo = array();
    self::$ipTV_db->query("SELECT * FROM `streams` t1 INNER JOIN `streams_types` t2 ON t2.type_id = t1.type AND t2.live = 0 LEFT JOIN `transcoding_profiles` t4 ON t1.transcode_profile_id = t4.profile_id WHERE t1.direct_source = 0 AND t1.id = '%d'", $streamId);

    if (self::$ipTV_db->d1E5CE3B87bB868b9e6Efd39AA355A4F() <= 0) {
        return false;
    }

    $streamInfo["stream_info"] = self::$ipTV_db->f1ed191d78470660eDfF4A007696bc1f();
    $targetContainer = json_decode($streamInfo["stream_info"]["target_container"], true);

    if (json_last_error() === JSON_ERROR_NONE) {
        $streamInfo["stream_info"]["target_container"] = $targetContainer;
    } else {
        $streamInfo["stream_info"]["target_container"] = array($streamInfo["stream_info"]["target_container"]);
    }

    self::$ipTV_db->query("SELECT * FROM `streams_sys` WHERE stream_id  = '%d' AND `server_id` = '%d'", $streamId, SERVER_ID);

    if (self::$ipTV_db->D1E5cE3B87bb868B9E6EfD39Aa355a4F() <= 0) {
        return false;
    }

    $streamInfo["server_info"] = self::$ipTV_db->f1ed191D78470660EDFF4a007696BC1f();
    self::$ipTV_db->query("SELECT t1.*, t2.* FROM `streams_options` t1, `streams_arguments` t2 WHERE t1.stream_id = '%d' AND t1.argument_id = t2.id", $streamId);
    $streamInfo["stream_arguments"] = self::$ipTV_db->c126FD559932F625CDf6098d86C63880();
    $streamSource = urldecode(json_decode($streamInfo["stream_info"]["stream_source"], true)[0]);

    if (substr($streamSource, 0, 2) == "s:") {
        $streamSourceParts = explode(":", $streamSource, 3);
        $serverId = $streamSourceParts[1];

        if ($serverId != SERVER_ID) {
            $filePath = a78Bf8d35765BE2408c50712ce7A43aD::$StreamingServers[$serverId]["api_url"] . "&action=getFile&filename=" . urlencode($streamSourceParts[2]);
        } else {
            $filePath = $streamSourceParts[2];
        }

        $streamProtocol = null;
    } else {
        $streamProtocol = substr($streamSource, 0, strpos($streamSource, "://"));
        $filePath = str_replace(" ", "%20", $streamSource);
    }

    $streamArguments = implode(" ", self::eA860C1D3851c46D06e64911E3602768($streamInfo["stream_arguments"], $streamProtocol, "fetch"));

    if (isset($serverId) && $serverId == SERVER_ID && $streamInfo["stream_info"]["movie_symlink"] == 1) {
        $symlinkCommand = "ln -s \"{$filePath}\" " . MOVIES_PATH . $streamId . "." . pathinfo($filePath, PATHINFO_EXTENSION) . " >/dev/null 2>/dev/null & echo \$! > " . MOVIES_PATH . $streamId . "_.pid";
    }

    $subtitles = json_decode($streamInfo["stream_info"]["movie_subtitles"], true);
    $subtitlesString = '';

    for ($index = 0; $index < count($subtitles["files"]); $index++) {
        $subtitleFile = urldecode($subtitles["files"][$index]);
        $subtitleCharset = $subtitles["charset"][$index];

        if ($subtitles["location"] == SERVER_ID) {
            $subtitlesString .= "-sub_charenc \"{$subtitleCharset}\" -i \"{$subtitleFile}\" ";
        } else {
            $subtitlesString .= "-sub_charenc \"{$subtitleCharset}\" -i \"" . a78BF8D35765be2408c50712Ce7a43aD::$StreamingServers[$subtitles["location"]]["api_url"] . "&action=getFile&filename=" . urlencode($subtitleFile) . "\" ";
        }
    }

    $symlinkCommand = FFMPEG_PATH . " -y -nostdin -hide_banner -loglevel warning -err_detect ignore_err {FETCH_OPTIONS} -fflags +genpts -async 1 {READ_NATIVE} -i \"{STREAM_SOURCE}\" {$subtitlesString}";

    $readNativeOption = '';

    if (!($streamInfo["stream_info"]["read_native"] == 1)) {
        $readNativeOption = "-re";
    }

    if ($streamInfo["stream_info"]["enable_transcode"] == 1) {
        if ($streamInfo["stream_info"]["transcode_profile_id"] == -1) {
            $streamInfo["stream_info"]["transcode_attributes"] = array_merge(self::ea860c1d3851c46d06E64911e3602768($streamInfo["stream_arguments"], $streamProtocol, "transcode"), json_decode($streamInfo["stream_info"]["transcode_attributes"], true));
        } else {
            $streamInfo["stream_info"]["transcode_attributes"] = json_decode($streamInfo["stream_info"]["profile_options"], true);
        }
    } else {
        $streamInfo["stream_info"]["transcode_attributes"] = array();
    }

    $copyUnknownOption = "-map 0 -copy_unknown ";

    if (!empty($streamInfo["stream_info"]["custom_map"])) {
        $copyUnknownOption = $streamInfo["stream_info"]["custom_map"] . " -copy_unknown ";
    } elseif ($streamInfo["stream_info"]["remove_subtitles"] == 1) {
        $copyUnknownOption = "-map 0:a -map 0:v";
    }

    if (array_key_exists("-acodec", $streamInfo["stream_info"]["transcode_attributes"])) {
        $streamInfo["stream_info"]["transcode_attributes"]["-acodec"] = "copy";
    }

    if (array_key_exists("-vcodec", $streamInfo["stream_info"]["transcode_attributes"])) {
        $streamInfo["stream_info"]["transcode_attributes"]["-vcodec"] = "copy";
    }

    $fileExtensions = array();

    foreach ($streamInfo["stream_info"]["target_container"] as $extension) {
        $fileExtensions[$extension] = "-movflags +faststart -dn {$copyUnknownOption} -ignore_unknown {$subtitlesOptions} " . MOVIES_PATH . $streamId . "." . $extension . " ";
    }

    foreach ($fileExtensions as $extension => $codec) {
        if ($extension == "mp4") {
            $streamInfo["stream_info"]["transcode_attributes"]["-scodec"] = "mov_text";
        } elseif ($extension == "mkv") {
            $streamInfo["stream_info"]["transcode_attributes"]["-scodec"] = "srt";
        } else {
            $streamInfo["stream_info"]["transcode_attributes"]["-scodec"] = "copy";
        }

        $symlinkCommand .= implode(" ", self::F6664c80BDe3e9bbe2c12CEb906D5A11($streamInfo["stream_info"]["transcode_attributes"])) . " ";
        $symlinkCommand .= $codec;
    }

    $symlinkCommand .= " >/dev/null 2>" . MOVIES_PATH . $streamId . ".errors & echo \$! > " . MOVIES_PATH . $streamId . "_.pid";
    $symlinkCommand = str_replace(array("{FETCH_OPTIONS}", "{STREAM_SOURCE}", "{READ_NATIVE}"), array(empty($streamArguments) ? '' : $streamArguments, $filePath, empty($streamInfo["stream_info"]["custom_ffmpeg"]) ? $readNativeOption : ''), $symlinkCommand);
    shell_exec($symlinkCommand);
    file_put_contents("/tmp/commands", $symlinkCommand . "\n", FILE_APPEND);
    $pid = intval(file_get_contents(MOVIES_PATH . $streamId . "_.pid"));

    self::$ipTV_db->query("UPDATE `streams_sys` SET `to_analyze` = 1,`stream_started` = '%d',`stream_status` = 0,`pid` = '%d' WHERE `stream_id` = '%d' AND `server_id` = '%d'", time(), $pid, $streamId, SERVER_ID);
    return $pid;
}

Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы