Files
nexusphp/nexus/Torrent/TechnicalInformation.php

308 lines
11 KiB
PHP
Raw Normal View History

2021-03-16 21:12:27 +08:00
<?php
namespace Nexus\Torrent;
class TechnicalInformation
{
private $mediaInfo;
private $mediaInfoArr;
2021-03-18 20:32:35 +08:00
public function __construct(string $mediaInfo)
2021-03-16 21:12:27 +08:00
{
2021-03-18 20:32:35 +08:00
$this->mediaInfo = $mediaInfo;
$this->mediaInfoArr = $this->getMediaInfoArr($mediaInfo);
2021-03-16 21:12:27 +08:00
}
public function getMediaInfoArr(string $mediaInfo)
{
$arr = preg_split('/[\r\n]+/', $mediaInfo);
$result = [];
$parentKey = "";
foreach ($arr as $key => $value) {
2025-06-29 20:47:23 +07:00
$value = $this->trim($value);
2021-03-16 21:12:27 +08:00
if (empty($value)) {
continue;
}
$rowKeyValue = explode(':', $value);
2025-06-29 20:47:23 +07:00
$rowKeyValue = array_filter(array_map([$this, 'trim'], $rowKeyValue));
2021-03-16 21:12:27 +08:00
if (count($rowKeyValue) == 1) {
$parentKey = $rowKeyValue[0];
} elseif (count($rowKeyValue) == 2) {
if (empty($parentKey)) {
continue;
}
$result[$parentKey][$rowKeyValue[0]] = $rowKeyValue[1];
}
}
return $result;
}
2025-06-29 20:47:23 +07:00
private function trim(string $value): string
{
return trim($value, " \n\r\t\v\0\u{A0}");
}
2021-03-16 21:12:27 +08:00
public function getRuntime()
{
return $this->mediaInfoArr['General']['Duration'] ?? '';
}
public function getResolution()
{
$width = $this->mediaInfoArr['Video']['Width'] ?? '';
$height = $this->mediaInfoArr['Video']['Height'] ?? '';
$ratio = $this->mediaInfoArr['Video']['Display aspect ratio'] ?? '';
2021-03-18 20:32:35 +08:00
$result = '';
if ($width && $height) {
$result .= $width . ' x ' . $height;
}
2021-03-16 21:12:27 +08:00
if ($ratio) {
$result .= "($ratio)";
}
return $result;
}
public function getBitrate()
{
$result = $this->mediaInfoArr['Video']['Bit rate'] ?? '';
return $result;
}
public function getFramerate()
{
$result = $this->mediaInfoArr['Video']['Frame rate'] ?? '';
return $result;
}
public function getProfile()
{
$result = $this->mediaInfoArr['Video']['Format profile'] ?? '';
return $result;
}
public function getRefFrame()
{
2021-06-14 13:16:21 +08:00
foreach ($this->mediaInfoArr['Video'] ?? [] as $key => $value) {
2022-08-26 17:35:49 +08:00
if (str_contains($key, 'Reference frames')) {
2021-03-16 21:12:27 +08:00
return $value;
}
}
return '';
}
public function getAudios()
{
$result = [];
$audioIndex = 1;
2021-03-16 21:12:27 +08:00
foreach ($this->mediaInfoArr as $parentKey => $values) {
2021-03-18 20:32:35 +08:00
if (strpos($parentKey, 'Audio') === false) {
2021-03-16 21:12:27 +08:00
continue;
}
$audioInfoArr = [];
if (!empty($values['Language'])) {
$audioInfoArr[] = $values['Language'];
}
if (!empty($values['Title'])) {
$audioInfoArr[] = $values['Title'];
}
2021-03-16 21:12:27 +08:00
if (!empty($values['Format'])) {
$audioInfoArr[] = $values['Format'];
}
if (!empty($values['Channel(s)'])) {
$audioInfoArr[] = $values['Channel(s)'];
}
if (!empty($values['Bit rate'])) {
$audioInfoArr[]= "@" . $values['Bit rate'];
}
if (!empty($audioInfoArr)) {
// 使用多语言支持的键名
$result[nexus_trans('torrent.technicalinfo_audio') . $audioIndex] = implode(" ", $audioInfoArr);
$audioIndex++;
2021-03-16 21:12:27 +08:00
}
}
return $result;
}
public function getSubtitles()
{
$result = [];
$subtitleIndex = 1;
2021-03-16 21:12:27 +08:00
foreach ($this->mediaInfoArr as $parentKey => $values) {
2021-03-18 20:32:35 +08:00
if (strpos($parentKey, 'Text') === false) {
2021-03-16 21:12:27 +08:00
continue;
}
$subtitlesInfoArr = [];
2021-03-16 21:12:27 +08:00
if (!empty($values['Language'])) {
$subtitlesInfoArr[] = $values['Language'];
}
if (!empty($values['Title'])) {
$subtitlesInfoArr[] = $values['Title'];
2021-03-16 21:12:27 +08:00
}
if (!empty($values['Format'])) {
$subtitlesInfoArr[] = $values['Format'];
2021-03-16 21:12:27 +08:00
}
if (!empty($subtitlesInfoArr)) {
// 使用多语言支持的键名
$result[nexus_trans('torrent.technicalinfo_subtitles') . $subtitleIndex] = implode(" ", $subtitlesInfoArr);
$subtitleIndex++;
2021-03-16 21:12:27 +08:00
}
}
return $result;
}
2022-08-26 17:35:49 +08:00
public function getHDRFormat()
{
return $this->mediaInfoArr['Video']['HDR format'] ?? '';
}
public function getVideoFormat()
{
return $this->mediaInfoArr['Video']['Format'] ?? '';
}
2022-08-26 17:35:49 +08:00
public function getBitDepth()
{
return $this->mediaInfoArr['Video']['Bit depth'] ?? '';
}
2021-03-16 21:12:27 +08:00
public function renderOnDetailsPage()
{
global $lang_functions;
2025-04-17 01:39:40 +07:00
// $videos = [
// 'Runtime' => $this->getRuntime(),
// 'Resolution' => $this->getResolution(),
// 'Bitrate' => $this->getBitrate(),
// 'HDR' => $this->getHDRFormat(),
// 'Bit depth' => $this->getBitDepth(),
// 'Frame rate' => $this->getFramerate(),
// 'Profile' => $this->getProfile(),
// 'Ref.Frames' => $this->getRefFrame(),
// ];
// $videos = array_filter($videos);
// $audios = $this->getAudios();
// $subtitles = $this->getSubtitles();
$summaryInfo = $this->getSummaryInfo();
$videos = $summaryInfo['videos'] ?: [];
$audios = $summaryInfo['audios'] ?: [];
$subtitles = $summaryInfo['subtitles'] ?: [];
// dd($summaryInfo, $videos, $audios, $subtitles);
if (empty($this->mediaInfo)) { //为空不渲染mediainfo
return '';
} else if (empty($videos) && empty($audios) && empty($subtitles)) { // 信息不全显示点击展开
$rawmediaInfo = sprintf('[spoiler=%s][raw]<pre>%s</pre>[/raw][/spoiler]', nexus_trans('torrent.show_hide_media_info'), $this->mediaInfo);
return sprintf('<div class="nexus-media-info-raw"><pre>%s</pre></div>', format_comment($rawmediaInfo, false));
2021-03-18 20:32:35 +08:00
}
2021-06-03 11:47:56 +08:00
$result = '<table style="border: none;width: 100%"><tbody><tr>';
2022-09-19 18:48:03 +08:00
$cols = 0;
2021-03-18 20:32:35 +08:00
if (!empty($videos)) {
2022-09-19 18:48:03 +08:00
$cols++;
2021-06-03 11:47:56 +08:00
$result .= $this->buildTdTable($videos);
2021-03-18 20:32:35 +08:00
}
if (!empty($audios)) {
2022-09-19 18:48:03 +08:00
$cols++;
2021-06-03 11:47:56 +08:00
$result .= $this->buildTdTable($audios);
2021-03-18 20:32:35 +08:00
}
if (!empty($subtitles)) {
2022-09-19 18:48:03 +08:00
$cols++;
2021-06-03 11:47:56 +08:00
$result .= $this->buildTdTable($subtitles);
2021-03-18 20:32:35 +08:00
}
2022-09-19 18:48:03 +08:00
$result .= '</tr>';
$rawMediaInfo = sprintf('[spoiler=%s][raw]<pre>%s</pre>[/raw][/spoiler]', nexus_trans('torrent.show_hide_media_info'), $this->mediaInfo);
$result .= sprintf('<tr><td colspan="%s" class="nexus-media-info-raw">%s</td></tr>', $cols, format_comment($rawMediaInfo, false));
$result .= '</tbody></table>';
2021-03-18 20:32:35 +08:00
return $result;
2021-03-16 21:12:27 +08:00
}
2021-06-03 11:47:56 +08:00
2025-04-17 01:39:40 +07:00
public function getSummaryInfo(): array
{
$videos = [
nexus_trans('torrent.technicalinfo_duration') => $this->getRuntime(),
nexus_trans('torrent.technicalinfo_resolution') => $this->getResolution(),
nexus_trans('torrent.technicalinfo_bit_rate') => $this->getBitrate(),
2025-04-17 01:39:40 +07:00
'HDR' => $this->getHDRFormat(),
nexus_trans('torrent.technicalinfo_bit_depth') => $this->getBitDepth(),
nexus_trans('torrent.technicalinfo_frame_rate') => $this->getFramerate(),
nexus_trans('torrent.technicalinfo_profile') => $this->getProfile(),
nexus_trans('torrent.technicalinfo_format') => $this->getVideoFormat(),
nexus_trans('torrent.technicalinfo_ref_frames') => $this->getRefFrame(),
2025-04-17 01:39:40 +07:00
];
$videos = array_filter($videos) ?: null;
$audios = $this->getAudios() ?: null;
$subtitles = $this->getSubtitles() ?: null;
return compact('videos', 'audios', 'subtitles');
}
2021-06-03 11:47:56 +08:00
private function buildTdTable(array $parts)
{
$table = '<table style="border: none;"><tbody>';
// 检查是否为音频或字幕数据
$isAudioOrSubtitle = false;
$audioOrSubtitleCount = 0;
$audioPrefix = nexus_trans('torrent.technicalinfo_audio');
$subtitlePrefix = nexus_trans('torrent.technicalinfo_subtitles');
2021-06-03 11:47:56 +08:00
foreach ($parts as $key => $value) {
if (strpos($key, $audioPrefix) === 0 || strpos($key, $subtitlePrefix) === 0) {
$isAudioOrSubtitle = true;
$audioOrSubtitleCount++;
}
}
$displayCount = 0;
$hiddenParts = [];
foreach ($parts as $key => $value) {
$displayCount++;
// 如果是音频或字幕且超过3条则隐藏多余的
if ($isAudioOrSubtitle && $audioOrSubtitleCount > 3) {
if ($displayCount <= 3) {
// 显示前3条
$table .= '<tr>';
$table .= sprintf('<td style="border: none; padding-right: 5px;padding-bottom: 5px;"><b>%s: </b>%s</td>', $key, $value);
$table .= '</tr>';
} else {
// 收集隐藏的部分
$hiddenParts[$key] = $value;
}
} else {
// 非音频/字幕数据或数量不超过3条正常显示
$table .= '<tr>';
$table .= sprintf('<td style="border: none; padding-right: 5px;padding-bottom: 5px;"><b>%s: </b>%s</td>', $key, $value);
$table .= '</tr>';
}
}
// 如果有隐藏的部分添加spoiler
if (!empty($hiddenParts)) {
$hiddenContent = '';
foreach ($hiddenParts as $key => $value) {
$hiddenContent .= sprintf('<b>%s: </b>%s<br>', $key, $value);
}
$hiddenContent = rtrim($hiddenContent, '<br>');
$spoilerTitle = $isAudioOrSubtitle && strpos(array_keys($parts)[0], $audioPrefix) === 0
? nexus_trans('torrent.collapse_show_more_audio')
: nexus_trans('torrent.collapse_show_more_subtitles');
$spoiler = sprintf('[spoiler=%s]%s[/spoiler]', $spoilerTitle, $hiddenContent);
2021-06-03 11:47:56 +08:00
$table .= '<tr>';
// 检查format_comment函数是否存在
if (function_exists('format_comment')) {
$table .= sprintf('<td style="border: none; padding-right: 5px;padding-bottom: 5px;">%s</td>', format_comment($spoiler, false));
} else {
$table .= sprintf('<td style="border: none; padding-right: 5px;padding-bottom: 5px;">%s</td>', $spoiler);
}
2021-06-03 11:47:56 +08:00
$table .= '</tr>';
}
2022-09-19 18:48:03 +08:00
$table .= '</tbody>';
$table .= '</table>';
2021-06-03 11:47:56 +08:00
return sprintf('<td style="border: none; padding-right: 5px;padding-bottom: 5px">%s</td>', $table);
}
}