当前位置:   article > 正文

vue前端播放视频

vue前端播放视频
  1. <div style="display: flex; justify-content: center; align-items: center; height: 60vh; margin-top: 10vh;">
  2. <video ref="videoPlayer" controls style="width: auto; height: 100%;"></video>
  3. </div>
  4. </template>
  5. <script>
  6. export default {
  7. name: 'viewFDDetails',
  8. props: {
  9. tableData: {},
  10. id:String
  11. },
  12. data() {
  13. return {};
  14. },
  15. mounted() {},
  16. computed: {
  17. },
  18. methods: {
  19. viewVideo(row,type){
  20. console.log("row=",row);
  21. console.log("totalBatch=",row.totalBatch,"idNumber",row.idNumber,"batchName=",row.batchName);
  22. let videoName = row.batchName+"_"+row.idNumber+"_"+type;
  23. let videoUrl = row.totalBatch+"/"+videoName+".mp4";
  24. //视频地址先写这,后期改到配置里
  25. let url = "http://192.168.16.119:9001/images/"+videoUrl;
  26. console.log("videoUrl=",videoUrl)
  27. sessionStorage.setItem("videoUrl",'http://192.168.16.119:9001/images/1765267861926932482/20_511681200012212819_2.mp4');
  28. window.open("viewvideo","_blank");
  29. },
  30. //流方法播放视频
  31. playVideo(row, type) {
  32. console.log("进入playVideo");
  33. console.log("row=", row);
  34. console.log("totalBatch=", row.totalBatch, "idNumber", row.idNumber, "batchName=", row.batchName);
  35. let videoName = row.batchName + "_" + row.idNumber + "_" + type;
  36. let videoUrl = row.totalBatch + "/" + videoName + ".mp4";
  37. this.loadVideo("6666666","1_4865466371306164225_2.mp4");
  38. },
  39. loadVideo(videoPath,videoName) {
  40. const videoPlayer = this.$refs.videoPlayer;
  41. fetch(`http://127.0.0.1:8080/ftp/getVideo?videoPath=${encodeURIComponent(videoPath)}&videoName=${encodeURIComponent(videoName)}`)
  42. .then(response => {
  43. if (!response.ok) {
  44. throw new Error('Network response was not ok');
  45. }
  46. return response.blob();
  47. })
  48. .then(blob => {
  49. const videoBlob = new Blob([blob], { type: 'video/mp4' });
  50. const videoURL = URL.createObjectURL(videoBlob);
  51. videoPlayer.src = videoURL;
  52. videoPlayer.play();
  53. })
  54. .catch(error => {
  55. console.error('There was a problem with your fetch operation:', error);
  56. });
  57. }
  58. },

顺便带后端代码

  1. @CrossOrigin(origins = "*") // 允许任何来源的请求
  2. @GetMapping("/ftp/getVideo")
  3. public void getVideo(HttpServletResponse response, @RequestParam String videoPath, @RequestParam String videoName) {
  4. FTPClient ftp = new FTPClient();
  5. ftp.setControlEncoding("UTF-8");
  6. try {
  7. ftp.connect(FTPProperties.getServer(), FTPProperties.getPort());
  8. ftp.login(FTPProperties.getUsername(), FTPProperties.getPassword());
  9. int reply = ftp.getReplyCode();
  10. if (!FTPReply.isPositiveCompletion(reply)) {
  11. ftp.disconnect();
  12. throw new RuntimeException("FTP server refused connection.");
  13. }
  14. ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
  15. InputStream inputStream = ftp.retrieveFileStream(videoPath + "/" + videoName);
  16. if (inputStream != null) {
  17. try (OutputStream outputStream = response.getOutputStream()) {
  18. byte[] buffer = new byte[4096];
  19. int bytesRead;
  20. while ((bytesRead = inputStream.read(buffer)) != -1) {
  21. outputStream.write(buffer, 0, bytesRead);
  22. }
  23. outputStream.flush(); // 确保所有数据已写入输出流
  24. }
  25. // 关闭输出流
  26. response.flushBuffer();
  27. inputStream.close();
  28. ftp.logout(); // 登出
  29. } else {
  30. throw new RuntimeException("文件未找到");
  31. }
  32. } catch (IOException e) {
  33. // throw new RuntimeException("Error downloading file from FTP server", e);
  34. } finally {
  35. if (ftp.isConnected()) {
  36. try {
  37. ftp.disconnect();
  38. } catch (IOException e) {
  39. // log error or handle as needed
  40. }
  41. }
  42. }
  43. }
  44. @CrossOrigin(origins = "*") // 允许任何来源的请求
  45. @GetMapping("/ftp/getVideoList")
  46. public List<VideoInfo> getVideoList() {
  47. List<VideoInfo> videoList = new ArrayList<>();
  48. // 连接FTP服务器
  49. FTPClient ftp = new FTPClient();
  50. ftp.setControlEncoding("UTF-8");
  51. try {
  52. int reply;
  53. ftp.connect(FTPProperties.getServer(), FTPProperties.getPort());
  54. ftp.login(FTPProperties.getUsername(), FTPProperties.getPassword());
  55. reply = ftp.getReplyCode();
  56. if (!FTPReply.isPositiveCompletion(reply)) {
  57. ftp.disconnect();
  58. }
  59. FTPFile[] files = ftp.listFiles(); // 获取FTP服务器上所有文件
  60. for (FTPFile file : files) {
  61. listFilesRecursive(ftp, "/home/ftpuser", videoList);
  62. }
  63. } catch (Exception e) {
  64. e.printStackTrace();
  65. } finally {
  66. try {
  67. ftp.logout();
  68. ftp.disconnect();
  69. } catch (IOException e) {
  70. e.printStackTrace();
  71. }
  72. }
  73. return videoList;
  74. }
  75. private void listFilesRecursive(FTPClient ftp, String path, List<VideoInfo> videoList) throws IOException {
  76. FTPFile[] files = ftp.listFiles(path);
  77. for (FTPFile file : files) {
  78. if (file.isDirectory()) {
  79. // 如果是文件夹,递归进入文件夹
  80. listFilesRecursive(ftp, path + "/"+ file.getName() , videoList);
  81. } else if (file.getName().toLowerCase().endsWith(".mp4")) {
  82. // 处理视频文件
  83. String videoPath = path ; // 视频文件路径
  84. String videoName = file.getName(); // 视频文件名称
  85. videoList.add(new VideoInfo(videoPath, videoName));
  86. }
  87. }
  88. }
  89. //利用ftp技术,将文件上传到ftp服务器
  90. @CrossOrigin(origins = "*") // 允许任何来源的请求
  91. @PostMapping("/uploadFileToFTP")
  92. //ftp 上传方法
  93. public boolean uploadFile(@RequestParam("file") MultipartFile file,@RequestParam("batchId") String batchId) throws IOException {
  94. boolean flag = uploadFile(FTPProperties.getServer(),FTPProperties.getPort(),FTPProperties.getUsername(),
  95. FTPProperties.getPassword(),batchId,"video",file.getOriginalFilename(),file.getInputStream());
  96. //根据反馈输出日志
  97. if (flag){
  98. log.info("上传成功,文件名为:"+file.getOriginalFilename()+"文件大小="+file.getSize()+"字节"+"MB="+file.getSize()/1024/1024+"MB");
  99. }else {
  100. log.error("上传失败,文件名为:"+file.getOriginalFilename()+"文件大小="+file.getSize()+"字节"+"MB="+file.getSize()/1024/1024+"MB");
  101. }
  102. return flag;
  103. }
  104. /**
  105. * #!/bin/bash
  106. *
  107. * # 安装vsftpd
  108. * sudo yum remove vsftpd -y
  109. * sudo yum install vsftpd -y
  110. *
  111. * # 启动vsftpd服务
  112. * sudo systemctl start vsftpd
  113. *
  114. * # 检查服务状态
  115. * sudo systemctl status vsftpd
  116. * 云服务器记得开端口
  117. *
  118. * 创建一个新用户
  119. * 切记 必须在/etc/vsftpd/user_list
  120. *
  121. * 指定允许使用vsftpd的用户列表文件=》 也就是自己的用户名,要不然访问530错误
  122. * @param url
  123. * @param port
  124. * @param username
  125. * @param password
  126. * @param path
  127. * @param path1
  128. * @param filename
  129. * @param input
  130. * @return
  131. */
  132. public boolean uploadFile(String url,// FTP服务器hostname
  133. int port,// FTP服务器端口
  134. String username, // FTP登录账号
  135. String password, // FTP登录密码
  136. String path, // FTP服务器保存目录
  137. String path1, // FTP服务器保存目录1
  138. String filename, // 上传到FTP服务器上的文件名
  139. InputStream input // 输入流
  140. ){
  141. boolean success = false;
  142. FTPClient ftp = new FTPClient();
  143. ftp.setControlEncoding("UTF-8");
  144. try {
  145. boolean b = false;
  146. int reply;
  147. ftp.connect(url, port);// 连接FTP服务器
  148. // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
  149. ftp.login(username, password);// 登录
  150. reply = ftp.getReplyCode();// 获取服务器的响应码。
  151. if (!FTPReply.isPositiveCompletion(reply)) {
  152. ftp.disconnect();
  153. return success;
  154. }
  155. ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
  156. ftp.makeDirectory(path); //创建文件夹
  157. ftp.changeWorkingDirectory(path); //切换到文件夹
  158. // ftp.makeDirectory(path1);
  159. // ftp.changeWorkingDirectory(path1);
  160. b = ftp.storeFile(filename, input);//最终默认上传到 /home/ftpuser 目录下 批次id目录内
  161. if (b){
  162. //异步写入日志
  163. asynWriteVideoImportLog(path, filename, input);
  164. }
  165. input.close();
  166. ftp.logout();
  167. success = b;
  168. } catch (IOException e) {
  169. e.printStackTrace();
  170. } finally {
  171. if (ftp.isConnected()) {
  172. try {
  173. ftp.disconnect();
  174. } catch (IOException ioe) {
  175. }
  176. }
  177. }
  178. return success;
  179. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/336235
推荐阅读
相关标签
  

闽ICP备14008679号