Vue implements video upload function

Vue implements video upload function

This article example shares the specific code of Vue to realize the video upload function for your reference. The specific content is as follows

Environment: vue + TS upload video + upload to Alibaba Cloud Mainly handles the front-end uploading video under vue

Using Alibaba Cloud's video on demand service

1. You need to apply for a development API in the background and request Alibaba Cloud's interface access control
2. With the token for developing videos, supply it to the front end
3. The front end requests Alibaba Cloud storage

video.vue

<template>
  <div class="container">
    <el-card>
      <div slot="header">
        <div>Course:</div>
        <div>Stage:</div>
        <div>Class time:</div>
      </div>
      <el-form label-width="40px">
        <el-form-item label="Video">
          <input
            ref="video-file"
            type="file"
          >
        </el-form-item>
        <el-form-item label="Cover">
          <input
            ref="image-file"
            type="file"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="authUpload"
          >Start uploading</el-button>
          <el-button>Return</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
/* eslint-disable */
import axios from 'axios'
import {
  aliyunImagUploadAddressAdnAuth,
  aliyunVideoUploadAddressAdnAuth,
  transCodeVideo,
  getAliyunTransCodePercent
} from '@/services/aliyun-oss'

export default {
  data () {
    return {
      uploader: null,
      videoId: null,
      imageUrl: '',
      fileName: ''
    }
  },
  created () {
    this.initUploader()
  },
  methods: {
    authUpload() {
      const videoFile = this.$refs['video-file'].files[0]
      this.uploader.addFile(videoFile, null, null, null, '{"Vod":{}}')
      this.uploader.addFile(this.$refs['image-file'].files[0], null, null, null, '{"Vod":{}}')
      this.fileName = videoFile.name
      this.uploader.startUpload()
    },
    initUploader () {
      this.uploader = new window.AliyunUpload.Vod({
        // Ali account ID, must have a value, the value comes from https://help.aliyun.com/knowledge_detail/37196.html
        userId: 1618139964448548,
        // The region for uploading to VOD, the default value is 'cn-shanghai', //eu-central-1, ap-southeast-1
        region: 'cn-shanghai',
        // The default fragment size is 1M and cannot be less than 100K
        partSize: 1048576,
        // The number of shards to upload in parallel, the default is 5
        parallel: 5,
        // When network failure occurs, re-upload times, default is 3
        retryCount: 3,
        // When the network fails, the re-upload interval is 2 seconds by default. retryDuration: 2,
        // Start uploading onUploadstarted: async uploadInfo => {
          console.log('onUploadstarted', uploadInfo)
          let uploadAuthInfo = null
          if (uploadInfo.isImage) {
            const { data } = await aliyunImagUploadAddressAdnAuth()
            this.imageUrl = data.data.imageURL
            uploadAuthInfo = data.data
          } else {
            const { data } = await aliyunVideoUploadAddressAdnAuth({
              fileName: uploadInfo.file.name
            })
            this.videoId = data.data.videoId
            uploadAuthInfo = data.data
          }
          
          // console.log('uploadAuthInfo', uploadAuthInfo)

          this.uploader.setUploadAuthAndAddress(
            uploadInfo,
            uploadAuthInfo.uploadAuth,
            uploadAuthInfo.uploadAddress,
            uploadAuthInfo.videoId || uploadAuthInfo.imageId
          )
        },
        // File upload successful onUploadSucceed: function (uploadInfo) {
          console.log('onUploadSucceed', uploadInfo)
        },
        // File upload failed onUploadFailed: function (uploadInfo, code, message) {
          console.log('onUploadFailed')
        },
        // File upload progress, unit: bytes onUploadProgress: function (uploadInfo, totalSize, loadedPercent) {
        },
        // Upload credential timeout onUploadTokenExpired: function (uploadInfo) {
          console.log('onUploadTokenExpired')
        },
        // All files uploaded are finished onUploadEnd: async uploadInfo => {
          console.log(uploadInfo)
          console.log({
            lessonId: this.$route.query.lessonId,
            fileId: this.videoId,
            coverImageUrl: this.imageUrl,
            fileName: this.fileName
          })
          const { data } = await transCodeVideo({
            lessonId: this.$route.query.lessonId,
            fileId: this.videoId,
            coverImageUrl: this.imageUrl,
            fileName: this.fileName
          })
          console.log(data)

          setInterval(async () => {
             const { data } = await getAliyunTransCodePercent(this.$route.query.lessonId)
             console.log('transcoding progress', data)
          }, 3000)
        }
      })
    }
  }
}
</script>

aliyun-oss.ts , storage interface

/**
 * Upload to Alibaba Cloud*/

import request from '@/utils/request'

export const aliyunImagUploadAddressAdnAuth = () => {
  return request({
    method: 'GET',
    url: '/boss/course/upload/aliyunImagUploadAddressAdnAuth.json'
  })
}

export const aliyunVideoUploadAddressAdnAuth = (params: any) => {
  return request({
    method: 'GET',
    url: '/boss/course/upload/aliyunVideoUploadAddressAdnAuth.json',
    params
  })
}

export const transCodeVideo = (data: any) => {
  return request({
    method: 'POST',
    url: '/boss/course/upload/aliyunTransCode.json',
    data
  })
}

export const getAliyunTransCodePercent = (lessonId: string | number) => {
  return request({
    method: 'GET',
    url: '/boss/course/upload/aliyunTransCodePercent.json',
    params: {
      lessonId
    }
  })
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Vue+flask realizes video synthesis function (drag and drop upload)
  • Vue implements mobile input to upload video and audio
  • Vue implements Tencent Cloud on-demand video upload function implementation code
  • Vue combines el-upload to realize Tencent Cloud video upload function
  • Solution for uploading videos or pictures or pictures and text to the backend in Vue
  • Vue vantUI implements file (picture, document, video, audio) upload (multiple files)

<<:  Analysis of Difficulties in Hot Standby of MySQL Database

>>:  Summary of 4 ways to add users to groups in Linux

Recommend

Pitfall notes of vuex and pinia in vue3

Table of contents introduce Installation and Usag...

Detailed explanation of the usage of MySQL memory tables and temporary tables

Usage of MySQL memory tables and temporary tables...

How to set a fixed IP in Linux (tested and effective)

First, open the virtual machine Open xshell5 to c...

Html tips to make your code semantic

Html semantics seems to be a commonplace issue. G...

Use of Linux ln command

1. Command Introduction The ln command is used to...

MySql knowledge points: transaction, index, lock principle and usage analysis

This article uses examples to explain the princip...

Example code for implementing anti-shake in Vue

Anti-shake: Prevent repeated clicks from triggeri...

Mysql sets boolean type operations

Mysql sets boolean type 1. Tinyint type We create...

How to implement a binary search tree using JavaScript

One of the most commonly used and discussed data ...

Windows DNS server exposed "worm-level" vulnerability, has existed for 17 years

Vulnerability Introduction The SigRed vulnerabili...

Detailed explanation of non-parent-child component value transfer in Vue3

Table of contents App.vue sub1.vue sub2.vue Summa...

How to configure MySQL8 in Nacos

1. Create the MySQL database nacos_config 2. Sele...

Vue monitoring properties and calculated properties

Table of contents 1. watch monitoring properties ...

HTML Tutorial: Unordered List

<br />Original text: http://andymao.com/andy...