zoukankan      html  css  js  c++  java
  • 04: 使用七牛云进行视频点播

    1.1 七牛云存储介绍

      1、七牛云存储使用参考文档

    # 七牛云管理后台地址
    https://portal.qiniu.com/kodo/bucket
    # 七牛云使用
    https://developer.qiniu.com/kodo/manual/1233/console-quickstart
    
    # pythonSDK
    https://developer.qiniu.com/kodo/sdk/1242/python
    # Node.js SDK V6
    https://developer.qiniu.com/kodo/sdk/3828/node-js-v6
    # JavaScript SDK历史文档1.x
    https://developer.qiniu.com/kodo/sdk/4244/the-javascript-sdk-historical-documents-1-x

      2、七牛云介绍

          1. 以前看见过FastDfs+FFmpeg进行视频存储等操作,但是这种方式稳定性会低一些,而且成本也没有降低。

          2. 市面上关于云存储的第三方服务比比皆是,最著名的无疑就是七牛云存储,本次我们将演示用django+Vue+七牛云进行视频存储与播放。      

      3、七牛云上传逻辑

          1. 在做七牛云的文件上传时,很多人有一个误区,就是以为是前端先上传到后台服务器,然后后台服务器再将文件上传到七牛云。

          2. 这个逻辑本身没有问题,但是会遇到一个问题,如果文件大会导致上传很慢

          3. 正确逻辑应该是前端直接上传七牛,而后台只承担生成token和存储七牛云返回的hash的任务。

    1.2 django+JavaScript上传视频

      1、参考七牛云SDK

    # pythonSDK
    https://developer.qiniu.com/kodo/sdk/1242/python
    # JavaScript SDK历史文档1.x https://developer.qiniu.com/kodo/sdk/4244/the-javascript-sdk-historical-documents-1-x
    # JavaScript 官方demo
    https://github.com/qiniu/js-sdk/tree/1.x#demo

      2、代码

        参考代码:https://gitee.com/edushiyanlou/QiniuUploader

    # 配置模板的路径
    TEMPLATES = [
        {
           'DIRS': [os.path.join(BASE_DIR,'templates')],
        },
    ]
      2、 配置静态目录
    
    # 像ccs和js这些静态文件如果想要使用必须在这里配置路径
    STATICFILES_DIRS = (
        os.path.join(BASE_DIR,'static'),
    )
    settings.py
    from django.contrib import admin
    from django.urls import path
    from app01 import views
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('uptoken/', views.QNYTokenView.as_view()),
        path('upload/', views.UploadView.as_view()),
        path('vod/', views.VODView.as_view()),
    ]
    urls.py 定义总路由
    from django.shortcuts import render,HttpResponse
    from django.views import View
    import json
    
    '''获取上传token'''
    class QNYTokenView(View):
        def get(self,request):
            from qiniu import Auth, put_file, etag
            import qiniu.config
            # 需要填写你的 Access Key 和 Secret Key
            access_key = "PwyTqrclbus4ntRct1o8G2V-qkR1rI7hbd_5Gx29"
            secret_key = "IuvSm1vJh2YUiYWFwV-kGmHAJF9R9iGuH2Q1ifea"
            # 构建鉴权对象
            q = Auth(access_key, secret_key)
            # 要上传的空间
            bucket_name = 'imagesstatic'
            # 生成上传 Token,可以指定过期时间等
            token = q.upload_token(bucket_name, expires=3600)
            return HttpResponse(json.dumps({'uptoken': token}, ensure_ascii=False))
    
    '''上传页面'''
    class UploadView(View):
        def get(self,request):
            return render(request,'upload.html')
    
    '''播放页面'''
    class VODView(View):
        def get(self,request):
            return render(request,'vod.html')
    app01/views.py
    <!doctype html>
    <html lang="zh">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>js上传文件到七牛</title>
        <style>
            #container{
                width:200px;
                height:200px;
                border:1px solid #9d9d9d;
                border-radius: 6px;
                margin:50px auto;
                position: relative;
                overflow: hidden;
            }
            .upload-progress{
                width:100%;
                height:100%;
                position: absolute;
                top:0;
                left:0;
                background: rgba(0,0,0,0.5);
                z-index: 5;
                color:#fff;
                line-height: 200px;
                text-align: center;
                display: none;
            }
            #uploadImage{
                width:100%;
                height:100%;
                position: absolute;
                top:0;
                left:0;
                z-index: 2;
                text-align: center;
                line-height: 200px;
                cursor: pointer;
            }
            #container img{
                width:100%;
                position: absolute;
                top:0;
                left:0;
                z-index: 1;
            }
        </style>
    </head>
    <body>
    <div id="container">
        <div id="uploadImage">选择文件</div>
        <div class="upload-progress"></div>
    </div>
    <script src="/static/qiniu.min.js"></script>
    <script src="/static/jquery-1.12.1.min.js"></script>
    <script>
        var uploader = Qiniu.uploader({
            disable_statistics_report: false,                                   // 禁止自动发送上传统计信息到七牛,默认允许发送
            runtimes: 'html5,flash,html4',                                      // 上传模式,依次退化
            browse_button: 'uploadImage',                                       // 上传选择的点选按钮,必需
            container: 'container',                                             // 上传区域DOM ID,默认是browser_button的父元素
            max_file_size: '500mb',                                             // 最大文件体积限制
            flash_swf_url: 'Moxie.swf',                                         // 引入flash,相对路径
            dragdrop: false,                                                    // 关闭可拖曳上传
            chunk_size: '4mb',                                                  // 分块上传时,每块的体积
            multi_selection: !(moxie.core.utils.Env.OS.toLowerCase() === "ios"),
            uptoken_url: 'http://127.0.0.1:8000/uptoken',                                                 // 在初始化时,uptoken,uptoken_url,uptoken_func三个参数中必须有一个被设置,uptoken是上传凭证,由其他程序生成;uptoken_url是提供了获取上传凭证的地址,如果需要定制获取uptoken的过程则可以设置uptoken_func;其优先级为uptoken > uptoken_url > uptoken_func
            //uptoken:'q06bq54Ps5JLfZyP8Ax-qvByMBdu8AoIVJpMco2m:kyTiuN6GBUpfNt1nJIA7C8CCStY=:eyJzY29wZSI6IjEzMTIzMTIzMTIzIiwiZGVhZGxpbmUiOjE1NzY0MTM3MTB9',
            domain: 'redinnovation.s3-cn-north-1.qiniucs.com',               // bucket域名,下载资源时用到,必需
            get_new_uptoken: false,                                              // 设置上传文件的时候是否每次都重新获取新的uptoken
            auto_start: true,                                                   // 选择文件后自动上传,若关闭需要自己绑定事件触发上传
            max_retries: 3,                                                     // 上传失败最大重试次数
            save_key: true,
            resize: {                                                           // 想限制上传图片尺寸,直接用resize这个属性
                 300,
                height: 300
            },
            init: {
                'FilesAdded': function(up, files) {                             // 文件添加进队列后,处理相关的事情
                    plupload.each(files, function(file) {
                        console.log(file)
                    });
                },
                'BeforeUpload': function(up, file) {                            // 每个文件上传前,处理相关的事情
                    console.log("开始上传之前");
                    $(".upload-progress").show();
                },
                'UploadProgress': function(up, file) {                          // 每个文件上传时,处理相关的事情
                    console.log("上传中");
                    $(".upload-progress").html("上传进度:"+file.percent + "%");
                },
                'FileUploaded': function(up, file, info) {                       // 每个文件上传成功后,处理相关的事情
                    console.log("上传成功");
                    console.log(info);
                    //$(".upload-progress").hide();
                    //var img = new Image();                                      //创建一个Image对象,实现图片的预下载
                    //img.src = "http://qiniu.com/"+res.key;
                    //$("#container").append(img);
                },
                'Error': function(up, err, errTip) {
                    console.log("上传出错")
                },
                'UploadComplete': function() {
                    //队列文件处理完毕后,处理相关的事情
                }
            }
        });
    </script>
    </body>
    </html>
    templates/upload.html 上传视频
    <!doctype html>
    <html lang="zh">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>xxx</title>
        <style></style>
    </head>
    <body>
        <div>
            <video id="video"  width="320" height="240" src="http://q5gq3qy79.bkt.clouddn.com/lucclaPl4-Hi4LcepzKNVWpfN4u7" controls="controls" autoplay="autoplay" muted loop="loop" >
                        您的浏览器不支持 video 标签。
             </video>
        </div>
    </body>
    </html>
    templates/vod.html 播放视频

         

     1.3 vue+django上传视频

         参考代码:https://gitee.com/edushiyanlou/QiniuUploader

      1、前端Vue代码

    vue init webpack qiniuVue              # 初始化一个vue前端项目
    npm install --save axios               # 安装axios
    npm install  --save  jquery@1.12.1     # 安装jquery
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width,initial-scale=1.0">
        <title>qiniu_vue</title>
        <script src="./static/qiniu.min.js"></script>
      </head>
      <body>
        <div id="app"></div>
        <!-- built files will be auto injected -->
      </body>
    </html>
    index.html 引入qiniu.min.js
    import Vue from 'vue'
    import Router from 'vue-router'
    import HelloWorld from '@/components/HelloWorld'
    import Upload from '@/components/Upload'
    import Vod from '@/components/Vod'
    
    Vue.use(Router)
    
    export default new Router({
      routes: [
        { path: '/', name: 'HelloWorld', component: HelloWorld },
        { path: '/upload', name: 'Upload', component: Upload },
        { path: '/vod', name: 'Vod', component: Vod },
      ]
    })
    src/router/index.js
    <template>
      <div id="container">
        <div>
          <div id="uploadImage">选择文件</div>
          <div class="upload-progress"></div>
        </div>
      </div>
    </template>
    
    <script>
      import $ from 'jquery'
      import axios from 'axios'
      export default {
        data() {
          return {
            uptoken: '',
          };
        },
        methods: {
          getQiniuToken: function (callback){
            axios({
              url: 'http://127.0.0.1:8000/uptoken',
              method: 'get',
            }).then( ret =>{
              this.uptoken = ret.data.uptoken;
              callback()  // callback 出入的是回调函数 initQiniu() 用来初始化Qiniu.uploader()
            }).catch( err=>{
              console.log(err,'err')
            })
          },
    
          initQiniu: function () {
            var uploader = Qiniu.uploader({
              disable_statistics_report: false,                                   // 禁止自动发送上传统计信息到七牛,默认允许发送
              runtimes: 'html5,flash,html4',                                      // 上传模式,依次退化
              browse_button: 'uploadImage',                                       // 上传选择的点选按钮,必需
              container: 'container',                                             // 上传区域DOM ID,默认是browser_button的父元素
              max_file_size: '500mb',                                             // 最大文件体积限制
              flash_swf_url: 'Moxie.swf',                                         // 引入flash,相对路径
              dragdrop: false,                                                    // 关闭可拖曳上传
              chunk_size: '4mb',                                                  // 分块上传时,每块的体积
              multi_selection: !(moxie.core.utils.Env.OS.toLowerCase() === "ios"),
              uptoken: this.uptoken,     // 在初始化时,uptoken,uptoken_url,uptoken_func三个参数中必须有一个被设置,uptoken是上传凭证,由其他程序生成;uptoken_url是提供了获取上传凭证的地址,如果需要定制获取uptoken的过程则可以设置uptoken_func;其优先级为uptoken > uptoken_url > uptoken_func
              // uptoken_url: 'http://127.0.0.1:8000/uptoken',                                                 // 在初始化时,uptoken,uptoken_url,uptoken_func三个参数中必须有一个被设置,uptoken是上传凭证,由其他程序生成;uptoken_url是提供了获取上传凭证的地址,如果需要定制获取uptoken的过程则可以设置uptoken_func;其优先级为uptoken > uptoken_url > uptoken_func
              // uptoken:'q06bq54Ps5JLfZyP8Ax-qvByMBdu8AoIVJpMco2m:kyTiuN6GBUpfNt1nJIA7C8CCStY=:eyJzY29wZSI6IjEzMTIzMTIzMTIzIiwiZGVhZGxpbmUiOjE1NzY0MTM3MTB9',
              domain: 'redinnovation.s3-cn-north-1.qiniucs.com',               // bucket域名,下载资源时用到,必需
              get_new_uptoken: false,                                              // 设置上传文件的时候是否每次都重新获取新的uptoken
              auto_start: true,                                                   // 选择文件后自动上传,若关闭需要自己绑定事件触发上传
              max_retries: 3,                                                     // 上传失败最大重试次数
              save_key: true,
              resize: {                                                           // 想限制上传图片尺寸,直接用resize这个属性
                 300,
                height: 300
              },
              init: {
                'FilesAdded': function(up, files) {                             // 文件添加进队列后,处理相关的事情
                  plupload.each(files, function(file) {
                    console.log(file)
                  });
                },
                'BeforeUpload': function(up, file) {                            // 每个文件上传前,处理相关的事情
                  console.log("开始上传之前");
                  $(".upload-progress").show();
                },
                'UploadProgress': function(up, file) {                          // 每个文件上传时,处理相关的事情
                  console.log("上传中");
                  $(".upload-progress").html("上传进度:"+file.percent + "%");
                },
                'FileUploaded': function(up, file, info) {                       // 每个文件上传成功后,处理相关的事情
                  console.log("上传成功");
                  console.log(info);
                  //$(".upload-progress").hide();
                  //var img = new Image();                                      //创建一个Image对象,实现图片的预下载
                  //img.src = "http://qiniu.com/"+res.key;
                  //$("#container").append(img);
                },
                'Error': function(up, err, errTip) {
                  console.log("上传出错")
                },
                'UploadComplete': function() {
                  //队列文件处理完毕后,处理相关的事情
                }
              }
            });
          }
        },
        mounted(){
          this.getQiniuToken(() => {
            this.initQiniu()  // 将initQiniu()当做callback回调函数传入给getQiniuToken函数
          })
        }
      }
    </script>
    
    <style>
      #container{
        200px;
        height:200px;
        border:1px solid #9d9d9d;
        border-radius: 6px;
        margin:50px auto;
        position: relative;
        overflow: hidden;
      }
      .upload-progress{
        100%;
        height:100%;
        position: absolute;
        top:0;
        left:0;
        background: rgba(0,0,0,0.5);
        z-index: 5;
        color:#fff;
        line-height: 200px;
        text-align: center;
        display: none;
      }
      #uploadImage{
        100%;
        height:100%;
        position: absolute;
        top:0;
        left:0;
        z-index: 2;
        text-align: center;
        line-height: 200px;
        cursor: pointer;
      }
      #container img{
        100%;
        position: absolute;
        top:0;
        left:0;
        z-index: 1;
      }
    </style>
    src/components/upload.vue
    <template>
      <div>
        <div>
          <video id="video"  width="320" height="240" src="http://q5gq3qy79.bkt.clouddn.com/lucclaPl4-Hi4LcepzKNVWpfN4u7" controls="controls" autoplay="autoplay" muted loop="loop" >
            您的浏览器不支持 video 标签。
          </video>
        </div>
      </div>
    </template>
    
    <script>
    
    </script>
    
    <style>
    
    </style>
    src/components/vod.vue

      2、django代码

    INSTALLED_APPS = [
    
        'corsheaders',
    ]
    
    
    '''配置模板的路径'''
    TEMPLATES = [
        {
           'DIRS': [os.path.join(BASE_DIR,'templates')],
        },
    ]
      2、 配置静态目录
    
    
    '''像ccs和js这些静态文件如果想要使用必须在这里配置路径'''
    STATICFILES_DIRS = (
        os.path.join(BASE_DIR,'static'),
    )
    
    #
    
    '''配置cors'''
    CORS_ALLOW_CREDENTIALS = True
    CORS_ORIGIN_ALLOW_ALL = True
    settings.py
    from django.contrib import admin
    from django.urls import path
    from app01 import views
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('uptoken/', views.QNYTokenView.as_view()),
        path('upload/', views.UploadView.as_view()),
        path('vod/', views.VODView.as_view()),
    ]
    urls.py 定义总路由
    from django.shortcuts import render,HttpResponse
    from django.views import View
    import json
    
    '''获取上传token'''
    class QNYTokenView(View):
        def get(self,request):
            from qiniu import Auth, put_file, etag
            import qiniu.config
            # 需要填写你的 Access Key 和 Secret Key
            access_key = "PwyTqrclbus4ntRct1o8G2V-qkR1rI7hbd_5Gx29"
            secret_key = "IuvSm1vJh2YUiYWFwV-kGmHAJF9R9iGuH2Q1ifea"
            # 构建鉴权对象
            q = Auth(access_key, secret_key)
            # 要上传的空间
            bucket_name = 'imagesstatic'
            # 生成上传 Token,可以指定过期时间等
            token = q.upload_token(bucket_name, expires=3600)
            return HttpResponse(json.dumps({'uptoken': token}, ensure_ascii=False))
    
    '''上传页面'''
    class UploadView(View):
        def get(self,request):
            return render(request,'upload.html')
    
    '''播放页面'''
    class VODView(View):
        def get(self,request):
            return render(request,'vod.html')
    app01/views.py
    <!doctype html>
    <html lang="zh">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>js上传文件到七牛</title>
        <style>
            #container{
                200px;
                height:200px;
                border:1px solid #9d9d9d;
                border-radius: 6px;
                margin:50px auto;
                position: relative;
                overflow: hidden;
            }
            .upload-progress{
                100%;
                height:100%;
                position: absolute;
                top:0;
                left:0;
                background: rgba(0,0,0,0.5);
                z-index: 5;
                color:#fff;
                line-height: 200px;
                text-align: center;
                display: none;
            }
            #uploadImage{
                100%;
                height:100%;
                position: absolute;
                top:0;
                left:0;
                z-index: 2;
                text-align: center;
                line-height: 200px;
                cursor: pointer;
            }
            #container img{
                100%;
                position: absolute;
                top:0;
                left:0;
                z-index: 1;
            }
        </style>
    </head>
    <body>
    <div id="container">
        <div id="uploadImage">选择文件</div>
        <div class="upload-progress"></div>
    </div>
    <script src="/static/qiniu.min.js"></script>
    <script src="/static/jquery-1.12.1.min.js"></script>
    <script>
        var uploader = Qiniu.uploader({
            disable_statistics_report: false,                                   // 禁止自动发送上传统计信息到七牛,默认允许发送
            runtimes: 'html5,flash,html4',                                      // 上传模式,依次退化
            browse_button: 'uploadImage',                                       // 上传选择的点选按钮,必需
            container: 'container',                                             // 上传区域DOM ID,默认是browser_button的父元素
            max_file_size: '500mb',                                             // 最大文件体积限制
            flash_swf_url: 'Moxie.swf',                                         // 引入flash,相对路径
            dragdrop: false,                                                    // 关闭可拖曳上传
            chunk_size: '4mb',                                                  // 分块上传时,每块的体积
            multi_selection: !(moxie.core.utils.Env.OS.toLowerCase() === "ios"),
            uptoken_url: 'http://127.0.0.1:8000/uptoken',                                                 // 在初始化时,uptoken,uptoken_url,uptoken_func三个参数中必须有一个被设置,uptoken是上传凭证,由其他程序生成;uptoken_url是提供了获取上传凭证的地址,如果需要定制获取uptoken的过程则可以设置uptoken_func;其优先级为uptoken > uptoken_url > uptoken_func
            //uptoken:'q06bq54Ps5JLfZyP8Ax-qvByMBdu8AoIVJpMco2m:kyTiuN6GBUpfNt1nJIA7C8CCStY=:eyJzY29wZSI6IjEzMTIzMTIzMTIzIiwiZGVhZGxpbmUiOjE1NzY0MTM3MTB9',
            domain: 'redinnovation.s3-cn-north-1.qiniucs.com',               // bucket域名,下载资源时用到,必需
            get_new_uptoken: false,                                              // 设置上传文件的时候是否每次都重新获取新的uptoken
            auto_start: true,                                                   // 选择文件后自动上传,若关闭需要自己绑定事件触发上传
            max_retries: 3,                                                     // 上传失败最大重试次数
            save_key: true,
            resize: {                                                           // 想限制上传图片尺寸,直接用resize这个属性
                 300,
                height: 300
            },
            init: {
                'FilesAdded': function(up, files) {                             // 文件添加进队列后,处理相关的事情
                    plupload.each(files, function(file) {
                        console.log(file)
                    });
                },
                'BeforeUpload': function(up, file) {                            // 每个文件上传前,处理相关的事情
                    console.log("开始上传之前");
                    $(".upload-progress").show();
                },
                'UploadProgress': function(up, file) {                          // 每个文件上传时,处理相关的事情
                    console.log("上传中");
                    $(".upload-progress").html("上传进度:"+file.percent + "%");
                },
                'FileUploaded': function(up, file, info) {                       // 每个文件上传成功后,处理相关的事情
                    console.log("上传成功");
                    console.log(info);
                    //$(".upload-progress").hide();
                    //var img = new Image();                                      //创建一个Image对象,实现图片的预下载
                    //img.src = "http://qiniu.com/"+res.key;
                    //$("#container").append(img);
                },
                'Error': function(up, err, errTip) {
                    console.log("上传出错")
                },
                'UploadComplete': function() {
                    //队列文件处理完毕后,处理相关的事情
                }
            }
        });
    </script>
    </body>
    </html>
    templates/upload.html 上传视频
    <!doctype html>
    <html lang="zh">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>xxx</title>
        <style></style>
    </head>
    <body>
        <div>
            <video id="video"  width="320" height="240" src="http://q5gq3qy79.bkt.clouddn.com/lucclaPl4-Hi4LcepzKNVWpfN4u7" controls="controls" autoplay="autoplay" muted loop="loop" >
                        您的浏览器不支持 video 标签。
             </video>
        </div>
    </body>
    </html>
    templates/vod.html 播放视频

      

  • 相关阅读:
    element-ui 中日期控件限制时间跨度
    自定义指令值绑定问题
    vuejs 深度监听
    vscode 快捷键
    JS跳转方式
    JSON
    面向对象编程
    DOM
    JS响应事件
    登录事件
  • 原文地址:https://www.cnblogs.com/xiaonq/p/12290151.html
Copyright © 2011-2022 走看看