uniapp对接oss视频上传+压缩

本文涉及的产品
对象存储 OSS,20GB 3个月
对象存储 OSS,恶意文件检测 1000次 1年
对象存储 OSS,内容安全 1000次 1年
简介: uniapp对接oss视频上传+压缩

与其在绝望和挣扎中苟活,不如在希冀和盼望中死亡。——纪伯伦

首先是文件上传的代码:

module.exports = {
  getUUID() { //生成UUID
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
      return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
    })
  },
  beforeUpload({
    file,
    module
  }) {
    let _self = this;
    return new Promise((resolve, reject) => {
      this.policy(module)
        .then(response => {
          resolve({
            policy: response.data.policy,
            signature: response.data.signature,
            ossaccessKeyId: response.data.accessid,
            key: response.data.dir + this.getUUID() + "${filename}",
            dir: response.data.dir,
            host: response.data.host
          });
        })
        .catch(err => {
          reject(err);
        });
    });
  },
  policy(module) {
    return new Promise((resolve, reject) => {
      uni.request({
                // 在这改获取oss签证的接口
        url: 'http://获取签证的接口/' + 'oss',
        data: {
          module
        },
        success: (res) => {
          if (res.data.success) {
            resolve(res.data)
          } else {
            reject(res)
          }
        },
        fail: (err) => {
          reject(err)
        }
      });
    })
  },
  uploadFile({
    file,
    module,
    response,
    limit = 5242880
  }) {
    console.log("uploadFile: ", {
      file,
      module,
      response
    })
    // 限制5M,单位B
    if (!file) {
      uni.showToast({
        title: "读取文件失败",
        icon: 'none'
      });
      return false;
    }
    if (file.size > limit) {
      uni.showToast({
        title: "超过限制大小",
        icon: 'none'
      });
      return false;
    }
    let filename = file.path.substr(file.path.lastIndexOf("/") + 1)
    this.beforeUpload({
      file,
      module
    }).then(res => {
      let options = {
        url: res.host,
        name: 'file',
        filePath: file.path,
        formData: res,
        success: ossCallBack => {
          console.log({
            ossCallBack,
            filename
          })
          response(res.host + "/" + res.key.replace("${filename}", filename))
        },
        fail: console.error
      }
      console.log(options)
      uni.uploadFile(options)
    }).catch(console.error)
  }
}


然后是视频上传+压缩

<script>
const oss = require('@/utils/oss.js');
export default {
    methods: {
    upload(file, { contentType }) {
      console.log({ file });
      console.log({ contentType });
      let messageType = '';
      let content = file.path;
      let limit;
      switch (contentType) {
        case 1:
          messageType = 'IMAGE';
          limit = 2097152;
          break;
        case 4:
          messageType = 'VIDEO';
          limit = 10485760;
          break;
      }
      oss.uploadFile({
        file,
        module: 'chat',
        limit,
        response: filePath => {
          console.log('File upload success!');
          console.log({ filePath });
                    // 上传完成结束回调
        }
      });
    },
    onImage() {
      uni.chooseImage({
        sourceType: ['album'],
        success: res => {
          res.tempFiles.forEach(file => {
            this.upload(file, {
              contentType: 1
            });
          });
        }
      });
    },
    onVideo() {
      uni.chooseVideo({
        count: 1,
        compressed: true,
        sourceType: ['album', 'camera'],
        success: res => {
          console.log({ res });
                    // 这块是自定义压缩,需要在上方compressed设置为false关闭默认上传压缩
          // console.log('压缩前大小:: ', res.size / (1024 * 1024) + 'KB');
          // uni.getVideoInfo({
          //  src: res.tempFilePath,
          //  success: ({ fps, bitrate }) => {
          //    console.log({ fps, bitrate });
          //    uni.compressVideo({
          //      src: res.tempFilePath,
          //      quality: 'high',
          //      bitrate,
          //      fps,
          //      resolution: 1,
          //      success: res => {
          //        console.log({ res });
          //        console.log('压缩后大小:: ', res.size / (1024 * 1024) + 'KB');
          //        this.upload({ path: res.tempFilePath, size: res.size }, { contentType: 4 });
          //      }
          //    });
          //  }
          // });
          console.log('视频大小:: ', res.size / (1024 * 1024) + 'KB');
          this.upload({ path: res.tempFilePath, size: res.size }, { contentType: 4 });
        }
      });
    },
    onShoot() {
      uni.chooseImage({
        sourceType: ['camera'],
        success: res => {
          this.upload(res.tempFiles[0], {
            contentType: 1
          });
        }
      });
    }
  }
};
</script>
相关实践学习
借助OSS搭建在线教育视频课程分享网站
本教程介绍如何基于云服务器ECS和对象存储OSS,搭建一个在线教育视频课程分享网站。
相关文章
|
4月前
|
存储 SQL 分布式计算
数据计算MaxCompute读取外部表(数据在oss gz压缩)速度非常慢,有什么方法可以提升效率么?
数据计算MaxCompute读取外部表(数据在oss gz压缩)速度非常慢,有什么方法可以提升效率么?
49 1
|
11天前
|
存储 小程序 API
对象存储OSS产品常见问题之前端直传视频获取视频的长度获得多少秒如何解决
对象存储OSS是基于互联网的数据存储服务模式,让用户可以安全、可靠地存储大量非结构化数据,如图片、音频、视频、文档等任意类型文件,并通过简单的基于HTTP/HTTPS协议的RESTful API接口进行访问和管理。本帖梳理了用户在实际使用中可能遇到的各种常见问题,涵盖了基础操作、性能优化、安全设置、费用管理、数据备份与恢复、跨区域同步、API接口调用等多个方面。
31 0
|
1月前
|
机器学习/深度学习 编解码 API
视觉智能平台常见问题之如何将融合后的视频上传至oss
视觉智能平台是利用机器学习和图像处理技术,提供图像识别、视频分析等智能视觉服务的平台;本合集针对该平台在使用中遇到的常见问题进行了收集和解答,以帮助开发者和企业用户在整合和部署视觉智能解决方案时,能够更快地定位问题并找到有效的解决策略。
32 3
|
3月前
|
移动开发 小程序 前端开发
Uniapp Vue3 基础到实战 教学视频
Uniapp Vue3 基础到实战 教学视频
124 0
|
3月前
|
JavaScript
uniapp计算视频学习进程,并且下次回来继续播放(不能快进)
uniapp计算视频学习进程,并且下次回来继续播放(不能快进)
80 0
|
3月前
uniapp如何让视频无法快进
uniapp如何让视频无法快进
45 0
|
3月前
|
JavaScript
uniapp实现无法快进的视频
uniapp实现无法快进的视频
35 0
|
4月前
HBuilderX使用uniapp中的video标签开发视频应用APP,出现视频覆盖<view>图层无法遮住等问题如何解决?
HBuilderX使用uniapp中的video标签开发视频应用APP,出现视频覆盖<view>图层无法遮住等问题如何解决?
|
4月前
|
XML 存储 JSON
C# 对象存储 (轻松实现序列化 | Xml | Json | 加密 | 压缩 | 注册表 | Redis)
开发时经常会遇到需要保存配置的情况,最常见的实现方式是将对象序列化成Json,再写入文件并保存到本地磁盘。 本文将使用开源库**ApeFree.DataStore**来替换原有的对象存储过程,实现一个可以随意切换存储方式的对象存储方法。 ApeFree.DataStore是一款可配置的对象存储库,支持在不同平台/介质中对内存中的对象进行存储与还原(如本地存储、注册表存储)。支持配置序列化格式(如Json、Xml),支持配置压缩算法(如GZip、Defalte),支持配置加密算法(如AES、RSA)。
68 0
C# 对象存储 (轻松实现序列化 | Xml | Json | 加密 | 压缩 | 注册表 | Redis)
|
4月前
|
vr&ar 对象存储
oss视频截封面
oss视频截封面
243 0