Browse Source

add snapshot for audio

master
filesite 2 months ago
parent
commit
5ccfe01431
  1. 5
      lib/DirScanner.php
  2. 16
      plugins/Html.php
  3. 15
      themes/beauty/controller/ListController.php
  4. 43
      themes/beauty/controller/SiteController.php
  5. 12
      themes/beauty/views/site/index.php
  6. 4
      themes/beauty/views/site/mp3player.php
  7. 7
      www/js/beauty.js

5
lib/DirScanner.php

@ -285,6 +285,7 @@ Class DirScanner { @@ -285,6 +285,7 @@ Class DirScanner {
//合并描述文件内容到md文件或者目录数据
//增加视频文件:mp4, mov, m3u8描述文件支持
//增加.url文件支持
//增加.mp3文件支持
private function mergeDescriptionData($realpath) {
$data = array();
$ext = $this->parseDescriptionFiles($realpath);
@ -296,6 +297,8 @@ Class DirScanner { @@ -296,6 +297,8 @@ Class DirScanner {
$targetFile_mov = preg_replace('/_?[a-z0-9]+\.txt$/U', '.mov', $realpath);
$targetFile_m3u8 = preg_replace('/_?[a-z0-9]+\.txt$/U', '.m3u8', $realpath);
$targetFile_url = preg_replace('/_?[a-z0-9]+\.txt$/U', '.url', $realpath);
$targetFile_mp3 = preg_replace('/_?[a-z0-9]+\.txt$/U', '.mp3', $realpath);
if (file_exists($targetFile_md)) {
$targetFile = $targetFile_md;
}else if (file_exists($targetFile_mp4)) {
@ -306,6 +309,8 @@ Class DirScanner { @@ -306,6 +309,8 @@ Class DirScanner {
$targetFile = $targetFile_m3u8;
}else if (file_exists($targetFile_url)) {
$targetFile = $targetFile_url;
}else if (file_exists($targetFile_mp3)) {
$targetFile = $targetFile_mp3;
}
if (!empty($targetFile) && $targetFile != $realpath) {

16
plugins/Html.php

@ -291,4 +291,20 @@ eof; @@ -291,4 +291,20 @@ eof;
return $sourceType;
}
//根据文件名,找出同名的图片文件
public static function searchImageByFilename($filename, $files, $imgExts = array('jpg', 'jpeg', 'png', 'webp', 'gif', 'svg')) {
$matchedImage = null;
if (!empty($files)) {
foreach($files as $item) {
if ($item['filename'] == $filename && in_array($item['extension'], $imgExts)) {
$matchedImage = $item;
break;
}
}
}
return $matchedImage;
}
}

15
themes/beauty/controller/ListController.php

@ -122,6 +122,7 @@ Class ListController extends Controller { @@ -122,6 +122,7 @@ Class ListController extends Controller {
}
//图片、视频类型筛选支持
$allFiles = $scanResults[$cateId]['files'];
$showType = $this->get('show', 'all');
if ($showType == 'image' && !empty($scanResults[$cateId]['files'])) {
$scanResults[$cateId]['files'] = array_filter($scanResults[$cateId]['files'], function($item) {
@ -247,6 +248,18 @@ Class ListController extends Controller { @@ -247,6 +248,18 @@ Class ListController extends Controller {
}
if (!empty($item['extension']) && in_array($item['extension'], $audioExts)) {
//print_r($subcate['files']);exit;
//为音乐文件获取封面图
if (empty($item['snapshot'])) {
$imgExts = !empty(FSC::$app['config']['supportedImageExts']) ? FSC::$app['config']['supportedImageExts'] : array('jpg', 'jpeg', 'png', 'webp', 'gif');
$matchedImage = Html::searchImageByFilename($item['filename'], $allFiles, $imgExts);
if (!empty($matchedImage)) {
$item['snapshot'] = $matchedImage['path'];
}else {
$item['snapshot'] = '/img/beauty/audio_icon.jpeg?v1';
}
}
array_push($audios, $item);
$index ++;
}
@ -263,7 +276,7 @@ Class ListController extends Controller { @@ -263,7 +276,7 @@ Class ListController extends Controller {
$viewName = '//site/index'; //共享视图
$params = compact(
'cateId', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'breadcrumbs', 'htmlCateReadme',
'mp3File', 'page', 'pageSize', 'cacheDataId', 'copyright', 'showType', 'isAdminIp'
'mp3File', 'page', 'pageSize', 'cacheDataId', 'copyright', 'showType', 'isAdminIp', 'allFiles'
);
return $this->render($viewName, $params, $pageTitle);
}

43
themes/beauty/controller/SiteController.php

@ -155,6 +155,7 @@ Class SiteController extends Controller { @@ -155,6 +155,7 @@ Class SiteController extends Controller {
//mp3支持
$audioExts = !empty(FSC::$app['config']['supportedAudioExts']) ? FSC::$app['config']['supportedAudioExts'] : array('mp3');
$allFiles = $scanResults;
$showType = $this->get('show', 'all');
if ($showType == 'image') {
$scanResults = array_filter($scanResults, function($item) {
@ -174,7 +175,7 @@ Class SiteController extends Controller { @@ -174,7 +175,7 @@ Class SiteController extends Controller {
}
//dataType支持:[image, video]
//dataType支持:[image, video, audio]
$dataType = $this->get('dataType', 'html');
if ($dataType == 'image') {
$imgs = array();
@ -235,6 +236,17 @@ Class SiteController extends Controller { @@ -235,6 +236,17 @@ Class SiteController extends Controller {
}
if (!empty($item['extension']) && in_array($item['extension'], $audioExts)) {
//为音乐文件获取封面图
if (empty($item['snapshot'])) {
$imgExts = !empty(FSC::$app['config']['supportedImageExts']) ? FSC::$app['config']['supportedImageExts'] : array('jpg', 'jpeg', 'png', 'webp', 'gif');
$matchedImage = Html::searchImageByFilename($item['filename'], $allFiles, $imgExts);
if (!empty($matchedImage)) {
$item['snapshot'] = $matchedImage['path'];
}else {
$item['snapshot'] = '/img/beauty/audio_icon.jpeg?v1';
}
}
array_push($audios, $item);
$index ++;
}
@ -249,7 +261,7 @@ Class SiteController extends Controller { @@ -249,7 +261,7 @@ Class SiteController extends Controller {
$params = compact(
'page', 'pageSize', 'cacheDataId', 'showType',
'dirTree', 'scanResults', 'menus', 'htmlReadme', 'htmlCateReadme', 'mp3File', 'copyright',
'alertWarning', 'isAdminIp'
'alertWarning', 'isAdminIp', 'allFiles'
);
return $this->render($viewName, $params, $pageTitle);
}
@ -286,7 +298,7 @@ Class SiteController extends Controller { @@ -286,7 +298,7 @@ Class SiteController extends Controller {
* original - 原图
* small - 缩略图
* vm - 视频封面图(vmeta)
* am - 音乐封面图(ameta)
* am - 音乐封面图
**/
public function actionDirsnap() {
$code = 1;
@ -307,8 +319,8 @@ Class SiteController extends Controller { @@ -307,8 +319,8 @@ Class SiteController extends Controller {
$cacheSubDir = 'dir';
$withCreateTime = true; //返回数据的缓存时间
$cache = Common::getCacheFromFile($cacheKey, $expireSeconds, $cacheSubDir, $withCreateTime);
$cachedData = $cache['data'];
$cachedCtime = $cache['ctime'];
$cachedData = !empty($cache) ? $cache['data'] : null;
$cachedCtime = !empty($cache) ? $cache['ctime'] : 0;
$now = time();
//如果关闭缩略图
@ -318,7 +330,7 @@ Class SiteController extends Controller { @@ -318,7 +330,7 @@ Class SiteController extends Controller {
}
}else if ( !empty($cachedData) && !empty($cachedData['size']) && in_array($cachedData['size'], array('vm', 'am')) ) {
//如果是视频、音乐封面图,则缓存 10 分钟
if ($now - $cachedCtime > 600) {
if ($cachedCtime > 0 && $now - $cachedCtime > 600) {
$cachedData = null;
}
}
@ -635,6 +647,23 @@ Class SiteController extends Controller { @@ -635,6 +647,23 @@ Class SiteController extends Controller {
$videoExtension = pathinfo($arr['path'], PATHINFO_EXTENSION);
$videoSourceType = Html::getMediaSourceType($videoExtension);
//从缓存数据获取封面图
$poster = '/img/beauty/audio_bg.jpg';
$cacheSeconds = 86400;
$cachedParentData = Common::getCacheFromFile($cacheParentDataId, $cacheSeconds);
if (!empty($cachedParentData)) {
$mp3 = $cachedParentData[$videoId];
if (!empty($mp3['snapshot'])) {
$poster = $mp3['snapshot'];
}else {
$imgExts = !empty(FSC::$app['config']['supportedImageExts']) ? FSC::$app['config']['supportedImageExts'] : array('jpg', 'jpeg', 'png', 'webp', 'gif');
$matchedImage = Html::searchImageByFilename($mp3['filename'], $cachedParentData, $imgExts);
if (!empty($matchedImage)) {
$poster = $matchedImage['path'];
}
}
}
//获取联系方式
$maxScanDeep = 0; //最大扫描目录级数
@ -656,7 +685,7 @@ Class SiteController extends Controller { @@ -656,7 +685,7 @@ Class SiteController extends Controller {
$params = compact(
'videoUrl', 'videoId', 'videoFilename',
'cateId', 'cacheParentDataId', 'page', 'pageSize',
'copyright', 'isAdminIp', 'videoExtension', 'videoSourceType'
'copyright', 'isAdminIp', 'videoExtension', 'videoSourceType', 'poster'
);
return $this->render($viewName, $params, $pageTitle);
}

12
themes/beauty/views/site/index.php

@ -407,10 +407,20 @@ eof; @@ -407,10 +407,20 @@ eof;
$linkUrl .= "&page={$viewData['page']}&limit={$viewData['pageSize']}";
}
$snapshot = '/img/beauty/audio_icon.jpeg';
if (!empty($file['snapshot'])) {
$snapshot = $file['snapshot'];
}else { //尝试找出同名的图片文件
$matchedImage = Html::searchImageByFilename($file['filename'], $viewData['allFiles'], $imgExts);
if (!empty($matchedImage)) {
$snapshot = $matchedImage['path'];
}
}
echo <<<eof
<div class="im_item bor_radius col-xs-6 col-sm-4 col-md-3 col-lg-2 audio-list-item">
<a href="{$linkUrl}" target="_blank" class="bor_radius vercenter" title="{$title} - {$file['filename']}">
<img src="/img/beauty/audio_icon.jpeg" class="bor_radius im_img video-poster" id="poster_{$file['id']}"
<img src="{$snapshot}" class="bor_radius im_img video-poster" id="poster_{$file['id']}"
data-video-id="{$file['id']}"
data-video-url="{$file['path']}"
alt="{$file['filename']}">

4
themes/beauty/views/site/mp3player.php

@ -56,7 +56,7 @@ @@ -56,7 +56,7 @@
controls
playsinline
data-setup='{"inactivityTimeout":0}'
poster="/img/beauty/audio_bg.jpg"
poster="<?php echo $viewData['poster']; ?>"
data-src="<?php echo $viewData['videoUrl']; ?>"
data-type="<?php echo $viewData['videoSourceType']; ?>"
id="my-player">
@ -79,7 +79,7 @@ @@ -79,7 +79,7 @@
<div class="col-xs-12">
<div class="mb-1 audio-item clearfix">
<a href="{videoUrl}" title="{title}">
<img src="/img/beauty/audio_icon.jpeg?v1" class="bor_radius video-poster" id="poster_{videoId}"
<img src="{snapshot}" class="bor_radius video-poster" id="poster_{videoId}"
width="60" height="60"
data-video-id="{videoId}"
data-video-url="{videoPath}"

7
www/js/beauty.js

@ -537,7 +537,7 @@ var getVideoMetaAndShowIt = function(videoId, videoUrl) { @@ -537,7 +537,7 @@ var getVideoMetaAndShowIt = function(videoId, videoUrl) {
if (data.code != 1) {
console.warn('no meta data', data.msg);
noSnapVideos.push({id: videoId, url: videoUrl});
}else {
}else { //jQuery会兼容传值为undefined的情况
$('#poster_'+videoId).attr('src', data.meta.snapshot);
$('#poster_'+videoId).parent('a').find('.duration').text(formatDuration(data.meta.duration));
$('#poster_'+videoId).parent('a').find('.playbtn').removeClass('hide');
@ -685,6 +685,11 @@ if ($('#my-player').length > 0 && typeof(videojs) != 'undefined') { @@ -685,6 +685,11 @@ if ($('#my-player').length > 0 && typeof(videojs) != 'undefined') {
tmp = tmp.replace(/\{videoId\}/ig, videos[index].id);
tmp = tmp.replace(/\{videoPath\}/ig, videos[index].path);
//支持mp3的封面图显示
if (typeof(videos[index].snapshot) != 'undefined') {
tmp = tmp.replace(/\{snapshot\}/ig, videos[index].snapshot);
}
html += tmp;
}

Loading…
Cancel
Save