smartideo 修改及分享哔哩哔哩视频自适应优化

1、前言

在写博客的时候难免会插入或借用一些三方视频平台的视频,而 smartideo 就是这样一款适用于 WordPress 的插件,然而该插件已经许久未更新修复了,在使用的时候发现并不能识别 B 站的视频,而且不能自适应。好在是开源的,不过我还是直接在 Begin 主题文件里提取的代码加以修改做成插件的形式了。

2、核心代码

<?php
    $addvideo = new addvideo();
class addvideo{
	private $edit = false;
	private $width = '100%';
	private $height = '500px';
	private $youku_client_id = 'd0b1b77a17cded3b';
	public function __construct(){
		if(!empty($youku_client_id) && strlen($youku_client_id) == 16){
			$this->youku_client_id = $youku_client_id;
		}

		wp_embed_unregister_handler('youku');
		wp_embed_unregister_handler('56com');
		wp_embed_unregister_handler('youtube');

		// video
		wp_embed_register_handler( 'addvideo_youku',
			'#https?://(?:[v|vo]+)\.youku\.com/v_show/id_(?<video_id>[a-z0-9_=\-]+)#i',
			array($this, 'addvideo_embed_handler_youku') );

		wp_embed_register_handler( 'addvideo_qq',
			'#https?://v\.qq\.com/(?:[a-z0-9_\./]+\?vid=(?<video_id1>[a-z0-9_=\-]+)|(?:[a-z0-9/]+)/(?<video_id2>[a-z0-9_=\-]+))#i',
			array($this, 'addvideo_embed_handler_qq') );

		wp_embed_register_handler( 'addvideo_wasu',
			'#https?://www\.wasu\.cn/play/show/id/(?<video_id>\d+)#i',
			array($this, 'addvideo_embed_handler_wasu') );

        wp_embed_register_handler( 'addvideo_youtube',
			'#https?://www\.youtube\.com/watch\?v=(?<video_id>[a-zA-Z0-9_=\-]+)#i',
			array($this, 'addvideo_embed_handler_youtube') );

		wp_embed_register_handler( 'addvideo_bilibili',
			'#https?://www\.bilibili\.com/video/av(?:(?<video_id1>\d+)/(?:index_|\#page=)(?<video_id2>\d+)|(?<video_id>\d+))#i',
			array($this, 'addvideo_embed_handler_bilibili') );

		wp_embed_register_handler( 'addvideo_bilibili_bv',
			'#https?://www\.bilibili\.com/video/BV(?:(?<video_id1>[a-z0-9_=\-]+)/(?:index_|\#page=)(?<video_id2>[a-z0-9_=\-]+)|(?<video_id>[a-z0-9_=\-]+))#i',
			array($this, 'addvideo_embed_handler_bilibili_bv') );

		wp_embed_register_handler( 'addvideo_iqiyi',
			'#https?://www\.iqiyi\.com/(?:[a-zA-Z]+)_(?<video_id>[a-z0-9_~\-]+)#i',
			array($this, 'addvideo_embed_handler_iqiyi') );

        // music
		wp_embed_register_handler( 'addvideo_music163',
			'#https?://music\.163\.com/\#/song\?id=(?<video_id>\d+)#i',
			array($this, 'addvideo_embed_handler_music163') );

		wp_embed_register_handler( 'addvideo_musicqq',
			'#https?://y\.qq\.com/n/yqq/song/(?<video_id>\w+)\.html#i',
			array($this, 'addvideo_embed_handler_musicqq') );

		wp_embed_register_handler( 'addvideo_xiami',
			'#https?://www\.xiami\.com/song/(?<video_id>\d+)#i',
			array($this, 'addvideo_embed_handler_xiami') );
	}

    # video
	public function addvideo_embed_handler_youku( $matches, $attr, $url, $rawattr ) {
		$embed = $this->get_iframe("//player.youku.com/embed/{$matches['video_id']}?client_id={$this->youku_client_id}", $url);
		return apply_filters( 'embed_youku', $embed, $matches, $attr, $url, $rawattr );
	}

	public function addvideo_embed_handler_qq( $matches, $attr, $url, $rawattr ) {
		$matches['video_id'] = $matches['video_id1'] == '' ? $matches['video_id2'] : $matches['video_id1'];
		$embed = $this->get_iframe("//v.qq.com/iframe/player.html?vid={$matches['video_id']}&auto=0", $url);
		return apply_filters( 'embed_qq', $embed, $matches, $attr, $url, $rawattr );
	}

	public function addvideo_embed_handler_wasu( $matches, $attr, $url, $rawattr ) {
		$embed = $this->get_iframe("http://www.wasu.cn/Play/iframe/id/{$matches['video_id']}", $url);
		return apply_filters( 'embed_wasu', $embed, $matches, $attr, $url, $rawattr );
	}

	public function addvideo_embed_handler_youtube( $matches, $attr, $url, $rawattr ) {
		$embed = $this->get_iframe("//www.youtube.com/embed/{$matches['video_id']}", $url);
		return apply_filters( 'embed_youtube', $embed, $matches, $attr, $url, $rawattr );
	}

	public function addvideo_embed_handler_bilibili( $matches, $attr, $url, $rawattr ) {
		$matches['video_id'] = ($matches['video_id1'] == '') ? $matches['video_id'] : $matches['video_id1'];
		$page = ($matches['video_id2'] > 1) ? $matches['video_id2'] : 1;
		$cid = '';
		$embed = $this->get_iframe("//player.bilibili.com/player.html?aid={$matches['video_id']}&cid={$cid}&page={$page}&as_wide=1&high_quality=1&danmaku=0", $url);
		return apply_filters( 'embed_bilibili', $embed, $matches, $attr, $url, $rawattr );
	}

	public function addvideo_embed_handler_bilibili_bv( $matches, $attr, $url, $rawattr ) {
		$matches['video_id'] = ($matches['video_id1'] == '') ? $matches['video_id'] : $matches['video_id1'];
		$page = ($matches['video_id2'] > 1) ? $matches['video_id2'] : 1;
		$cid = '';
		$embed = $this->get_iframe("//player.bilibili.com/player.html?bvid=BV{$matches['video_id']}&cid={$cid}&page={$page}&as_wide=1&high_quality=1&danmaku=0", $url);
		return apply_filters( 'embed_bilibili', $embed, $matches, $attr, $url, $rawattr );
	}

	public function addvideo_embed_handler_iqiyi( $matches, $attr, $url, $rawattr ) {
		$embed = '';
		try{
			$request = new WP_Http();
			$data = (array)$request->request($url, array('timeout' => 3));
			if(!isset($data['body'])){
				$data['data'] = '';
			}
			preg_match('/"vid":"(\w+)"/i', (string)$data['body'], $match);
			$vid = $match[1];
			preg_match('/"tvId":(\d+)/i', (string)$data['body'], $match);
			$tvid = $match[1];
			if ($tvid > 0 && !empty($vid)) {
				$embed = $this->get_iframe("//open.iqiyi.com/developer/player_js/coopPlayerIndex.html?vid={$vid}&tvId={$tvid}&height=100%&width=100%&autoplay=0", $url);
			}
		}catch(Exception $e){}
		if(empty($embed)){
			$embed = '解析失败,请刷新页面重试';
		}
		return apply_filters( 'embed_iqiyi', $embed, $matches, $attr, $url, $rawattr );
	}

	# video widthout h5
	public function addvideo_embed_handler_tudou( $matches, $attr, $url, $rawattr ) {
		$embed = $this->get_link($url);
		return apply_filters( 'embed_tudou', $embed, $matches, $attr, $url, $rawattr );
	}
    
	# music
	public function addvideo_embed_handler_music163( $matches, $attr, $url, $rawattr ) {
		$embed = $this->get_iframe("//music.163.com/outchain/player?type=2&id={$matches['video_id']}&auto=0&height=90", '', '100%', '110px');
		return apply_filters( 'embed_music163', $embed, $matches, $attr, $url, $rawattr );
	}

	public function addvideo_embed_handler_musicqq( $matches, $attr, $url, $rawattr ) {
		$embed = $this->get_iframe("//cc.stream.qqmusic.qq.com/C100{$matches['video_id']}.m4a?fromtag=52", '', '100%', '110px');
		return apply_filters( 'embed_musicqq', $embed, $matches, $attr, $url, $rawattr );
	}

	public function addvideo_embed_handler_xiami( $matches, $attr, $url, $rawattr ) {
		$embed =
			'<div class="smartvideo" style="background: transparent;">
				<script src="http://www.xiami.com/widget/player-single?uid=0&sid='.$matches['video_id'].'&autoplay=0&mode=js" type="text/javascript"></script>
			</div>';
		return apply_filters( 'embed_xiami', $embed, $matches, $attr, $url, $rawattr );
	}

	private function get_iframe($url = '', $source = ''){
		$html = '';
		$html .='<div class="smartideo"><div class="player"><iframe src="' . $url . '" width="100%" height="100%" frameborder="0" allowfullscreen="true"></iframe></div>';
		$html .= '</div>';
		return $html;
	}
	private function is_https(){
		if(strtolower($_SERVER['HTTPS']) == 'on'){
			return true;
		}else{
			return false;
		}
	}
}

3、自适应 css

.smartideo { z-index: 0; text-align: center; background: #CCC; line-height: 0; text-indent: 0; }
.smartideo embed, .smartideo iframe { padding: 0; margin: 0; }
.smartideo .player { width: 100%; height: 500px; overflow: hidden; position: relative; }
.smartideo .player a.smartideo-play-link { display: block; width: 50px; height: 50px; text-decoration: none; border: 0; position: absolute; left: 50%; top: 50%; margin: -25px; }
.smartideo .player a.smartideo-play-link p { display: none; }
.smartideo .player .smartideo-play-button { width: 0; height: 0; border-top: 25px solid transparent; border-left: 50px solid #FFF; border-bottom: 25px solid transparent; }
.smartideo .tips { background: #f2f2f2; text-align: center; max-height: 32px; line-height: 32px; font-size: 12px; }
.smartideo .tips a { text-decoration: none; }
 
@media screen and (max-width:959px){
    .smartideo .player { height: 450px; }
}
@media screen and (max-width:767px){
    .smartideo .player { height: 400px; }
}
@media screen and (max-width:639px){
    .smartideo .player { height: 350px; }
}
@media screen and (max-width:479px){
    .smartideo .player { height: 250px; }
}

4、修改说明

我是不会编程的小白,一直想要学习的也没自觉的去学,所以折腾了很久都没成功,毕竟有些函数不知道是什么意思不过还好最终搞定了!

该修改主要针对 B 站视频,因为我主要用 B 站进行分享,至于原因嘛:高清无广告!

4.1、功能说明

1、视频支持自适应页面

2、视频清晰度默认为 1080p+

3、去除视频播放弹幕

5、插件下载

Github: https://github.com/adream2/smartvideo

蓝奏云:https://winmx.lanzous.com/i8mxzfycuab

暂无评论

发送评论 编辑评论


上一篇
下一篇