最近公司作一个视频活动的HTML5页面,页面并不复杂,可是要求视频播放的时候不全屏。在网上看到好论坛相似的基本问题,之前有申请白名单的,在白名单的视频连接或者腾讯旗下的视频连接是原生播放,不然安卓会被劫持成腾讯家的播放器播放而且默认全屏,目前经过白名单的方法是解决不了的。还好终于找到了解决办法,下面好好整理一下。
<video id="videos" playsinline="true" poster="img/1.jpg" webkit-playsinline="true" x-webkit-airplay="true" x5-video-player-type="h5" x5-video-player-fullscreen="true"> <source src="img/output.mp4" type="video/mp4" /> </video>
也就是所谓的开启同层播放器html
x5-video-player-type="h5" /*让video开启同层H5播放器*/ x5-video-player-fullscreen="true" /*设置为 true 是防止横屏*/ playsinline="true" 和 webkit-playsinline="true" /*这个属性是ios中设置可让视频在小窗内播放*/
上面的方法试了,在ios中能够不全屏,可是在安卓的微信中打开依然是全屏的。。。ios
视频解码最好用FFmpeg转码,能够转mp四、mpeg、mkv....,还能够提取视频中的音乐。git
mp4转MP4github
ffmpeg -i D:\Projects\cat.mp4 -vcodec libx264 -pass 1 -coder 0 -bf 0 -flags -loop -wpredp 0 out.mp4
mkv转MP4web
ffmpeg -i D:\Projects\cat.mkv -c:v copy -c:a copy cat.mp4
提取音乐canvas
ffmpeg -i D:\Projects\cat.mp4 -f mp3 -vn apple.mp3
mp4转mpeg,视频的宽度必须是2的倍数数组
ffmpeg -i D:\Projects\cat.mp4 -f mpeg1video -vf "crop=iw-mod(iw\,2):ih-mod(ih\,2)" -b 0 cat.mpg
其余使用方法能够到官网中查找,这里就很少介绍了。微信
Broadway是从其余语言翻译而成,这个解码器支持 mp4 后缀的视频文件,有时候即便是手机拍摄的mp4格式的视频也没什么用,最好仍是用ffmpeg再转一下格式。视频的地址最好是在线的地址,本地测试的时候本地视频在iphone上播放不了。app
document.querySelector('body').addEventListener('click', function() { var player = new MP4Player(new Stream('img/cat.mp4'), false, '', 'auto'); player.play(); document.querySelector('#containerMP4').innerHTML = ''; document.querySelector('#containerMP4').appendChild(player.canvas); });
jsmpeg是一个 MPEG1 解码器,我的以为代码比较轻量。iphone
var canvas = document.getElementById('canvas2'),off1 = true; var audio = document.getElementById("bgMusic"); $('#canvas2').on("click",function(e){ e.preventDefault(); if(off1){ off1 = false; document.querySelector('#loadWrap2').style.display = 'block'; var player = new jsmpeg('img/cat.mpg', { canvas: canvas, onload : function(){ document.querySelector('#loadWrap2').style.display = 'none'; player.play(); }, onfinished : function(){ off1 = true; } }); } })
以上两种方法均可以实现视频的不全屏播放,终于解决了一个大问题,可是对于专门作视频开发的不知道适不适用了。
另外还有一种方法,就是将图片转成一个个帧了,我的以为有点麻烦,可是对于短视频来说也能够采用。麻烦的就是用工具将视频转成图片了。
var imgArr = [],source = {},now2 = 0,imgNum = 0,timer=null; var canvas2 = document.querySelector('#canvas2'); var videoBox = document.querySelector('.videoBox'); var view = {w : videoBox.offsetWidth,h : videoBox.offsetHeight}; canvas2.width = view.w; canvas2.height = view.h; var ctx = canvas2.getContext("2d"); ctx.clearRect(0,0,canvas2.width,canvas2.height); var audio = document.getElementById("bgMusic"); //添加图片进数组 function pushImgArr (num) { document.querySelector('#loadWrap2').style.display = 'block'; //播放(继续播放) audio.play(); imgArr = []; for( var i = 0;i < num;i++ ) { imgArr.push('videoImg/'+i+'.jpg'); }; imgLoad (); }; //图片加载 function imgLoad(){ document.querySelector('#btn-play').style.display = 'none'; source['src'+now2] = new Image(); source['src'+now2].src = imgArr[now2]; source['src'+now2].onload = function(){ now2 ++ ; if( now2 > imgArr.length-1 ){ //加载成功 document.querySelector('#loadWrap2').style.display = 'none'; //执行canvas渲染 movieInit() }else{ //递归加载 imgLoad(); }; }; }; //canvas图片渲染 function movieInit (){ clearInterval(timer); timer = setInterval(function(){ if( imgNum == imgArr.length ){ clearInterval(timer); //中止 audio.pause(); audio.currentTime = 0; }else{ ctx.clearRect(0,0,canvas2.width,canvas2.height); ctx.drawImage(source['src'+imgNum],0,0,view.w,view.h); imgNum++; }; },60); }; //按钮点击开始播放 document.querySelector('.playBtn2').onclick = function(){ pushImgArr(30); };