1、VUE页面上传视频到后台

<el-upload class="upload-demo"
            :limit="1"
            :http-request="uploadVideo" <!--http-request 覆盖默认上传行为-->
    			  :file-list="fileList3">
    			  <el-button size="small" type="primary">点击上传</el-button>
    			  <div slot="tip" class="el-upload__tip">只能上传AVI、WMV、RM、RMVB、MP4等格式文件</div>
    			</el-upload>

2、自定义上传方法

import axios from 'axios'

     uploadVideo (event){

        let form = new FormData()
        var that = this
        form.append('file', event.file)
        form.append('dir', 'temp1')
        
        const option = {
          url: that.uploadURL,//上传路径
          data: form,
          method: 'post',
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        }
        axios(option).then((res) => {
          console.log('上传结束')
          console.log(res)
          that.dataInfo = res.data.data
          this.loadingUpload = false
          this.buttonName = '确定上传'
        }).catch((err) => {
          console.log('上传错误')
          console.log(err)
          this.loadingUpload = false
          this.buttonName = '确定上传'
        })


      },

3、下载 ffmpeg 插件并放入相应位置

链接:https://pan.baidu.com/s/16OC2ce1crgaTjChBCryWKA
提取码:75m5

注:如果下载后不可用,请自行搜索 ffmpeg 下载使用

4、后台接收文件并保存本地,截图并上传

@PostMapping("/uploadFile")
	public Result<VodUploadResponse> upload(@RequestParam("file")MultipartFile file,String dir) {
	    try {
            
            VodUploadResponse response = uploadFile(file, dir);
            
            return this.success(response);
        } catch (Exception var3) {
            return this.failure(var3.getMessage());
        }
	}
	
	public VodUploadResponse uploadFile(MultipartFile file, String resSort) throws Exception {
        String shortPath =  file.getOriginalFilename();
        File dest = new File("D://temp", shortPath);
        if (!dest.getParentFile().exists()) {
            boolean rel = dest.getParentFile().mkdirs();
            if (!rel) {
                throw new Exception("文件夹创建失败");
            }
        }
        InputStream is = file.getInputStream();
        OutputStream os = new FileOutputStream(dest);
        try {
            byte[] buffer = new byte[8 * 1024];
            int bytesRead;
            while ((bytesRead = is.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
        } catch (Exception e) {
            throw e;
        } finally {
            if (is != null) {
                is.close();
            }
            if (os != null) {
                os.close();
            }
        }
        VodUploadResponse response = uploadTecent("D://temp/".concat(shortPath));
        return response;
    }
	
	public VodUploadResponse uploadTecent(String url) {
	    VodUploadResponse response = null;
	    String imgurl = vidpicirl(url);
	    VodUploadClient client = new VodUploadClient(tencentSecretId, tencentSecretKey);
	    VodUploadRequest request = new VodUploadRequest();
	    request.setMediaFilePath(url);
	    request.setCoverFilePath(imgurl);
	    try {
	        response = client.upload("ap-beijing", request);
	        logger.info("Upload FileId = {}", response.getMediaUrl());
	    } catch (Exception e) {
	        // 业务方进行异常处理
	        logger.error("Upload Err", e);
	    }
	    return response;
	}
	
	
    
	public String vidpicirl(String videourl) {
	    
        System.err.println(videourl);

        String picId = UUID.randomUUID().toString().replace("-", "");

        String picurl = "D:\\temp\\"
                + picId + ".jpg";//截图存放路径

        List<String> commend = new java.util.ArrayList<String>();
        commend.add("D:\\ffmpeg.exe");//插件存放路径
        commend.add("-i");
        commend.add(videourl);
        commend.add("-y");
        commend.add("-f");
        commend.add("image2");
        commend.add("-ss");
        commend.add("1");
        commend.add("-t");
        commend.add("0.001");
        commend.add("-s");
        commend.add("350*240");
        commend.add(picurl);
        try {
            ProcessBuilder builder = new ProcessBuilder();
            builder.command(commend);
            builder.redirectErrorStream(true);
            
            Process process = builder.start();
            InputStream in = process.getInputStream();
            byte[] re = new byte[1024];
            
            while (in.read(re) != -1) {
                System.out.print(".");
            }
            System.out.println("");
            in.close();
            
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("视频截图失败!");
        }
        return picurl;
    }

 

转载于:https://my.oschina.net/u/2560963/blog/3081197

Logo

前往低代码交流专区

更多推荐