['mp3', 'wav', 'ogg', 'flac', 'aac'], 'video' => ['mp4', 'webm', 'mov', 'avi', 'mkv', 'flv', 'wmv'] ]; // 扫描目录和子目录获取所有支持的媒体文件 function scanMediaDirectory($dir, $root, $supportedFormats) { $files = []; $dir = rtrim($dir, '/') . '/'; // 打开目录 if ($handle = opendir($dir)) { while (($file = readdir($handle)) !== false) { // 跳过.和..目录 if ($file === '.' || $file === '..') { continue; } $path = $dir . $file; // 处理中文文件名 $file = mb_convert_encoding($file, 'UTF-8', 'auto'); // 如果是目录,递归扫描 if (is_dir($path)) { $subFiles = scanMediaDirectory($path, $root, $supportedFormats); $files = array_merge($files, $subFiles); } // 如果是支持的媒体文件 else if (is_file($path)) { $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); // 检查是否为支持的音频或视频格式 $type = ''; if (in_array($ext, $supportedFormats['audio'])) { $type = 'audio'; } elseif (in_array($ext, $supportedFormats['video'])) { $type = 'video'; } if ($type) { // 获取相对于根目录的路径 $relativePath = substr($path, strlen($root)); $relativePath = mb_convert_encoding($relativePath, 'UTF-8', 'auto'); $files[] = [ 'file' => $relativePath, 'path' => dirname($relativePath), // 提取目录路径 'type' => $type, // 文件类型(audio/video) 'ext' => $ext // 文件扩展名 ]; } } } closedir($handle); } return $files; } // 构建目录树结构 function buildDirectoryTree($directories) { $tree = []; foreach ($directories as $dir) { if ($dir === '.') continue; $parts = explode('/', $dir); $current = &$tree; foreach ($parts as $part) { if (empty($part)) continue; $found = false; foreach ($current as &$node) { if ($node['name'] === $part) { $current = &$node['children']; $found = true; break; } } unset($node); // 解除引用 if (!$found) { $newNode = [ 'name' => $part, 'path' => implode('/', array_slice($parts, 0, array_search($part, $parts) + 1)), 'children' => [] ]; $current[] = $newNode; $current = &$current[count($current) - 1]['children']; } } unset($current); // 解除引用 } return $tree; } // 扫描媒体目录 $allFiles = scanMediaDirectory($mediaRoot, $mediaRoot, $supportedFormats); // 提取目录结构 $directories = []; foreach ($allFiles as $fileInfo) { $dirPath = $fileInfo['path']; if ($dirPath !== '.' && !in_array($dirPath, $directories)) { $directories[] = $dirPath; } } // 构建目录树 $directoryTree = buildDirectoryTree($directories); // 按目录路径排序 sort($directories); // 按文件名排序 usort($allFiles, function($a, $b) { return strcmp($a['file'], $b['file']); }); // 统计音视频数量 $audioCount = 0; $videoCount = 0; foreach ($allFiles as $file) { if ($file['type'] === 'audio') $audioCount++; else $videoCount++; } // 返回目录信息和文件列表,确保中文正常编码 echo json_encode([ 'root' => $mediaRoot, 'directories' => $directories, 'directoryTree' => $directoryTree, 'files' => array_column($allFiles, 'file'), 'filesWithPath' => $allFiles, 'audioCount' => $audioCount, 'videoCount' => $videoCount ], JSON_UNESCAPED_UNICODE); // 添加JSON_UNESCAPED_UNICODE确保中文不被编码 exit; } ?> 豆豆播放器

豆豆播放器

0 个目录
0 个音频
0 个视频
当前媒体根目录: ./

视频加载中...

选择媒体文件开始播放

播放列表中的文件

0:00 0:00

媒体目录

  • 加载目录中...

媒体列表

第1页 / 共1页
  • 正在加载媒体列表...