我的网站正在使用 ffmpeg 为每个上传的视频生成缩略图 jpg。代码如下:
// get the videos uploaded
foreach ($videos as $video) {
if ($profile_author_id == $userid || current_user_can('level_10')) {
$imagebuttons = '<span class="edit-buttons"><span class="icon button-delete icon-cancel rad3"></span></span>';
}
echo '<div class="profile-video-thumb-wrapper"><div class="profile-img-thumb profile-video-thumb rad3" id="'.$video->ID.'" style="background: url('.$video->guid.'.jpg) center no-repeat; background-size: cover;">';
echo $imagebuttons;
if(get_post_meta($video->ID, 'processing', true) && !is_video_processing_running(get_post_meta($video->ID, 'processing', true))) {
delete_post_meta($video->ID, 'processing');
unlink(get_post_meta($video->ID, "original_file", true));
delete_post_meta($video->ID, 'original_file');
}
$file_path = get_attached_file($video->ID);
$file_path_thumb = $file_path.".jpg";
if(!file_exists($file_path_thumb)) {
$output = shell_exec("/usr/local/bin/ffmpeg -i $file_path");
$videoresizeheight = get_option("videoresizeheight") ? get_option("videoresizeheight") : '400';
$comd = "/usr/local/bin/ffmpeg -y -i \"$file_path\" -f mjpeg -vframes 1 -ss 00:00:03.000 -vf scale=".$videoresizeheight.":-1 \"$file_path_thumb\" 2>&1";
shell_exec($comd);
}
if(get_post_meta($video->ID, 'processing', true)) {
if ($profile_author_id == $userid || current_user_can('level_10')) {
echo '<span class="video-processing rad3">'._d('this video is still processing',1269).'</span>';
echo '<img data-original-url="'.get_template_directory_uri().'/i/video-placeholder.svg" class="mobile-ready-img rad3" alt="'.get_the_title().'" data-responsive-img-url="'.get_template_directory_uri().'/i/video-placeholder-mobile.svg" />';
}
} else {
echo '<div id="'.preg_replace("/([^a-zA-Z0-9])/", "", $video->post_title).'" class="video-player-lightbox text-center hide" itemprop="video" itemscope itemtype="http://schema.org/VideoObject">';
echo '<meta itemprop="thumbnailUrl" content="'.$video->guid.'.jpg" />';
echo '<meta itemprop="contentURL" content="'.$video->guid.'" />';
echo '<video height="100%" width="100%" controls>';
echo '<source src="'.$video->guid.'" type="video/mp4">';
echo _d("Your browser does not support the video tag.",1270);
echo '</video> ';
echo '</div>';
echo '<a href="#'.preg_replace("/([^a-zA-Z0-9])/", "", $video->post_title).'" rel="profile-video">';
echo '<img src="'.$video->guid.'.jpg" class="hide" />';
echo '<img src="'.get_template_directory_uri().'/i/video-placeholder.svg" class="video-image-play" />';
echo '</a>';
}
echo '<div class="clear"></div></div></div>'."\n";
}
if(count($videos) > 0) {
echo '<div class="clear10"></div>';
}
因此,这会为每个上传的视频生成一个缩略图 jpg 文件;但是当jpg的文件名包含一个从原始视频文件中带出的点时就会出现问题,例如我上传了一个文件名为video.mp4的视频,这将生成一个jpg文件调用video.mp4.jpg。而这个,我的网站将把这个文件视为不存在。
现在我需要找到一个解决方案,它可以生成正确的缩略图文件名,同时仍然坚持使用 wordpress 标签,即在添加额外的 .jpg 扩展名时将 .mp4 更改为 -mp4。