uniapp 上传视频显示视频第一帧(视频缩略图)作为封面 可以自定义封面内容

 效果图

 

html

<template>
  <view>
    <view class="uploadImage">
      <view v-for="(item, index) in viList" class="aa" :key="index">
        <view class="del" @click="delHandler(index)">删除</view>
        <view v-if="item.type === 'v'" class="typeBox">视频格式</view>
        <image class="sty" :src="item.image" @click="previewImageHandler(item.type)" mode="scaleToFill" />
      </view>
    </view>
    <view @click="chooseVideoHandler('video')" class="flexC btnBox">上传视频</view>
    <view @click="chooseVideoHandler('image')" class="flexC btnBox">上传图片</view>
  </view>
</template>

 

js

<script>
export default {
  data() {
    return {
      viList: [],
      vue_uploadAction: '上传接口',
    }
  },
  methods: {
    async chooseVideoHandler(state) {
      uni.showLoading({
        mask: true,
        title: '上传中...',
      })
      let uploadFile = ''
      // 1.选择要上传的视频
      const res = await uni.chooseMedia({
        maxDuration: 60,
        sourceType: ['album'],
        mediaType: Array(state),
      })
      // 判断函数传来的类型是不是视频 如果是就使用视频缩略图 => thumbTempFilePath
      uploadFile = state == 'video' ? res[1].tempFiles[0].thumbTempFilePath : res[1].tempFiles[0].tempFilePath
      var that = this
      const arr = await uni.uploadFile({
        // 需要上传的地址
        url: that.vue_uploadAction,
        // filePath  需要上传的文件
        filePath: uploadFile,
        name: 'file',
        // header: {
        // 	token: that.vuex_token		// 挂载请求头为用户的 token
        // },
      })
      let data = JSON.parse(arr[1].data)
      console.log('data:', data)
      if (data.code !== 1) {
        // 视频上传失败了
        uni.hideLoading()
        uni.showToast({
          title: data.msg,
          icon: 'none',
        })
        return
      }
      // 上传成功(把上传成功后的文件路径 push 到页面需要显示的视频数据列表中)
      this.viList.push({
        image: data.data.fullurl,
        type: state == 'video' ? 'v' : 'i',
      })
      uni.hideLoading()
    },
    previewImageHandler(type) {
      type === 'i' &&
        uni.previewImage({
          urls: this.viList.filter(t => t.type === 'i').map(t => t.image),
        })
    },
    delHandler(index) {
      this.viList.splice(index, 1)
    },
  },
}
</script>

 css

<style lang="less">
.uploadImage {
  display: flex;
  flex-wrap: wrap;
}
.aa {
  position: relative;
  > .del {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 999;
    color: #fff;
    font-size: 26rpx;
  }
  > .sty {
    width: 250rpx;
    height: 250rpx;
  }
  > .jinzhi {
    width: 250rpx;
    height: 250rpx;
    background: transparent;
    position: absolute;
    z-index: 998;
    top: 0;
    left: 0;
  }
}
.typeBox {
  color: aqua;
  position: absolute;
  top: 0;
  right: 0;
  font-size: 26rpx;
}
.btnBox {
  margin: 0 auto;
  width: 240rpx;
  height: 80rpx;
  margin-top: 20rpx;
  background: #dcbbaa;
  color: #000;
}
</style>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 vant 的 Upload 组件实现文件上传,同时使用 video.js 库获取视频第一作为缩略图。具体实现可以参考以下代码: <template> <van-uploader :after-read="afterRead" :before-read="beforeRead" :preview-options="{showIndicators: false}" :max-count="1" accept="video/*" capture="camera" multiple :preview-image="false" :preview-full-image="false" :preview-cover="false" > <van-icon name="photograph" /> </van-uploader> </template> <script> import videojs from 'video.js' import 'video.js/dist/video-js.css' export default { methods: { async beforeRead(file) { // 判断是否为视频文件 if (file.type.indexOf('video') !== 0) { this.$toast('请选择视频文件') return false } }, async afterRead(file) { // 创建 video 元素 const video = document.createElement('video') video.src = URL.createObjectURL(file) video.preload = 'metadata' video.onloadedmetadata = async () => { // 获取视频第一作为缩略图 const canvas = document.createElement('canvas') canvas.width = video.videoWidth canvas.height = video.videoHeight canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height) const thumbnail = canvas.toDataURL('image/jpeg') // 上传视频缩略图 const formData = new FormData() formData.append('video', file) formData.append('thumbnail', thumbnail) // 发送上传请求 const response = await this.$axios.post('/api/upload', formData) this.$toast('上传成功') } // 初始化 video.js videojs(video) }, }, } </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值