本文介紹了Html5 video 上傳預(yù)覽圖片視頻,設(shè)置、預(yù)覽視頻某秒的海報(bào)幀,分享給大家,具體如下:
當(dāng)一收到上傳圖片視頻并可以動(dòng)態(tài)設(shè)置視頻顯示的海報(bào)幀的需求時(shí),主要想的是怎么樣解析視頻并獲取保存每幀的圖片,百度出來(lái)的大多是類似下面這種需要播放video并點(diǎn)擊截圖的,或者是用php ffmpeg擴(kuò)展,跟需求不一致,有點(diǎn)抓狂了,然后就先做了視頻圖片的預(yù)覽功能,進(jìn)而對(duì)設(shè)置海報(bào)幀換了種思路,通過(guò)輸入設(shè)置video開(kāi)始播放的時(shí)間,取消自動(dòng)播放和控制條,這樣用戶看到的就是一張圖片
/*預(yù)覽*/ $('.qtuploader__items').on('click', '[name="viewVideoPicBtn"]', function() { var parent = $(this).closest('.qtab__page'); var video = $(this).closest('.qtuploader__itemsbd').find('video'); var srcStr = '', htmlStr = ''; if($(this).siblings('.qtuploader__picinputbox').hasClass('is-error')){ $.fn.toast({ 'parentDom': parent, 'classes': 'isorange', 'top': '0', 'spacing': 0, 'toastContent': '請(qǐng)?jiān)O(shè)置正確范圍的海報(bào)幀', 'autoHide': 3000, 'position': { 'top': '5px', 'left': '50%' } }); return; } if (video.length > 0) { var thumbHeight = setSize(video)[0]; var thumbWidth = setSize(video)[1]; srcStr = video.attr('src'); htmlStr = '<div class="qtuploader__view"><div class="qtuploader__mask"></div><div class="qtuploader__thumb" style="width:' + thumbWidth + 'px;height:' + thumbHeight + 'px;margin:0 auto;"><video controls width="' + thumbWidth + '" height="' + thumbHeight + '" src="' + srcStr + '">您的瀏覽器不支持 video 標(biāo)簽</video></div></div>'; } parent.append(htmlStr); parent.find('.qtuploader__view video')[0].currentTime = $(this).siblings('.qtuploader__picinputbox').find('.qtuploader__picinput').val(); parent.find('.qtuploader__view').fadeIn(); }); /*設(shè)置海報(bào)幀預(yù)覽時(shí)間*/ $('.qtuploader__items').on('keyup', '.qtuploader__picinput', function() { var parent = $(this).closest('.qtuploader__picinputbox'); var video = $(this).closest('.qtuploader__itemsbd').find('video'); var strVal = $.trim($(this).val()); console.log(strVal) if (strVal == '') { parent.addClass('is-error'); parent.find('.qverify__font').text('請(qǐng)?jiān)O(shè)置海報(bào)幀'); } else if (!(/^[0-9]*$/.test(strVal))) { parent.addClass('is-error'); parent.find('.qverify__font').text('請(qǐng)輸入數(shù)字'); } else if (video.length > 0 && strVal > video[0].duration) { parent.addClass('is-error'); parent.find('.qverify__font').text('不超過(guò)(' + video[0].duration + ')'); console.log('111---' + video[0].duration) } else { parent.removeClass('is-error'); parent.find('.qverify__font').text('請(qǐng)?jiān)O(shè)置海報(bào)幀'); } }) /*關(guān)閉預(yù)覽*/ $(document).undelegate('.qtuploader__mask', 'click'); $(document).delegate('.qtuploader__mask', 'click', function() { $(this).closest('.qtuploader__view').fadeOut('normal', function() { $(this).closest('.qtuploader__view').remove(); }) }) /*設(shè)置預(yù)覽大小*/ function setSize(element) { var thumbWidth = 0, thumbHeight = 0, arr = []; var winWidth = $(window).width(), winHeight = $(window).height(); var imgWidth = element.width(), imgHeight = element.height(); if (imgWidth > imgHeight) { thumbHeight = parseInt(winHeight - 200); thumbWidth = parseInt((1920 * thumbHeight) / 1080); } else { thumbHeight = parseInt(winHeight - 200); thumbWidth = parseInt((1080 * thumbHeight) / 1920); } arr.push(thumbHeight, thumbWidth) return arr; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。
新聞熱點(diǎn)
疑難解答