前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >移动端自动播放视频

移动端自动播放视频

作者头像
程序员不务正业
发布2021-03-03 11:19:54
1.8K0
发布2021-03-03 11:19:54
举报

TS(Transport Stream,传输流)是一种封装的格式,它的全称为MPEG2-TS。是一种视频格式,一般用于实时流媒体和广播电视领域。

Mp4在IOS下可以自动播放,但是在部分安卓机下无法自动播放产生黑屏。 Ts可实现自动播放,IOS8以上和Android4.4以上都支持。 基于自动播放的优势

需要下ffmpeg来将Mp4转化成Ts视频。下面下载操作如下所示: mac下可以运行

代码语言:javascript
复制
// 安装 
 brew install ffmpeg
// 生成ts视频
ffmpeg -i loop_moon.mp4 -f mpegts \
    -codec:v mpeg1video -s 960x540 -b:v 1500k -r 30 -bf 0 \
    -codec:a mp2 -ar 44100 -ac 1 -b:a 128k \
    loop_moon.ts

还可以控制视频大小(-s),帧速率(-r),视频比特率(-b:v),音频比特率(-b:a),音频通道数(-ac),采样率(-ar ) 

推荐使用jsmpeg-player,它是基于jsmpeg封装的npm包

代码语言:javascript
复制
npm install @cycjimmy/jsmpeg-player --save
cnpm install @cycjimmy/jsmpeg-player --save

以下是贴代码

React
代码语言:javascript
复制
import React,{ useEffect, useState, useRef } from 'react';
import JSMpeg from '@cycjimmy/jsmpeg-player';

import './App.css';

function App() {
  const cover ='https://img11.360buyimg.com/imagetools/jfs/t1/105707/30/17596/32160/5e8c8ae6Ee2bfd8db/1ef4084de9ec103f.jpg'
  const tsSrc='https://storage.360buyimg.com/xingdianzhang/%E6%B5%8B%E8%AF%95ts.ts'

  const hdVideo = useRef(null);
  var [isCover, setIsCover] = useState(true);//是否用封面覆盖
  useEffect(() => {
    initTs()
  }, [])

  function initTs() {
    let canvas = hdVideo.current.querySelector('.hdVideo-ts');
    canvas.style.width = '0px'
    new JSMpeg.VideoElement(canvas, tsSrc, {
      canvas,
      autoplay: true,
      loop: true,
      progressive: false,//是否为chunk
      control: false,
      poster: cover,
      preserveDrawingBuffer: true,
      decodeFirstFrame: true,
      disableGl: true,
    }, {
      audio: false, //静音
      onPlay: () => {
        canvas.style.width = hdVideo.current.clientWidth + 'px'
        setIsCover(false)
      }
    })
  }

  return (
    <div ref={hdVideo} className='hdVideo'>
      {isCover ? <img className="hdVideo-image" src={cover} /> : null}
      <canvas className="hdVideo-ts"></canvas> 
    </div>
  );
}

export default App;
VUE
代码语言:javascript
复制
<template>
  <body>
    <div>
      <div class="homepage-hero-module">
        <div class="video-wrap">
          <div class="filter" :style="fixStyle ">
          </div>
          <canvas id="videoWrapper" class="fillWidth"></canvas>
                <div class="poster">
                    <span class="h1s">Welcome</span>
                </div>
        </div>
      </div>
    </div>
  </body>
</template>

<script>
  import JSMpeg from '@cycjimmy/jsmpeg-player';
  export default {
   
    data() {
      return {       
      }
    },

    mounted: function () {
      var tsSrc = 'https://storage.360buyimg.com/xingdianzhang/%E6%B5%8B%E8%AF%95ts.ts'
      var cover =
        'https://img11.360buyimg.com/imagetools/jfs/t1/105707/30/17596/32160/5e8c8ae6Ee2bfd8db/1ef4084de9ec103f.jpg'
      let canvas = document.getElementById('videoWrapper')
      new JSMpeg.VideoElement(canvas, tsSrc, {
        canvas,
        autoplay: true,
        loop: true,
        progressive: false, //是否为chunk
        control: false,
        poster: cover,
        preserveDrawingBuffer: true,
        decodeFirstFrame: true,
        disableGl: true,
      }, {
        audio: false, //静音
        onPlay: () => {
          console.log("---1111")
        }
      })
    },
  }
</script>

<style lang="less" scoped>
  .login {
    padding: 0px;
    text-align: right;
    min-width: 100%;
    position: relative;

    .line {
      padding: 10px;

      input {
        padding: 0px 10px;
        line-height: 28px;
        min-width: 20px;
      }
    }

    button {
      padding: 20px 20px;
      margin-top: 20px;
      line-height: 2px;
    }
  }

  .el-aside {
    line-height: 100%;
  }
</style>

<style scoped>
  .video {
    width: 100%;
    height: 100%;
    min-width: 100%;
    min-height: 100%;
    object-fit: cover;
  }

  .homepage-hero-module,
  .video-container {
    position: relative;
    height: 100vh;
    overflow: hidden;
    z-index: 0;
  }

  .video-wrap {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 3;
    opacity: 1;
    overflow: hidden;
  }

  .video-container .poster img,
  .video-container video {
    z-index: 0;
    position: absolute;
  }

  .filter {
    z-index: 1;
    position: absolute;
    background: rgba(110, 110, 110, 0);
  }

  .video-container .filter {
    z-index: 1;
    position: absolute;
    background: rgba(0, 0, 0, 0.4);
  }

  .h1s {
    font-size: 32px;
    margin-top: 50%;
    margin-bottom: 48px;
    font-weight: 300;
    color: #FFF;
    position: absolute;
    height: 100%;
    justify-content: center;
    z-index: 50;
    align-items: center;
    font-family: "Arial";
    margin-block-start: 25%;
    margin-block-end: 45%;
    margin-inline-start: 35%;
    margin-inline-end: 50%;
    font-size: 80px;


  }

  .video-js .vjs-icon-placeholder {
    width: 100%;
    height: 100%;
    display: block;
  }
</style>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 以下是贴代码
    • React
      • VUE
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档