App下載

在HTML5中怎么通過video上傳預覽圖片和視頻?實現方法分享!

猿友 2021-07-15 14:29:29 瀏覽數 (4342)
反饋

在我們日常網上購物中都可以看到很多漂亮的海報和各種產品效果圖,那么大家知道怎么通過html5實現嗎?那么今天我們要說的就是:“在HTML5中怎么通過video上傳預覽圖片視頻效果?”這個問題,下面是小編整理的相關內容,希望對大家的學習有所幫助!

我們來看這個預覽圖,如下所示:

當一收到上傳圖片視頻并可以動態(tài)設置視頻顯示的海報幀的需求時,主要想的是怎么樣解析視頻并獲取保存每幀的圖片,百度出來的大多是類似下面這種需要播放video并點擊截圖的,或者是用php ffmpeg擴展,跟需求不一致,有點抓狂了,然后就先做了視頻圖片的預覽功能,進而對設置海報幀換了種思路,通過輸入設置video開始播放的時間,取消自動播放和控制條,這樣用戶看到的就是一張圖片

/*預覽*/
              $('.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': '請設置正確范圍的海報幀',
        '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 標簽</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();
  });
  /*設置海報幀預覽時間*/
  $('.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('請設置海報幀');
    } else if (!(/^[0-9]*$/.test(strVal))) {
      parent.addClass('is-error');
      parent.find('.qverify__font').text('請輸入數字');
    } else if (video.length > 0 && strVal > video[0].duration) {
      parent.addClass('is-error');
      parent.find('.qverify__font').text('不超過(' + video[0].duration + ')');
      console.log('111---' + video[0].duration)
    } else {
      parent.removeClass('is-error');
      parent.find('.qverify__font').text('請設置海報幀');
    }
  })
  /*關閉預覽*/
  $(document).undelegate('.qtuploader__mask', 'click');
  $(document).delegate('.qtuploader__mask', 'click', function() {
    $(this).closest('.qtuploader__view').fadeOut('normal', function() {
      $(this).closest('.qtuploader__view').remove();
    })
  })
  /*設置預覽大小*/
  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;
  }

總結

那么通過文章內容的講解相信大家對網上產品視頻的預覽圖和效果圖也有所了解了吧!那么對于:“在HTML5中怎么通過video上傳預覽圖片視頻效果?”這個問題的分享我們就到這了,更多有關于html5這方面的知識和內容我們都可以在W3Cschool中進行學習和了解。 


0 人點贊